GitLabCICD 入门
- 编写 .gitlab-ci.yml 文件
- 创建项目后在项目中有.gitlab-ci.yml提交到gitlab后会自动跑流水线
- .gitlab-ci.yml 文件案例yaml
# 自定义的job名称 helloworld: # 执行的脚本命令 script: - echo "hello world,gitlab" run_test: script: - echo "执行单元测试3" # 看名字就知道 before_script: - echo "1" - echo "2" # 看名字就知道 after_script: - echo "4" bad_test: script: - yfd
- 工作阶段以及依赖性
- stages 代表阶段如果testing失败了那么deploy也就不执行了
- 我们看到testing阶段下有2个任务,如果想做到更精确的流程控制增加了needs关键字
yamlstages: - linter - testing - build - deploy helloworld: stage: linter script: - echo "hello world,gitlab" run_test: stage: testing needs: bad_test script: - echo "执行单元测试3" bad_test: stage: testing script: - yfd deploy_dowm: stage: deploy script: - echo "部署完成"
- 执行外部脚本
- 执行 ./run.sh脚本
- chmod +x ./run.sh
shellecho "外部文件执行" ls -al echo "外部文件执行end"
yamlstages: - linter - testing - build - deploy helloworld: stage: linter script: - echo "hello world,gitlab" - chmod +x ./run.sh - ./run.sh run_test: stage: testing needs: - bad_test script: - echo "执行单元测试3" bad_test: stage: testing script: - yfd deploy_dowm: stage: deploy script: - echo "部署完成"
- 指定分支
- only except 关键字
yamlstages: - linter - testing - build - deploy helloworld: stage: linter script: - echo "hello world,gitlab" - chmod +x ./run.sh - ./run.sh run_test: stage: testing script: - echo "执行单元测试3" build_docker_image: stage: build only: - mian script: - echo "构建docker镜像" deploy_dowm: stage: deploy only: - main script: - echo "部署完成"
- 环境变量
- 在yml中配置环境变量
- 还可以在gitlab页面中配置
- gitlab 中有很多已经定义好的变量 CI_COMMIT_BRANCH CI_PIPELINE_SOURCE 预定义的环境变量
yaml
stages:
- linter
- testing
- build
- deploy
# 全局变量
variables:
MY_URL: "全局变量"
helloworld:
# 局部变量只能在当前job下使用
variables:
MY_NAME: "YFD"
stage: linter
script:
- echo "hello world,$MY_NAME"
7.使用 workflow: 来确定是否创建流水线
yaml
workflow:
rules:
- if: $CI_COMMIT_BRANCH == "dev"
- when: never
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
- when: always