记录下使用gitlab的ci/cd的devops构建过程中,一些易忘点或者踩坑点:
- 官方文档中英文(建议英文)
https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html
https://gitlab.cn/docs/jh/ci/pipelines/schedules.html - 为什么创建了流水线后,触发了一次流水线之后会自动不停地创建流水线?
如果你在你的工程设置中,让流水线给你自动生成tag,该tag的提交就会被push到代码库,而如果你每禁用push事件或每指定具体push规则,每次push又会创建运行新的流水线,因此造成了死循环;
解决方法要么把流水线改为手动触发,要么禁用push触发,要么就细化push触发规则
一个手动触发的案例如下:
yaml
stages: # List of stages for jobs, and their order of execution
- build
- release
- package
- test
- deploy
#workflow:
# rules:
#- if: '$CI_PIPELINE_SOURCE == "push"'
# when: never
# - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
build_job: # This job runs in the build stage, which runs first.
stage: build
rules:
- if: '$CI_PIPELINE_SOURCE == "push"'
when: manual
# - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
script:
- work_root=`pwd`
- echo "wortroot:${work_root} " >> release.log
- chmod +x script/build_control.sh && sh script/build_control.sh >> release.log
- tar -cf output.tar build >> release.log
artifacts:
paths:
- output.tar
- release.log
expire_in: 1 week
release_job:
stage: release
needs: ["build_job"]
#rules:
# - if: '$CI_PIPELINE_SOURCE == "push"'
# when: never
script:
- echo "$CI_PIPELINE_SOURCE" >> release.log
- echo "running release_job" >> release.log
- export CI_COMMIT_TAG=`date +"%Y%m%d%H%M%S"`
- echo "$CI_COMMIT_TAG" >> release.log
- export ci_commit_branch=${CI_COMMIT_BRANCH}
- echo "$CI_COMMIT_BRANCH" >> release.log
- release-cli create --description "release $CI_COMMIT_TAG" --tag-name "${ci_commit_branch}.v.$CI_COMMIT_TAG"
dependencies:
- build_job
- 为什么我在一些阶段的产出文件无法在最终流程结束后找到也无法被下一个阶段使用?
这是因为每个阶段都会删除上一阶段的数据,重新拉取运行,如果你希望你上一个阶段的脚本执行产出被保留,需要使用artifacts,上面的案例也有 - gitlabrunner可以自己装在自己的服务器上,不一定要用它推荐的镜像
- 为什么找不到CI_COMMIT_TAG环境变量?或者无法把gitlab-runner里面的环境变量带到流水线的脚本里?
目前这是gitlab设计的一个已知问题,把gitlab流水线的专有变量带到gitlab-runner所在实例可采用示例中export方式,然后你就可以直接使用了。
两个比较多的案例
https://zhuanlan.zhihu.com/p/510820543
https://blog.51cto.com/busy/6543293
未完待续...