提交代码后,由kubernetes中的gitlab-runner,编译构建发布。光是想想就好鸡冻了,对不对。如果gitlab-runner 也直接在kubernetes 中,随着任务量的变动而自动伸缩,多美妙啊。
部署gitlab runner
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
34gitlab-runner chart的value文件,最重要是修改gitlabUrl, 与注册token。
[root@ip-10-20-1-93 gitlab-runner]# grep -v '#' values.yaml | grep -v '^$'
imagePullPolicy: IfNotPresent
gitlabUrl: http://gitlab.srv.sunvalley/
runnerRegistrationToken: "3Pvqyxm3AJUURXkHW44K"
unregisterRunners: true
terminationGracePeriodSeconds: 3600
concurrent: 10
checkInterval: 30
rbac:
create: true
clusterWideAccess: false
metrics:
enabled: true
runners:
image: ubuntu:16.04
privileged: true
pollTimeout: 180
outputLimit: 4096
cache: {}
builds: {}
services: {}
helpers: {}
securityContext:
fsGroup: 65533
runAsUser: 100
resources: {}
affinity: {}
nodeSelector: {}
tolerations: []
hostAliases: []
podAnnotations: {}
podLabels: {}下一步在代码仓库中,配置k8s集群的信息。企业版可以对不同的环境配置做权限管控以及owner才有的发布控制,强烈推gitlab企业版。
接下来就是让gitlab runner 跑起来印证咯。
代码仓库中,创建.gitlab-ci.yml。
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#variables:
# MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
# MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
# DOCKER_HOST: "http://10.20.0.108:2375"
#build:
# stage: build
# image: harbor.sunvalley.com.cn/library/maven:3.0.5
# script:
# - test CI_COMMIT_REF_NAME == "master" && IMAGE_VERSION="latest" || IMAGE_VERSION=$CI_COMMIT_REF_NAME
# - mvn $MAVEN_CLI_OPTS clean package docker:build -DpushImage -Dmaven.test.skip=true -Dimage.version=${IMAGE_VERSION}
# cache:
# paths:
# - .m2/repository/
# artifacts:
# paths:
# - target/*.jar
# expire_in: 2 week
# environment:
# name: test
# only:
# - tags
test:
stage: test
script: echo "skipping test"
only:
- tags
deploy:
stage: deploy
image: harbor.sunvalley.com.cn/library/kubectl-tools:1.16.3
environment:
name: test
url: https://kube-dashboard.sunvalley.com.cn #内网开发注册中心
when: manual
only:
- tags # tag分支 commit 之后触发
script:
- echo "deploy to test"
- kubectl version
- cd manifests/
- sed -i "s/__CI_PROJECT_NAME__/${CI_PROJECT_NAME}/" *.yaml
- sed -i "s/__CI_ENVIRONMENT_SLUG__/${CI_ENVIRONMENT_SLUG}/" *.yaml
- sed -i "s/__VERSION__/${CI_COMMIT_REF_NAME}/" *.yaml
- |
if kubectl apply -f deployment.yaml | grep -q unchanged; then
echo "=> Patching deployment to force image update."
kubectl patch -f deployment.yaml -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"ci-last-updated\":\"$(date +'%s')\"}}}}}"
else
echo "=> Deployment apply has changed the object, no need to force image update."
fi
# - kubectl rollout status -f deployment.yaml
- kubectl get all,ing -l env=${CI_ENVIRONMENT_SLUG}