Starting from GitLab 17.2[1], you can grant the push/write privilege to gitlab-ci-token
for your repository, which is not allowed by default (docs):
To grant permission to job tokens generated in your project to push to the project’s repository:
- On the left sidebar, select Search or go to and find your project.
- Select Settings > CI/CD.
- Expand Job token permissions.
- In the Permissions section, select Allow Git push requests to the repository.
You can then push changes like this:
script:
- ...
- git config --global user.email "$GITLAB_USER_EMAIL"
- git config --global user.name "$GITLAB_USER_NAME"
- git remote set-url --push origin "https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git"
- git add -A
- 'git commit -m "refactor: apply changes from pipeline $CI_PIPELINE_ID"'
- git push origin HEAD:$CI_COMMIT_REF_NAME
[1] In GitLab 17.2 and later minor releases, this feature is hidden behind a feature flag. You have to enable allow_push_repository_for_job_token
to make the option available in the UI. Currently, it is estimated that this feature will become GA in 17.6. For updates, have a look at this issue.