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
0 commit comments