Gitlab Runner 环境变量找不到

在定义环境变量的时候,Protect variable会限制“受保护的分支”和“受保护的标签才能访问”。受保护的分支在项目设置的repository菜单下可以设置,默认是master,这意味着如果提交develop分支是无法访问protect variable的。

以下是我碰到的场景

1
2
3
4
5
6
7
8
9
10
11
12
13
deploy_prod:
stage: deploy_prod
image:
name: docker-executor:latest
pull_policy: if-not-present
before_script:
- source /app/setup.sh
script:
- docker-compose --context production pull
- docker-compose --context production --env-file .env.prod up -d --force-recreate
rules:
- if: '$CI_COMMIT_REF_NAME == "master"' # 仅在主分支上触发生产环境部署
- when: manual # 手动触发,确保只有在测试通过后才进行正式环境部署

这种配置非master分支都会触发manual,if和when是或者的关系,当在非master分支提交后再手动触发该任务是无法访问到受保护的环境变量的。