I have a Job that can only be triggered manually. When triggered, it generates a release version commit and tag.
I would like to be able to push that commit and tag to the current commit branch, and the push should be "by the user".
Is there any way to accomplish this? I dont want to use a token, because that loses the user.
I dont want to use a token, because that loses the user.
Using a token doesn't prevent you from using any particular git author. One option is just set the git config to use the email of the user that triggered the job. This is available in the predefined environment variable $GITLAB_USER_EMAIL
.
Before writing the commit/tag, set the user email in the git config:
git config --local user.email "$GITLAB_USER_EMAIL"
GitLab uses the commit author email to associate commits with users. So if you do this, the commit and tag will be associated with the user who triggered the job and their portrait will show up in the UI for the commit.
You can also add the git user.name
if you want with the variable $GITLAB_USER_NAME
though this is not strictly necessary. You may also use a different name to help differentiate automated commits.
As a complete example (for branch pipelines, using a project access token stored as the variable PROJECT_ACCESS_TOKEN
):
# set the user properties
git config --local user.email "$GITLAB_USER_EMAIL"
git config --local user.name "$GITLAB_USER_NAME (gitlab pipeline)"
# set the remote URL with authentication
# if using a personal access token,
# you will need to replace `gitlab-ci-token` with your username
git remote set-url origin "https://gitlab-ci-token:${PROJECT_ACCESS_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git"
# Uncomment these steps if you have an error for not being on a branch
# git fetch origin
# git checkout "$CI_COMMIT_BRANCH"
# create a commit and push it to the commit branch
git commit --allow-empty -m "my release commit"
git push origin "$CI_COMMIT_BRANCH"
# create a tag and push the tag
git tag "my-realease-tag"
git push origin "my-release-tag"