document how to finish a (scheduled) pipeline with a Git push
Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.
As a user who doesn't already know how to do it, I would like to find easily in the documentation how to finish a (scheduled) pipeline with a Git push.
I believe one common case for running a Schedule is to automate code maintenance tasks, or single-sourcing tasks. The Pipeline should finish by pushing the changes back to the repository.
I couldn't find how to cover my use case by searching the documentation, and reading multiple related topics.
I ended up creating a project access token [1] and script to run in the deploy stage [2]. I believe this script is fragile and there must be a better and easier way.
[1] https://docs.gitlab.com/ee/user/project/settings/project_access_tokens.html#project-access-tokens
[2]
#!/usr/bin/env bash
#
# Copyright (c) 2022 Red Hat, Inc.
#
# SPDX-License-Identifier: MIT
#
# Commit, and push changes to GitLab
#
# Fail on error
set -e
# Behave differently on GitLab
case $CI in
true) # We are on GitLab CI
# Accept self signed certificate in certificate chain
git config http.sslVerify false
# Set user identity
git config user.email "${GIT_USER_EMAIL:-$GITLAB_USER_EMAIL}"
git config user.name "${GIT_USER_NAME:-$GITLAB_USER_NAME}"
# GitLab works on a detached head
git config push.autoSetupRemote true
# Add remote to enable push
git remote add upstream "https://${GITLAB_USERNAME}:${GITLAB_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git" || true
# Set push command
PUSH_COMMAND="git push upstream HEAD:${CI_COMMIT_BRANCH}"
# Display available variables
env
;;
*) # We are on a local environment
# Set push command
PUSH_COMMAND="git push"
;;
esac
# Check if we have modifications to commit
CHANGES=$(git status --porcelain | wc -l)
if [ "$CHANGES" -gt "0" ]; then
# Stage the changes
git add .
# Show the status of files that are about to be created, updated or deleted
git status
# Commit all changes
git commit -m "Single sourced on $(date -Iminutes)"
$PUSH_COMMAND
fi