gitlabci部署kubernetes应用程序示例
# 创建Dockerfile文件
FROM docker.cnb.cool/zzppjj/docker-images/php:7.2.34-fpm-alpine
WORKDIR /app
ENV TZ=Asia/Shanghai
# === 重点修改这里 ===
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
sed -i 's/mirrors.ustc.edu.cn/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
apk update && \
apk add --no-cache \
autoconf g++ libtool make \
curl-dev libxml2-dev linux-headers \
freetype-dev libjpeg-turbo-dev libpng-dev
# 后面部分保持不变(建议拆分 RUN)
RUN docker-php-ext-install -j2 zip
RUN docker-php-ext-configure gd \
--with-freetype-dir=/usr/include/freetype2 \
--with-jpeg-dir=/usr/include/ && \
docker-php-ext-install -j2 gd
RUN docker-php-ext-install -j$(nproc) bcmath mysqli pdo_mysql
RUN apk add --no-cache nginx && mkdir -p /run/nginx
COPY docker/site.conf /etc/nginx/conf.d/default.conf
COPY . .
COPY docker/start.sh /start.sh
RUN chmod +x /start.sh && \
chown -R www-data:www-data /app
EXPOSE 80
CMD ["/start.sh"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
docker目录下内容
# site.conf
server {
listen 80;
server_name localhost;
root /app/public;
client_max_body_size 50m;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header 'Access-Control-Allow-Headers' 'X-CSRF-Token';
index index.html index.htm index.php;
charset utf-8;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
root /app/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#location ~ /\.(?!well-known).* {
# deny all;
# }
# location ~* \.(sh)$ {
# deny all;
# }
}
# start.sh
nginx
php-fpm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 在代码仓库创建.gitlab-ci.yml文件
variables:
IMAGE_NAME: "docker.cnb.cool/zzppjj/docker-images/litghtpicture"
IMAGE_TAG: "$CI_COMMIT_SHORT_SHA"
CONFIG_REPO: "git@192.168.51.50:root/argo-demo.git"
stages:
- build
- deploy
build:
stage: build
image: docker:24.0
services:
- docker.cnb.cool/zzppjj/docker-images/docker:24.0-dind
script:
- docker login -u cnb docker.cnb.cool -p 01d21Lwxp6gMZBcf0o7BnPeaNgM
- docker build -t $IMAGE_NAME:$IMAGE_TAG -t $IMAGE_NAME:latest .
- docker push $IMAGE_NAME:$IMAGE_TAG
- docker push $IMAGE_NAME:latest
only:
- main
update-manifests:
stage: deploy
image: docker.cnb.cool/zzppjj/docker-images/git # 留着没事,等以后换了 Docker Executor 会自动生效
needs: ["build"]
script:
# 1. 删掉了 apk add --no-cache git openssh-client (因为宿主机已经装好了)
- git config --global user.email "ci@gitlab.com"
- git config --global user.name "GitLab CI"
- git clone $CONFIG_REPO
- cd argo-demo
- cd manifests
- |
find . -type f \( -name "*.yaml" -o -name "*.yml" \) | xargs sed -i "s|image: .*picture:[^[:space:]]*|image: $IMAGE_NAME:$IMAGE_TAG|g"
- git add .
- |
git diff --cached --quiet || git commit -m "chore: update image $IMAGE_NAME to $IMAGE_TAG [skip ci]"
- git push origin main
only:
- main
environment: production
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
上次更新: 2026/05/21, 21:01:23
|