committing to repository as part of CI build

3.8k views Asked by At

I have a CI pipeline that is likely doing something semi-perverted. Let's not debate that part.

As part of the CI, I will generate an artifact (README.md) which I would like to commit and push back to the same repository. Simply using git push origin ... doesn't work due to authentication error.

Am I constrained to using something like a secret variable and a token, and adding another remote so that it can push?

2

There are 2 answers

0
MadDocNC On

Gitlab seems to change .git folder after fetching project for CI job. I'm not sure it changes only remote section. So I found only solution is to add gitlab-runner user with sshkeys to gitlab. And in my job make git clone in separate folder, make changes, then commit and push it.

2
mohamnag On

There are ways to add a ssh token to your build runtime which is able to commit or even do a push to origin. I think even recently GitLab added a new functionality that for each build a unique token is generated which can be used for same sake.

However in general I dont think you can commit anything on the same git base that build is running on, as the check out is in a detached head mode. This means you will not be able to add to history, specially in a remote.

Next problem to consider is what this means if you were able to commit back for the build system, which can potentially trigger another build and a cycle will be triggered.

So probably either use the artifact system for it or do add the ssh token and clone/checkout/commit/push in a separate directory during the build. Anyway this doc explains how to add the token: https://docs.gitlab.com/ee/ci/ssh_keys/README.html