From 5955a6121989f43156a1972426e33530a6c5b853 Mon Sep 17 00:00:00 2001 From: Melekhin Anton Date: Mon, 1 Jul 2024 17:44:13 +0400 Subject: [PATCH] ci: add `update-version` workflow --- .github/scripts/update-version.sh | 74 ++++++++++++++++++++++++++++ .github/workflows/update-version.yml | 23 +++++++++ 2 files changed, 97 insertions(+) create mode 100755 .github/scripts/update-version.sh create mode 100644 .github/workflows/update-version.yml diff --git a/.github/scripts/update-version.sh b/.github/scripts/update-version.sh new file mode 100755 index 0000000..b0f1eff --- /dev/null +++ b/.github/scripts/update-version.sh @@ -0,0 +1,74 @@ +#!/bin/bash + +set -ex + +# Git commiter +GIT_USER="${GITHUB_ACTOR}" +GIT_MAIL="${GITHUB_ACTOR}@users.noreply.github.com" + +# Script output colors +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +RED='\033[0;31m' +NO_COLOR='\e[0m' + +# Get current version +CURRENT_VERSION="$(awk '/^.*_binary_version:/{print $2}' 'defaults/main.yml' | tr -d \')" + +# Get latest version +URL='https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab-runner/releases' +LATEST_VERSION="$(curl --silent $URL | jq '.[0].tag_name' | tr -d '"v')" + +# Validate latest version +if ! [[ $LATEST_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] ; then + echo "error: ${LATEST_VERSION} is not a valid Semantic Version" >&2; exit 0 +fi + +# Compare current and latest versions +if [[ $CURRENT_VERSION == $LATEST_VERSION ]]; then + echo -e "${GREEN}Newest version is used.${NO_COLOR}" + exit 0 +fi + +# Update latest version +sed -i '' "s/${CURRENT_VERSION}/${LATEST_VERSION}/" 'defaults/main.yml' +sed -i '' "s/${CURRENT_VERSION}/${LATEST_VERSION}/" 'README.md' + +# Repository variables +REPO_NAME=$(git config --get remote.origin.url | sed -e 's|^https://github.com/||') +UPDATE_VERSION_BRANCH="update-to-${LATEST_VERSION}" +UPDATE_VERSION_COMMIT="fix(version): gitlab-runner updated to \`${LATEST_VERSION}\` release" + +# Create an update branch +REMOTE_BRANCH="$(curl --silent https://github.com/gitapi/repos/${REPO_NAME}/branches/${UPDATE_VERSION_BRANCH} | jq -r .name)" + +if [ "${REMOTE_BRANCH}" == null ] ; then + git config user.name "${GIT_USER}" + git config user.email "${GIT_MAIL}" + git checkout -b "${UPDATE_VERSION_BRANCH}" + + # Push new version + git add defaults/main.yml README.md + git commit --signoff -m "${UPDATE_VERSION_COMMIT}" + + echo -e "${GREEN}Pushing to ${UPDATE_VERSION_BRANCH} branch.${NO_COLOR}" + if ! git push "https://${GITHUB_TOKEN}:@github.com/${REPO_NAME}" --set-upstream "${UPDATE_VERSION_BRANCH}"; then + echo -e "${RED}Branch push failed.${NO_COLOR}" + exit 1 + fi +else + echo -e "${YELLOW}Branch is already on remote.${NO_COLOR}" +fi + +# Create Pull Request +RELEASE_NOTES="$(curl --silent $URL | jq '.[0].description')" +PR_MESSAGE="The upstream GitLab Runner has released a new software version - **${LATEST_VERSION}**!\n\n${RELEASE_NOTES}\n\nThis automated PR updates code to bring new version into repository." +PR_JSON="$(printf '{"title":"%s","body":"%s","head":"%s","base":"%s"}' "${UPDATE_VERSION_COMMIT}" "${PR_MESSAGE}" "${UPDATE_VERSION_BRANCH}" "main")" + +curl -L \ + -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${GITHUB_TOKEN}"\ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://github.com/gitapi/repos/${REPO_NAME}/pulls" \ + -d "${PR_JSON}" diff --git a/.github/workflows/update-version.yml b/.github/workflows/update-version.yml new file mode 100644 index 0000000..4abc46d --- /dev/null +++ b/.github/workflows/update-version.yml @@ -0,0 +1,23 @@ +--- +name: 'Check for a new upstream version' +on: + schedule: + - cron: '59 23 * * 1' + workflow_dispatch: + +jobs: + update-version: + name: 'Update version' + runs-on: 'ubuntu-latest' + permissions: + contents: 'write' + pull-requests: 'write' + steps: + - name: 'Checkout the codebase' + uses: 'actions/checkout@v4' + + - name: 'Update version' + run: .github/scripts/update-version.sh + env: + GITHUB_ACTOR: ${{ github.actor }} + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}