Protected branches, tags and variables

Who can change main, who can cut a release, and which pipelines get the secrets.

Intermediate13 min · lesson 7 of 17
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (17 files, 6 KB): protected.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-gitlab/protected.tar.gz && tar -xzf protected.tar.gz, which creates ~/lab/protected/. SHA-256: a52bdacfee7d2b8d68559a769843afd9c30d2c227f2bb9e08a2fd7f5d1bfb9dd

A production deploy token lives in a CI/CD variable. Any developer can create a branch, and any branch runs the pipeline. Put those two facts together and every developer can read the production token, whatever the deploy job's rules say. GitLab's answer is one idea applied three times: some refs are protected, only some people can change them, and only pipelines on them receive protected secrets.

The setup

A project with a protected variable, PROD_TOKEN, and a second user, dev, who has the Developer role. member.sh adds the membership and waits until GitLab has applied it, since permissions are refreshed in the background. The pipeline only prints which ref it runs on and the length of PROD_TOKEN, never the value:

app/.gitlab-ci.yml
# Prints which secrets this pipeline received. Lengths only: never print a secret, even masked.
where-am-i:
image: secopslog-py:3.15
script:
- echo "ref $CI_COMMIT_REF_NAME, protected=$CI_COMMIT_REF_PROTECTED"
- echo "PROD_TOKEN length ${#PROD_TOKEN}"
ubuntu@secopslog-cicd:~/lab/protected · GitLab 19.5.3
$ ./up.sh
runner lab-runner: online (tags: lab,docker) GitLab 19.5.3 is up on http://127.0.0.1:8929
$ ./seed.sh app app/ && ./user.sh dev && ./member.sh app dev 30 && ./gl.sh /projects/root%2Fapp/variables -X POST -d key=PROD_TOKEN -d value=prod-fedcba9876543210 -d masked=true -d protected=true -o /dev/null && echo 'dev is a Developer; PROD_TOKEN is protected'
pushed app: 360c303 first commit user dev created (id 2), token stored in .env dev has access level 30 on root/app dev is a Developer; PROD_TOKEN is protected
$ ./gl.sh /projects/root%2Fapp/protected_branches | jq -r '.[] | "\(.name): push=\([.push_access_levels[].access_level_description] | join(",")) merge=\([.merge_access_levels[].access_level_description] | join(",")) force_push=\(.allow_force_push)"'
main: push=Maintainers merge=Maintainers force_push=false

A new project protects its default branch out of the box (an instance-wide default that administrators can change): only Maintainers may push to main or merge into it, and nobody may force-push.

Who can change main

ubuntu@secopslog-cicd:~/lab/protected · GitLab 19.5.3
$ echo "# dev was here" >> app/calc.py && ./as.sh dev ./commit.sh app/ "straight to main"
remote: GitLab: You are not allowed to push code to protected branches on this project. To http://127.0.0.1:8929/root/app.git ! [remote rejected] HEAD -> main (pre-receive hook declined) error: failed to push some refs to 'http://127.0.0.1:8929/root/app.git'

Refused by a server-side hook. A Developer can still work on a branch and propose the change:

ubuntu@secopslog-cicd:~/lab/protected · GitLab 19.5.3
$ git -C app reset -q --hard origin/main && echo "# dev was here" >> app/calc.py && ./as.sh dev ./commit.sh app/ "via a branch" feature/dev && ./pipeline.sh app/ | grep -E "^(ref|PROD_TOKEN|pipeline)"
pushed: 9dd314e via a branch to feature/dev ref feature/dev, protected=false PROD_TOKEN length 0 pipeline #2: success
$ ./as.sh dev ./gl.sh /projects/root%2Fapp/merge_requests -X POST -d source_branch=feature/dev -d target_branch=main -d title=dev | jq -r '"MR !\(.iid) opened by \(.author.username)"'
MR !1 opened by dev
$ sleep 3; ./as.sh dev ./gl.sh /projects/root%2Fapp/merge_requests/1/merge -X PUT
curl: (22) The requested URL returned error: 401 {"message":"401 Unauthorized"}

The branch pipeline ran with protected=false and got a PROD_TOKEN of length 0: the variable simply isn't there. The merge request was opened. And dev couldn't merge it, because merging into main needs the Maintainer role here. GitLab's API answers that with 401 Unauthorized, even though dev's token is valid. Now start a pipeline on main itself:

ubuntu@secopslog-cicd:~/lab/protected · GitLab 19.5.3
$ git -C app checkout -q main && git -C app pull -q && ./run-pipeline.sh app && sleep 3 && ./pipeline.sh app/ | grep -E "^(ref|PROD_TOKEN|pipeline)"
pipeline #3 created source=api ref main, protected=true PROD_TOKEN length 21 pipeline #3: success

On a protected ref, the pipeline received the token, 21 characters. A secret marked protected is handed only to pipelines running for protected branches or tags, so the people who can run code with it are the people who can change those refs.

Who can make a release tag

Tags matter as much as branches: release pipelines usually run on tags, and a protected tag pipeline also receives protected variables. Protect the v* pattern so only Maintainers can create release tags:

ubuntu@secopslog-cicd:~/lab/protected · GitLab 19.5.3
$ ./gl.sh /projects/root%2Fapp/protected_tags -X POST -d 'name=v*' -d create_access_level=40 | jq -r '"protected tag \(.name): create=\(.create_access_levels[0].access_level_description)"'
protected tag v*: create=Maintainers
$ ./as.sh dev ./gl.sh /projects/root%2Fapp/repository/tags -X POST -d tag_name=v1.0.0 -d ref=main
curl: (22) The requested URL returned error: 400 {"message":"You are not allowed to create this tag as it is protected."}
$ ./gl.sh /projects/root%2Fapp/repository/tags -X POST -d tag_name=v1.0.0 -d ref=main | jq -r '"tag \(.name) at \(.target[0:8])"' && sleep 3 && SHA=$(git -C app rev-parse HEAD) ./pipeline.sh app/ | grep -E '^(ref|PROD_TOKEN|pipeline)'
tag v1.0.0 at 360c3037 ref v1.0.0, protected=true PROD_TOKEN length 21 pipeline #4: success

The Developer's tag was refused. The Maintainer's tag v1.0.0 started a pipeline that ran with protected=true and the token. Unprotected tags are a quiet hole: if any developer can push a tag that your release jobs trust, a protected main doesn't stop them.

SettingProtectsFree tier
Protected branchWho can push, merge and force-pushYes
Protected tagWho can create matching tagsYes
Protected variableOnly pipelines on protected refs receive itYes
Protected environmentWho can deploy to an environmentNo, Premium and above
Required approvals on merge requestsMerges need reviewersNo, Premium and above
Watch out
Protection is only as strong as the list of people who can change it. A Maintainer can unprotect a branch, push, and protect it again. Keep Maintainer for the people you'd trust with production, and remember that anyone who can push to or merge into a protected branch can also edit the pipeline that holds the protected secrets. Every protected ref receives every protected variable, so a protected release/* pattern that Developers may push hands them every deployment secret. Keep the set of protected refs small and Maintainer-only.

Hands-on

Protect a release branch pattern: ./gl.sh /projects/root%2Fapp/protected_branches -X POST -d 'name=release/*' -d push_access_level=40 -d merge_access_level=40. Change a file (echo "# release" >> app/calc.py) and try to push release/1.0 as dev with ./as.sh dev ./commit.sh app/ "release" release/1.0. Then push the same local branch as root with git -C app push -u origin release/1.0 and run ./pipeline.sh app/. Which pipeline gets PROD_TOKEN? Finish with ./down.sh.

What you should see: as dev, the push of release/1.0 was rejected with You are not allowed to create protected branches on this project. The pattern protects branches that don't exist yet, so nobody below Maintainer can create one. Root pushed the same branch, its pipeline ran with protected=true, and PROD_TOKEN arrived (length 21). A wildcard protects every future release branch at once, so you never have to remember to protect a new one.

Remember

Mark every deployment secret protected, protect the branches and tags that deploy, and test it from a Developer account: the branch pipeline should see an empty variable.

Quick check
01PROD_TOKEN is a protected variable. A Developer pushes feature/x with a job that prints ${#PROD_TOKEN}. What does the job print?
Incorrect — Masking would hide the value, but here there is no value at all.
Incorrect — The job runs normally. It just does not receive the variable.
Incorrect — Protection decides which pipelines receive the variable.
Correct — Unprotected refs do not receive protected variables. The lab showed PROD_TOKEN length 0 on feature/dev and 21 on main.
02Release pipelines run on tags, and main is protected. Tags are not protected. What is the risk?
Incorrect — A tag can point at any commit, including one on the developer's own branch.
Correct — Pushing a tag is enough, and a tag can point at any commit. Protect the tag pattern your release jobs trust, such as v*.
Incorrect — Protected tags do receive them. That is why tag creation must be restricted.
Incorrect — GitLab does not delete tags.
03Which of these needs a paid tier, so the Free tier has to do without it?
Incorrect — They are available on every tier.
Incorrect — They are available on every tier.
Correct — Protected environments limit who may deploy. On the Free tier, protected branches, tags and variables have to cover it.
Incorrect — They are available on every tier.

Related