Skip to content

Commit ebbb308

Browse files
authored
Merge pull request #429 from stackhpc/feat/automate-release-upgrades
Automated PRs for version bumps
2 parents fd6abef + ce1ae98 commit ebbb308

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

.github/bin/create-merge-branch.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env bash
2+
3+
#####
4+
# This script creates a branch that merges the latest release
5+
#####
6+
7+
set -ex
8+
9+
# Only allow running on main
10+
CURRENT_BRANCH="$(git branch --show-current)"
11+
if [ "$CURRENT_BRANCH" != "main" ]; then
12+
echo "[ERROR] This script can only be run on the main branch" >&2
13+
exit 1
14+
fi
15+
16+
if [ -n "$(git status --short)" ]; then
17+
echo "[ERROR] This script cannot run with uncommitted changes" >&2
18+
exit 1
19+
fi
20+
21+
UPSTREAM_REPO="${UPSTREAM_REPO:-"stackhpc/ansible-slurm-appliance"}"
22+
echo "[INFO] Using upstream repo - $UPSTREAM_REPO"
23+
24+
# Fetch the tag for the latest release from the upstream repository
25+
RELEASE_TAG="$(curl -fsSL "https://api.github.com/repos/${UPSTREAM_REPO}/releases/latest" | jq -r '.tag_name')"
26+
echo "[INFO] Found latest release tag - $RELEASE_TAG"
27+
28+
# Add the repository as an upstream
29+
echo "[INFO] Adding upstream remote..."
30+
git remote add upstream "https://github.com/${UPSTREAM_REPO}.git"
31+
git remote show upstream
32+
33+
echo "[INFO] Fetching remote tags..."
34+
git remote update
35+
36+
# Use a branch that is named for the release
37+
BRANCH_NAME="upgrade/$RELEASE_TAG"
38+
39+
# Check if the branch already exists on the origin
40+
# If it does, there is nothing more to do as the branch can be rebased from the MR
41+
if git show-branch "remotes/origin/$BRANCH_NAME" >/dev/null 2>&1; then
42+
echo "[INFO] Merge branch already created for $RELEASE_TAG"
43+
exit
44+
fi
45+
46+
echo "[INFO] Merging release tag - $RELEASE_TAG"
47+
git merge --strategy recursive -X theirs --no-commit $RELEASE_TAG
48+
49+
# Check if the merge resulted in any changes being staged
50+
if [ -n "$(git status --short)" ]; then
51+
echo "[INFO] Merge resulted in the following changes"
52+
git status
53+
54+
# NOTE(scott): The GitHub create-pull-request action does
55+
# the commiting for us, so we only need to make branches
56+
# and commits if running outside of GitHub actions.
57+
if [ ! $GITHUB_ACTIONS ]; then
58+
echo "[INFO] Checking out temporary branch '$BRANCH_NAME'..."
59+
git checkout -b "$BRANCH_NAME"
60+
61+
echo "[INFO] Committing changes"
62+
git commit -m "Upgrade ansible-slurm-applaince to $RELEASE_TAG"
63+
64+
echo "[INFO] Pushing changes to origin"
65+
git push --set-upstream origin "$BRANCH_NAME"
66+
67+
# Go back to the main branch at the end
68+
echo "[INFO] Reverting back to main"
69+
git checkout main
70+
71+
echo "[INFO] Removing temporary branch"
72+
git branch -d "$BRANCH_NAME"
73+
fi
74+
75+
# Write a file containing the branch name and tag
76+
# for automatic PR or MR creation that follows
77+
echo "BRANCH_NAME=\"$BRANCH_NAME\"" > .mergeenv
78+
echo "RELEASE_TAG=\"$RELEASE_TAG\"" >> .mergeenv
79+
else
80+
echo "[INFO] Merge resulted in no changes"
81+
fi
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# This workflow compares a downstream ansible-slurm-appliance repository for a specific site with the upstream
2+
# stackhpc/ansible-slurm-appliance repository to check whether there is a new upstream version available. If a
3+
# newer tag is found in the upstream repository then a pull request is created to the downstream repo
4+
# in order to merge in the changes from the new upstream release.
5+
#
6+
# To use this workflow in a downstream ansible-slurm-appliance repository simply copy it into .github/workflows
7+
# and give it an appropriate name, e.g.
8+
# cp .github/workflows/upgrade-check.yml.sample .github/workflows/upgrade-check.yml
9+
#
10+
# Workflow uses https://github.com/peter-evans/create-pull-request to handle the pull request action.
11+
# See the docs for action inputs.
12+
#
13+
# In order for GitHub actions to create pull requests that make changes to workflows in `.github/workflows`,
14+
# a token for each deployment must be provided. Both user PAT and fine-grained tokens should work, but it was tested
15+
# with a PAT. Fine-grained repo-scoped token is preferred if possible but requires organisation admin privileges.
16+
#
17+
# See https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
18+
# for security considerations around tokens. TREAT YOUR ACCESS TOKENS LIKE PASSWORDS.
19+
#
20+
# The following repository permissions must be set for the PAT:
21+
# - `Workflows: Read and write`
22+
# - `Actions: Read and write`
23+
# - `Pull requests: Read and write`
24+
# The PAT should then be copied into an Actions repository secret in the downstream repo with the title `WORKFLOW_TOKEN`.
25+
26+
name: Check for upstream updates
27+
on:
28+
schedule:
29+
- cron: "0 9 * * *"
30+
workflow_dispatch:
31+
jobs:
32+
check_for_update:
33+
runs-on: ubuntu-22.04
34+
35+
steps:
36+
- name: Checkout the config repo
37+
uses: actions/checkout@v4
38+
with:
39+
fetch-depth: 0
40+
fetch-tags: true
41+
42+
# Based on equivalent azimuth-config job
43+
- name: Check for new release
44+
shell: bash
45+
run: |
46+
set -xe
47+
48+
# Tell git who we are for commits
49+
git config user.email "${{ github.actor }}[email protected]"
50+
git config user.name "${{ github.actor }} CI"
51+
52+
# Create the merge branch and write vars to .mergeenv file
53+
.github/bin/create-merge-branch.sh
54+
55+
- name: Set release tag output
56+
id: release_tag
57+
if: ${{ hashFiles('.mergeenv') }}
58+
run: source .mergeenv && echo value=$RELEASE_TAG >> $GITHUB_OUTPUT
59+
60+
- name: Set branch name output
61+
id: branch_name
62+
if: ${{ hashFiles('.mergeenv') }}
63+
run: source .mergeenv && echo value=$BRANCH_NAME >> $GITHUB_OUTPUT
64+
65+
- name: Remove tmp file
66+
run: rm .mergeenv
67+
if: ${{ hashFiles('.mergeenv') }}
68+
69+
- name: Create Pull Request
70+
if: ${{ steps.release_tag.outputs.value }}
71+
uses: peter-evans/create-pull-request@v6
72+
with:
73+
base: main
74+
branch: ${{ steps.branch_name.outputs.value }}
75+
title: "Upgrade ansible-slurm-appliance to ${{ steps.release_tag.outputs.value }}"
76+
body: This PR was automatically generated by GitHub Actions.
77+
commit-message: "Upgrade ansible-slurm-appliance to ${{ steps.release_tag.outputs.value }}"
78+
delete-branch: true
79+
token: ${{ secrets.WORKFLOW_TOKEN }}

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,9 @@ Please contact us for specific advice, but in outline this generally involves:
144144
## Monitoring and logging
145145

146146
Please see the [monitoring-and-logging.README.md](docs/monitoring-and-logging.README.md) for details.
147+
148+
## CI/CD automation
149+
150+
The `.github` directory contains a set of sample workflows which can be used by downstream site-specific configuration repositories to simplify ongoing maintainence tasks. These include:
151+
152+
- An [upgrade check](.github/workflows/upgrade-check.yml.sample) workflow which automatically checks this upstream stackhpc/ansible-slurm-appliance repo for new releases and proposes a pull request to the downstream site-specific repo when a new release is published.

0 commit comments

Comments
 (0)