From 02925b67a82976acbc1571bfff15b0af19badb6d Mon Sep 17 00:00:00 2001 From: Anton Melekhin <44847512+antmelekhin@users.noreply.github.com> Date: Mon, 16 Oct 2023 01:06:54 +0400 Subject: [PATCH] ci: add script and workflow for update version (#5) --- .github/scripts/update-version.sh | 70 ++++++++++++++++++++++++++++ .github/workflows/update-version.yml | 22 +++++++++ defaults/main.yml | 1 - 3 files changed, 92 insertions(+), 1 deletion(-) 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..a852937 --- /dev/null +++ b/.github/scripts/update-version.sh @@ -0,0 +1,70 @@ +#!/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 '/^.*_version:/{print $2}' 'defaults/main.yml' | tr -d \')" + +# Get latest version +URL='https://my.atlassian.com/download/feeds/current/jira-software.json' +APP_JSON="$(curl --silent $URL | sed -e 's/downloads//' -e 's/[()]//g' | jq '[ .[] | select((.edition == "Enterprise") and (.platform == "Mac OS X, Unix")) ]' | jq 'max_by(.version)')" +LATEST_VERSION="$(echo $APP_JSON | jq -r '.version')" + +# 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/_version:.*$/_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): jira updated to \`${LATEST_VERSION}\` release" + +# Create an update branch +REMOTE_BRANCH="$(curl --silent https://api.github.com/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="$(echo $APP_JSON | jq -r '.releaseNotes')" +PR_MESSAGE="Atlassian released a new LTS version of [Jira](${RELEASE_NOTES}) - **${LATEST_VERSION}**!\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://api.github.com/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..bb14a3e --- /dev/null +++ b/.github/workflows/update-version.yml @@ -0,0 +1,22 @@ +--- +name: 'Check for a new upstream version' +on: + schedule: + - cron: '59 23 * * 1' + workflow_dispatch: + +jobs: + update-version: + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + steps: + - name: 'Checkout the codebase' + uses: actions/checkout@v3 + + - name: 'Update version' + run: .github/scripts/update-version.sh + env: + GITHUB_ACTOR: ${{ github.actor }} + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} diff --git a/defaults/main.yml b/defaults/main.yml index 6140946..6e0339f 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,5 +1,4 @@ --- -# See available releases: https://confluence.atlassian.com/jirasoftware/jira-software-8-20-x-release-notes-1086411771.html jira_product: 'software' jira_version: '8.20.2' jira_archive_name: 'atlassian-jira-{{ jira_product }}-{{ jira_version }}.tar.gz'