1.局部/全局环境变量
stages:
- testing # stage编排
- build
- deploy
variables:
global_var: "全部变量" #全部变量
build_image:
stage: build
variables: #局部环境变量
my_name: "局部环境变量"
tags:
- shell
script:
- echo "打包镜像"
- echo "$my_name" #打印局部环境变量
- echo "$global_var" #打印全局环境变量
deploy_to_qa:
stage: deploy
tags:
- shell
script:
- echo "部署到QA"
运行结果:
2.判断语句
stages:
- testing # stage编排
- build
- deploy
workflow:
rules:
- if: $CI_COMMIT_BRANCH == "dev" #如果提交的分支,等于dev,则执行(类似if...else)
when: always
- when: never #类似else
run_test: #Job
stage: testing
tags:
- shell #Gitlab-runner
script:
- echo "执行测试."
- chmod +x ./run.sh # 给run.sh加可执行权限
- ./run.sh # 运行外部文件./run.sh
执行结果:(不做,因为在master分支提交的)
3.自定义系统 全局变量
stages:
- testing # stage编排
- build
- deploy
run_test: #Job
stage: testing
tags:
- shell #Gitlab-runner
script:
- echo "执行测试."
- chmod +x ./run.sh # 给run.sh加可执行权限
- ./run.sh # 运行外部文件./run.sh
- echo "$BAIDU" # 打印自定义系统全局变量
执行结果: