搭建GitLab CI&CD(基于GitLab Runner)
搭建GitLab CI&CD(基于GitLab Runner)
完成了搭建GitLab Runner(基于Docker)之后,可以基于GitLab Runner
搭建CI/CD
流水线。
前言
如果是安装我的文章中的步骤搭建的GitLab
,那么一定要在/srv/gitlab-runner/config
目录下的config.toml
文件中加上extra_hosts = ["gitlab.com:192.168.1.72"]
设置GitLab Runner
启动的Docker
容器的hosts
。因为我搭建GitLab
的时候使用的是自定义域名,在没有设置hosts
的情况下Docker
容器无法拉取代码。
concurrent = 1
check_interval = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "cicd"
url = "http://gitlab.com/"
token = "XeDWPgda3Ku2z9FdVFnT"
executor = "docker"
[runners.custom_build_dir]
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
[runners.cache.azure]
[runners.docker]
tls_verify = false
image = "node:alpine"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/cache"]
shm_size = 0
extra_hosts = ["gitlab.com:192.168.1.72"]
否则在处罚CI/CD之后会遇到这样的问题:
.gitlab-ci.yml
.gitlab-ci.yml
是GitLab CI/CD
的配置文件,相关命令可以参考官方文档。
编辑 .gitlab-ci.yml
.gitlab-ci.yml内容
# 定义阶段节点列表和阶段节点执行的顺序
stages:
# 构建阶段
- build
# 测试阶段
- test
# 部署阶段
- deploy
# 定义工作
build-job:
# 指定属于构建阶段
stage: build
# 指定使用cicd这个Runner
tags:
- cicd
# 执行脚本
script:
- echo "Compiling the code..."
- echo "Compile complete."
# 定义工作
unit-test-job:
# 指定属于测试阶段
stage: test
# 指定使用cicd这个Runner
tags:
- cicd
# 执行脚本
script:
- echo "Running unit tests... This will take about 60 seconds."
- sleep 60
- echo "Code coverage is 90%"
# 定义工作
lint-test-job:
# 指定属于测试阶段
stage: test
# 指定使用cicd这个Runner
tags:
- cicd
# 执行脚本
script:
- echo "Linting code... This will take about 10 seconds."
- sleep 10
- echo "No lint issues found."
# 定义工作
deploy-job:
# 指定属于部署阶段
stage: deploy
# 指定使用cicd这个Runner
tags:
- cicd
# 执行脚本
script:
- echo "Deploying application..."
- echo "Application successfully deployed."
提交.gitlab-ci.yml
编辑.gitlab-ci.yml
完成之后,点击Commit changes
提交。
提交触发流水线
提交.gitlab-ci.yml
之后,会触发一次流水线。
还没有评论,来说两句吧...