Here’s a simple way to execute a git push from inside a gitlab runner.
Samuel Richard Magny / Push From Gitlab Runner
1. Generate an Access Token (or Personal Access Token)
From Project Access Token
Go to Settings > Access Token , and create a new token.
Check read_repository and write_repository

OR from Personal Access Token
Go to User > Preferences > Access Token , and create a new token.
Check read_repository and write_repository

2. Assign the Access Token to the CI/CD Pipeline
Go to Settings > CI/CD > Variables and add the variable.
You may name itGIT_PUSH_TOKEN .


3. Allow the pipeline pushing commits to the repository
Set reusable following step, to be done at global before_script or at each steps that need to make a git
.configure-git-push: &configure-git-push |
git remote set-url origin https://CI:${GIT_PUSH_TOKEN}@${CI_REPOSITORY_URL#*@}
git config --global user.name "CI"
git config --global user.email "ci@my.domain"
For example, this step may change a file according to a new version, then make a commit, tag the new commit and push changes.
commit new version:
script:
- *configure-git-push
- <update version file>
- git commit -m "[ci skip] New Version ${NEW_VERSION}" <CHANGED FILE>
- git push origin HEAD:$CI_COMMIT_BRANCH -o ci.skip
# Tag the commit
- COMMIT_ID=$(git rev-parse --short HEAD)
- TAG_NAME="v-${NEW_VERSION}"
- git tag $TAG_NAME $COMMIT_ID -f
- git push origin $TAG_NAME -o ci.skip
It’s that simple!
Fully working example link on top of the article!