Skip to content

Commit 76b5184

Browse files
committed
Add GitHub Action to auto-switch default branch on new tags
1 parent afee4ad commit 76b5184

File tree

3 files changed

+330
-0
lines changed

3 files changed

+330
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
name: Auto-switch default branch on new tag
2+
3+
on:
4+
push:
5+
tags:
6+
- '*.*.*' # tags du type a.b.c (1.2.3, 2.0.0, etc.)
7+
8+
jobs:
9+
update-default-branch:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write # nécessaire pour modifier le repo via l'API
13+
14+
steps:
15+
- name: Parse tag and compute target branch
16+
id: vars
17+
run: |
18+
ref="${GITHUB_REF#refs/tags/}" # ex: 1.2.3 ou 1.2.3.
19+
# On enlève un éventuel '.' final (au cas où : 1.2.3. -> 1.2.3)
20+
version="${ref%.}"
21+
22+
echo "Tag ref: $ref"
23+
echo "Version: $version"
24+
25+
IFS='.' read -r major minor patch <<< "$version"
26+
27+
echo "Major: $major"
28+
echo "Minor: $minor"
29+
echo "Patch: $patch"
30+
31+
target_branch="${major}.${minor}.x"
32+
echo "Target branch: $target_branch"
33+
34+
echo "major=$major" >> "$GITHUB_OUTPUT"
35+
echo "minor=$minor" >> "$GITHUB_OUTPUT"
36+
echo "target_branch=$target_branch" >> "$GITHUB_OUTPUT"
37+
38+
- name: Get current default branch
39+
id: repo
40+
env:
41+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42+
run: |
43+
default_branch=$(gh repo view "${GITHUB_REPOSITORY}" --json defaultBranchRef --jq .defaultBranchRef.name)
44+
echo "Current default branch: $default_branch"
45+
echo "default_branch=$default_branch" >> "$GITHUB_OUTPUT"
46+
47+
- name: Check target branch exists
48+
id: exists
49+
env:
50+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
51+
run: |
52+
target="${{ steps.vars.outputs.target_branch }}"
53+
54+
echo "Checking if branch $target exists..."
55+
if gh api "repos/${GITHUB_REPOSITORY}/branches/$target" >/dev/null 2>&1; then
56+
echo "Branch $target exists."
57+
echo "exists=true" >> "$GITHUB_OUTPUT"
58+
else
59+
echo "Branch $target does not exist, skipping."
60+
echo "exists=false" >> "$GITHUB_OUTPUT"
61+
fi
62+
63+
- name: Decide whether to switch
64+
id: decide
65+
run: |
66+
target="${{ steps.vars.outputs.target_branch }}"
67+
current="${{ steps.repo.outputs.default_branch }}"
68+
major="${{ steps.vars.outputs.major }}"
69+
minor="${{ steps.vars.outputs.minor }}"
70+
71+
echo "Current default: $current"
72+
echo "Target branch: $target"
73+
echo "Tag version: $major.$minor"
74+
75+
# Si le target branch n'existe pas -> on ne fait rien
76+
if [ "${{ steps.exists.outputs.exists }}" != "true" ]; then
77+
echo "switch_needed=false" >> "$GITHUB_OUTPUT"
78+
exit 0
79+
fi
80+
81+
# On ne s'intéresse qu'aux default branches de type a.b.x
82+
if [[ "$current" =~ ^([0-9]+)\.([0-9]+)\.x$ ]]; then
83+
current_major="${BASH_REMATCH[1]}"
84+
current_minor="${BASH_REMATCH[2]}"
85+
echo "Current parsed version: $current_major.$current_minor"
86+
else
87+
echo "Current default branch is not of the form a.b.x, skipping."
88+
echo "switch_needed=false" >> "$GITHUB_OUTPUT"
89+
exit 0
90+
fi
91+
92+
# Comparaison (major, minor)
93+
if [ "$major" -gt "$current_major" ] || { [ "$major" -eq "$current_major" ] && [ "$minor" -gt "$current_minor" ]; }; then
94+
echo "New version $major.$minor is higher than $current_major.$current_minor → will switch."
95+
echo "switch_needed=true" >> "$GITHUB_OUTPUT"
96+
else
97+
echo "New version $major.$minor is not higher than $current_major.$current_minor → no switch."
98+
echo "switch_needed=false" >> "$GITHUB_OUTPUT"
99+
fi
100+
101+
- name: Update default branch
102+
if: steps.decide.outputs.switch_needed == 'true'
103+
env:
104+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
105+
run: |
106+
target="${{ steps.vars.outputs.target_branch }}"
107+
echo "Switching default branch to: $target"
108+
109+
gh api -X PATCH "repos/${GITHUB_REPOSITORY}" \
110+
-f default_branch="$target"
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
name: Auto-switch default branch on new tag
2+
3+
on:
4+
push:
5+
tags:
6+
- '*.*.*' # tags du type a.b.c (1.2.3, 2.0.0, etc.)
7+
8+
jobs:
9+
update-default-branch:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write # nécessaire pour modifier le repo via l'API
13+
14+
steps:
15+
- name: Parse tag and compute target branch
16+
id: vars
17+
run: |
18+
ref="${GITHUB_REF#refs/tags/}" # ex: 1.2.3 ou 1.2.3.
19+
# On enlève un éventuel '.' final (au cas où : 1.2.3. -> 1.2.3)
20+
version="${ref%.}"
21+
22+
echo "Tag ref: $ref"
23+
echo "Version: $version"
24+
25+
IFS='.' read -r major minor patch <<< "$version"
26+
27+
echo "Major: $major"
28+
echo "Minor: $minor"
29+
echo "Patch: $patch"
30+
31+
target_branch="${major}.${minor}.x"
32+
echo "Target branch: $target_branch"
33+
34+
echo "major=$major" >> "$GITHUB_OUTPUT"
35+
echo "minor=$minor" >> "$GITHUB_OUTPUT"
36+
echo "target_branch=$target_branch" >> "$GITHUB_OUTPUT"
37+
38+
- name: Get current default branch
39+
id: repo
40+
env:
41+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42+
run: |
43+
default_branch=$(gh repo view "${GITHUB_REPOSITORY}" --json defaultBranchRef --jq .defaultBranchRef.name)
44+
echo "Current default branch: $default_branch"
45+
echo "default_branch=$default_branch" >> "$GITHUB_OUTPUT"
46+
47+
- name: Check target branch exists
48+
id: exists
49+
env:
50+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
51+
run: |
52+
target="${{ steps.vars.outputs.target_branch }}"
53+
54+
echo "Checking if branch $target exists..."
55+
if gh api "repos/${GITHUB_REPOSITORY}/branches/$target" >/dev/null 2>&1; then
56+
echo "Branch $target exists."
57+
echo "exists=true" >> "$GITHUB_OUTPUT"
58+
else
59+
echo "Branch $target does not exist, skipping."
60+
echo "exists=false" >> "$GITHUB_OUTPUT"
61+
fi
62+
63+
- name: Decide whether to switch
64+
id: decide
65+
run: |
66+
target="${{ steps.vars.outputs.target_branch }}"
67+
current="${{ steps.repo.outputs.default_branch }}"
68+
major="${{ steps.vars.outputs.major }}"
69+
minor="${{ steps.vars.outputs.minor }}"
70+
71+
echo "Current default: $current"
72+
echo "Target branch: $target"
73+
echo "Tag version: $major.$minor"
74+
75+
# Si le target branch n'existe pas -> on ne fait rien
76+
if [ "${{ steps.exists.outputs.exists }}" != "true" ]; then
77+
echo "switch_needed=false" >> "$GITHUB_OUTPUT"
78+
exit 0
79+
fi
80+
81+
# On ne s'intéresse qu'aux default branches de type a.b.x
82+
if [[ "$current" =~ ^([0-9]+)\.([0-9]+)\.x$ ]]; then
83+
current_major="${BASH_REMATCH[1]}"
84+
current_minor="${BASH_REMATCH[2]}"
85+
echo "Current parsed version: $current_major.$current_minor"
86+
else
87+
echo "Current default branch is not of the form a.b.x, skipping."
88+
echo "switch_needed=false" >> "$GITHUB_OUTPUT"
89+
exit 0
90+
fi
91+
92+
# Comparaison (major, minor)
93+
if [ "$major" -gt "$current_major" ] || { [ "$major" -eq "$current_major" ] && [ "$minor" -gt "$current_minor" ]; }; then
94+
echo "New version $major.$minor is higher than $current_major.$current_minor → will switch."
95+
echo "switch_needed=true" >> "$GITHUB_OUTPUT"
96+
else
97+
echo "New version $major.$minor is not higher than $current_major.$current_minor → no switch."
98+
echo "switch_needed=false" >> "$GITHUB_OUTPUT"
99+
fi
100+
101+
- name: Update default branch
102+
if: steps.decide.outputs.switch_needed == 'true'
103+
env:
104+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
105+
run: |
106+
target="${{ steps.vars.outputs.target_branch }}"
107+
echo "Switching default branch to: $target"
108+
109+
gh api -X PATCH "repos/${GITHUB_REPOSITORY}" \
110+
-f default_branch="$target"
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
name: Auto-switch default branch on new tag
2+
3+
on:
4+
push:
5+
tags:
6+
- '*.*.*' # tags du type a.b.c (1.2.3, 2.0.0, etc.)
7+
8+
jobs:
9+
update-default-branch:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write # nécessaire pour modifier le repo via l'API
13+
14+
steps:
15+
- name: Parse tag and compute target branch
16+
id: vars
17+
run: |
18+
ref="${GITHUB_REF#refs/tags/}" # ex: 1.2.3 ou 1.2.3.
19+
# On enlève un éventuel '.' final (au cas où : 1.2.3. -> 1.2.3)
20+
version="${ref%.}"
21+
22+
echo "Tag ref: $ref"
23+
echo "Version: $version"
24+
25+
IFS='.' read -r major minor patch <<< "$version"
26+
27+
echo "Major: $major"
28+
echo "Minor: $minor"
29+
echo "Patch: $patch"
30+
31+
target_branch="${major}.${minor}.x"
32+
echo "Target branch: $target_branch"
33+
34+
echo "major=$major" >> "$GITHUB_OUTPUT"
35+
echo "minor=$minor" >> "$GITHUB_OUTPUT"
36+
echo "target_branch=$target_branch" >> "$GITHUB_OUTPUT"
37+
38+
- name: Get current default branch
39+
id: repo
40+
env:
41+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42+
run: |
43+
default_branch=$(gh repo view "${GITHUB_REPOSITORY}" --json defaultBranchRef --jq .defaultBranchRef.name)
44+
echo "Current default branch: $default_branch"
45+
echo "default_branch=$default_branch" >> "$GITHUB_OUTPUT"
46+
47+
- name: Check target branch exists
48+
id: exists
49+
env:
50+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
51+
run: |
52+
target="${{ steps.vars.outputs.target_branch }}"
53+
54+
echo "Checking if branch $target exists..."
55+
if gh api "repos/${GITHUB_REPOSITORY}/branches/$target" >/dev/null 2>&1; then
56+
echo "Branch $target exists."
57+
echo "exists=true" >> "$GITHUB_OUTPUT"
58+
else
59+
echo "Branch $target does not exist, skipping."
60+
echo "exists=false" >> "$GITHUB_OUTPUT"
61+
fi
62+
63+
- name: Decide whether to switch
64+
id: decide
65+
run: |
66+
target="${{ steps.vars.outputs.target_branch }}"
67+
current="${{ steps.repo.outputs.default_branch }}"
68+
major="${{ steps.vars.outputs.major }}"
69+
minor="${{ steps.vars.outputs.minor }}"
70+
71+
echo "Current default: $current"
72+
echo "Target branch: $target"
73+
echo "Tag version: $major.$minor"
74+
75+
# Si le target branch n'existe pas -> on ne fait rien
76+
if [ "${{ steps.exists.outputs.exists }}" != "true" ]; then
77+
echo "switch_needed=false" >> "$GITHUB_OUTPUT"
78+
exit 0
79+
fi
80+
81+
# On ne s'intéresse qu'aux default branches de type a.b.x
82+
if [[ "$current" =~ ^([0-9]+)\.([0-9]+)\.x$ ]]; then
83+
current_major="${BASH_REMATCH[1]}"
84+
current_minor="${BASH_REMATCH[2]}"
85+
echo "Current parsed version: $current_major.$current_minor"
86+
else
87+
echo "Current default branch is not of the form a.b.x, skipping."
88+
echo "switch_needed=false" >> "$GITHUB_OUTPUT"
89+
exit 0
90+
fi
91+
92+
# Comparaison (major, minor)
93+
if [ "$major" -gt "$current_major" ] || { [ "$major" -eq "$current_major" ] && [ "$minor" -gt "$current_minor" ]; }; then
94+
echo "New version $major.$minor is higher than $current_major.$current_minor → will switch."
95+
echo "switch_needed=true" >> "$GITHUB_OUTPUT"
96+
else
97+
echo "New version $major.$minor is not higher than $current_major.$current_minor → no switch."
98+
echo "switch_needed=false" >> "$GITHUB_OUTPUT"
99+
fi
100+
101+
- name: Update default branch
102+
if: steps.decide.outputs.switch_needed == 'true'
103+
env:
104+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
105+
run: |
106+
target="${{ steps.vars.outputs.target_branch }}"
107+
echo "Switching default branch to: $target"
108+
109+
gh api -X PATCH "repos/${GITHUB_REPOSITORY}" \
110+
-f default_branch="$target"

0 commit comments

Comments
 (0)