安装Docker
类型的GitLabRunner
一定要是Docker
类型的GitLabRunner
,之前测试时,没注意到这一点,一直测试没通过
详情参考之前文章
Let's Do it
.gitlab-ci.yaml
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
variables:
REGISTRY: "registry.cn-shenzhen.aliyuncs.com/siss"
REGISTRY_LOCAL: "hub.siss.io:5000/siss"
before_script:
- PROJECT=${CI_PROJECT_NAMESPACE}
- ENV=${CI_ENVIRONMENT_NAME}
- NAMESPACE=${PROJECT}
- APP=${CI_PROJECT_NAME}
- export TAG=$(date +%Y-%m-%d)
- IMAGE=${REGISTRY}/${CI_PROJECT_NAMESPACE}_${CI_PROJECT_NAME}
- LOCAL_IMAGE=${REGISTRY_LOCAL}/${CI_PROJECT_NAMESPACE}_${CI_PROJECT_NAME}
stages:
- build image
- deploy to k8s
# 相同stage名称定义多个时,并行执行
build-by-host:
stage: build image
when: delayed
start_in: 1 seconds
only:
- master
script:
- echo ${TAG}
- docker build -t ${LOCAL_IMAGE} .
- docker login -u admin -p 123456 ${REGISTRY_LOCAL}
- docker push ${LOCAL_IMAGE}
tags:
- dev
build-by-kaniko:
stage: build image
only:
- master
image:
name: raydoom/kaniko-project-executor:debug
entrypoint: [""]
script:
# 仓库验证信息通过配置文件挂载的方式,挂载进了kaniko的容器内部/kaniko/.docker/config.json
- >-
/kaniko/executor --insecure --skip-tls-verify
--cache=true
--cache-dir /cache
--context "${CI_PROJECT_DIR}"
--dockerfile "${CI_PROJECT_DIR}/Dockerfile"
--destination "${LOCAL_IMAGE}"
tags:
- kaniko
deploy:
stage: deploy to k8s
environment:
name: dev
url: http://dev.siss.io/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}
only:
- master
script:
- echo "image=${LOCAL_IMAGE}:${TAG}"
- ${HOME}/deploy.sh ${LOCAL_IMAGE}
tags:
- dev
|
优化
基础镜像缓存
将基础镜像nginx:alpine
缓存至宿主机的/cache
目录,在config.toml
中,/cache
目录挂载进容器里面的/cache
目录里面
1
2
3
4
5
6
|
docker run --rm \
-v /cache:/cache \
gcr.io/kaniko-project/warmer:latest \
--cache-dir=/cache \
--image=ImageName1 \
--image=ImageName2
|