Skip to content

Commit dddd5f0

Browse files
committed
feat: scheduled auto version update
1 parent 7e14f0b commit dddd5f0

2 files changed

Lines changed: 179 additions & 7 deletions

File tree

.github/workflows/create-release.yaml

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ on:
66
- main
77
pull_request:
88
branches: [ "main" ]
9+
workflow_call:
10+
911
jobs:
1012
check-version:
1113
runs-on: ubuntu-latest
@@ -21,10 +23,15 @@ jobs:
2123

2224
- name: Checkout repo
2325
uses: actions/checkout@v5
26+
with:
27+
fetch-depth: 0
2428

2529
- name: Get source version
2630
id: version-step
27-
run: echo "newTag=v$(mvn -f pom.xml help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_OUTPUT
31+
run: |
32+
git fetch origin main
33+
git checkout origin/main
34+
echo "newTag=v$(mvn -f pom-public.xml help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_OUTPUT
2835
2936
- name: Print source version
3037
run: echo ${{ steps.version-step.outputs.newTag }}
@@ -93,15 +100,46 @@ jobs:
93100
id: previousTag
94101
run: echo "previousTag=$(git --no-pager tag --sort=creatordate --merged ${{ github.ref_name }} | grep "^v[0-9]\+\.[0-9]\+\.[0-9]\+$" | tail -1)" >> $GITHUB_OUTPUT
95102

96-
- name: Extract content between titles
103+
- name: Extract changelog content
97104
id: changeLogContent
98105
run: |
99106
FILE_PATH='CHANGELOG.md'
100-
TITLE1="$(sed -n '/^## [0-9]/{p;}' $FILE_PATH | awk 'NR==1' | sed 's/\[/\\[/g; s/\]/\\]/g' | sed 's/^## //')"
101-
TITLE2="$(sed -n '/^## [0-9]/{p;}' $FILE_PATH | awk 'NR==2' | sed 's/\[/\\[/g; s/\]/\\]/g' | sed 's/^## //')"
102-
changes="$(sed -n '/^${TITLE1}/,/^${TITLE2}/{//!p}' $FILE_PATH)"
103-
echo "TITLE1=${TITLE1}" >> $GITHUB_OUTPUT
104-
echo "changes=${changes}" >> $GITHUB_OUTPUT
107+
VERSION="${{ needs.check-version.outputs.release-tag }}"
108+
VERSION_NO_V="${VERSION#v}"
109+
110+
# Look for release title in changelog
111+
TITLE1="$(grep -m1 "^## ${VERSION_NO_V}" "$FILE_PATH" | sed 's/^## //')"
112+
113+
# Generates it not found
114+
if [ -z "$TITLE1" ]; then
115+
DATE="$(date +'%Y-%m-%d')"
116+
TITLE1="${VERSION_NO_V} [${DATE}]"
117+
changes="No changes found in CHANGELOG.md for this version."
118+
else
119+
# Gets next title (previous version changes) to extract
120+
TITLE2=$(grep "^## " "$FILE_PATH" | sed '1d' | head -n1 | sed 's/^## //')
121+
122+
#Extract what's between lines
123+
escape_for_sed() {
124+
echo "$1" | sed -e 's/[][\/.^$*]/\\&/g'
125+
}
126+
127+
ESC_TITLE1=$(escape_for_sed "$TITLE1")
128+
ESC_TITLE2=$(escape_for_sed "$TITLE2")
129+
130+
if [ -z "$TITLE2" ]; then
131+
changes=$(sed -n "/^## $ESC_TITLE1$/,\$p" "$FILE_PATH" | sed '1d')
132+
else
133+
changes=$(sed -n "/^## $ESC_TITLE1$/,/^## $ESC_TITLE2$/p" "$FILE_PATH" | sed '1d;$d')
134+
fi
135+
fi
136+
137+
{
138+
echo "TITLE1=${TITLE1}"
139+
echo "changes<<EOF"
140+
echo "$changes"
141+
echo "EOF"
142+
} >> $GITHUB_OUTPUT
105143
106144
- uses: softprops/action-gh-release@v2
107145
with:
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
name: Scheduled version update
2+
3+
permissions:
4+
contents: write
5+
pull-requests: write
6+
7+
on:
8+
schedule:
9+
- cron: 30 7 * * 4
10+
workflow_dispatch:
11+
12+
jobs:
13+
update-version:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v5
18+
with:
19+
ref: main
20+
fetch-depth: 0
21+
22+
- name: Get last commit SHA
23+
id: current_sha
24+
run: |
25+
CURRENT_SHA=$(git log -n 1 --pretty=format:"%H")
26+
echo "last commit SHA : $CURRENT_SHA"
27+
echo "current_sha=$CURRENT_SHA" >> $GITHUB_OUTPUT
28+
29+
- name: Get last version tag and SHA
30+
id: last_tag
31+
run: |
32+
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
33+
TAG_SHA=$(git rev-list -n 1 "$LAST_TAG")
34+
echo "Last version SHA : $TAG_SHA"
35+
echo "tag_sha=$TAG_SHA" >> $GITHUB_OUTPUT
36+
echo "Last version tag : $LAST_TAG"
37+
echo "last_tag=$LAST_TAG" >> $GITHUB_OUTPUT
38+
39+
- name: Compare SHAs
40+
id: compare
41+
run: |
42+
if [ "${{ steps.current_sha.outputs.current_sha }}" = "${{ steps.last_tag.outputs.tag_sha }}" ]; then
43+
echo "No new commit since version. Exiting."
44+
exit 0
45+
fi
46+
47+
- name: Get version from changelog
48+
id: changelog_version
49+
run: |
50+
FILE_VERSION=$(grep -m1 '^## ' CHANGELOG.md | sed -E 's/^## ([0-9]+\.[0-9]+\.[0-9]+).*/\1/')
51+
echo "Last version in changelog : $FILE_VERSION"
52+
echo "file_version=$FILE_VERSION" >> $GITHUB_OUTPUT
53+
54+
- name: Determine version to use
55+
id: determine_version
56+
run: |
57+
RAW_TAG="${{ steps.last_tag.outputs.last_tag }}"
58+
RAW_TAG="${RAW_TAG#v}"
59+
60+
IFS='.' read -r MAJOR LAST_MINOR LAST_PATCH <<< "$RAW_TAG"
61+
IFS='.' read -r FILE_MAJOR FILE_MINOR FILE_PATCH <<< "${{ steps.changelog_version.outputs.file_version }}"
62+
63+
if [ "$FILE_MAJOR" -gt "$MAJOR" ] || \
64+
[ "$FILE_MAJOR" -eq "$MAJOR" -a "$FILE_MINOR" -gt "$LAST_MINOR" ] || \
65+
[ "$FILE_MAJOR" -eq "$MAJOR" -a "$FILE_MINOR" -eq "$LAST_MINOR" -a "$FILE_PATCH" -gt "$LAST_PATCH" ]; then
66+
NEW_VERSION="${{ steps.changelog_version.outputs.file_version }}"
67+
else
68+
PATCH=$((LAST_PATCH + 1))
69+
NEW_VERSION="$MAJOR.$LAST_MINOR.$PATCH"
70+
fi
71+
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
72+
73+
- name: Update files and commit
74+
run: |
75+
#Update poms
76+
mvn versions:set -DnewVersion="${{ steps.determine_version.outputs.new_version }}" -DprocessAllModules
77+
mvn versions:commit
78+
79+
# Update TODO to today in changelog
80+
TODAY=$(date +%Y-%m-%d)
81+
if grep -q '\[TODO\]' CHANGELOG.md; then
82+
sed -i "0,/\[TODO\]/s/\[TODO\]/[$TODAY]/" CHANGELOG.md
83+
git add CHANGELOG.md
84+
else
85+
echo "No change done in changelog"
86+
fi
87+
88+
git config user.name 'github-actions'
89+
git config user.email 'github-actions@github.com'
90+
git commit -am "chore: update version to ${{ steps.determine_version.outputs.new_version }}" || echo "No changes to commit"
91+
92+
- name: Create Pull Request
93+
id: create_pr
94+
uses: peter-evans/create-pull-request@v7
95+
with:
96+
token: ${{ secrets.GITHUB_TOKEN }}
97+
base: "main"
98+
commit-message: "Update for release ${{ steps.determine_version.outputs.new_version }}"
99+
branch: "update-version-${{ steps.determine_version.outputs.new_version }}"
100+
branch-suffix: timestamp
101+
title: "Auto release ${{ steps.determine_version.outputs.new_version }}"
102+
body: "This PR updates the application to version ${{ steps.determine_version.outputs.new_version }}."
103+
labels: "Version Update - Prod"
104+
105+
- name: Merge pull request
106+
run: gh pr merge --merge --auto "${{ steps.create_pr.outputs.pull-request-number }}"
107+
env:
108+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
109+
110+
wait-for-merge:
111+
runs-on: ubuntu-latest
112+
needs: update-version
113+
steps:
114+
- uses: actions/checkout@v5
115+
- name: Wait for merged version commit
116+
run: |
117+
echo "Fetching main branch..."
118+
git fetch origin main
119+
120+
while true; do
121+
LAST_VERSION_COMMIT=$(git log origin/main --pretty=format:"%H %s" | grep -v 'Merge pull request' | grep 'chore: update version to' | head -n1 | awk '{print $1}')
122+
if [ -n "$LAST_VERSION_COMMIT" ]; then
123+
echo "Found version commit: $LAST_VERSION_COMMIT"
124+
break
125+
else
126+
echo "Version commit not yet merged, sleeping 10s..."
127+
sleep 10
128+
git fetch origin main
129+
fi
130+
done
131+
132+
launch-create-release-workflow:
133+
needs: wait-for-merge
134+
uses: ./.github/workflows/create-release.yaml

0 commit comments

Comments
 (0)