Skip to content

Commit f1fbd98

Browse files
committed
chore: test full release workflow (refactor + automation)
1 parent 514851b commit f1fbd98

File tree

5 files changed

+98
-75
lines changed

5 files changed

+98
-75
lines changed

.github/workflows/release.yml

Lines changed: 86 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,114 @@
1-
name: Android Release
1+
name: Create Release PR
22

33
on:
44
push:
55
branches:
6-
- master
6+
- test/dry-run # <--- Change this from 'master'
7+
# - master
78

89
permissions:
910
contents: write
10-
issues: write
1111
pull-requests: write
1212

1313
jobs:
14-
release:
15-
name: Release
16-
# Prevent infinite loops: Don't run if the commit is already a release commit
17-
if: "!contains(github.event.head_commit.message, 'chore(release):')"
14+
create-release-pr:
15+
name: Create Release PR
16+
# Skip if the commit is already a release commit to prevent infinite loops
17+
if: github.event_name == 'push' && !contains(github.event.head_commit.message, 'chore(release):')
1818
runs-on: ubuntu-latest
1919
steps:
20-
- name: Checkout Code
20+
- name: Checkout
2121
uses: actions/checkout@v4
2222
with:
23-
fetch-depth: 0 # Important: Fetch all history for semantic-release to analyze commits
23+
fetch-depth: 0
2424
token: ${{ secrets.GITHUB_TOKEN }}
2525

2626
- name: Setup Node.js
2727
uses: actions/setup-node@v4
2828
with:
2929
node-version: 20
3030

31-
- name: Setup Java
32-
uses: actions/setup-java@v3
33-
with:
34-
distribution: 'temurin'
35-
java-version: '17'
31+
# Clean Repo Approach: Install tools globally so we don't need package.json
32+
- name: Install dependencies
33+
run: npm install -g semantic-release @semantic-release/exec @semantic-release/changelog @semantic-release/git conventional-changelog-conventionalcommits
3634

3735
- name: Make Script Executable
3836
run: chmod +x ./scripts/bump_version.sh
3937

40-
- name: Install Release Tools
38+
- name: Configure Git
39+
run: |
40+
git config user.name "semantic-release-bot"
41+
git config user.email "semantic-release-bot@github.com"
42+
43+
- name: Analyze commits and prepare release
44+
id: prepare-release
45+
env:
46+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4147
run: |
42-
npm install -g semantic-release \
43-
@semantic-release/git \
44-
@semantic-release/changelog \
45-
@semantic-release/exec \
46-
@semantic-release/github \
47-
conventional-changelog-conventionalcommits
48-
49-
- name: Run Semantic Release
48+
# 1. Run dry-run to calculate version (without modifying files yet)
49+
npx semantic-release --dry-run --no-ci > dry-run.log 2>&1 || true
50+
51+
# 2. Extract the new version number from the logs
52+
NEW_VERSION=$(grep "The next release version is" dry-run.log | sed -n 's/.*The next release version is \([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*/\1/p' | head -1)
53+
54+
if [ -z "$NEW_VERSION" ]; then
55+
echo "No release needed."
56+
echo "skip=true" >> $GITHUB_OUTPUT
57+
exit 0
58+
fi
59+
60+
RELEASE_BRANCH="release/v${NEW_VERSION}"
61+
62+
# 3. Check if this release branch already exists
63+
if git ls-remote --exit-code --heads origin "$RELEASE_BRANCH" >/dev/null 2>&1; then
64+
echo "Branch $RELEASE_BRANCH already exists."
65+
echo "skip=true" >> $GITHUB_OUTPUT
66+
exit 0
67+
fi
68+
69+
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
70+
echo "release_branch=$RELEASE_BRANCH" >> $GITHUB_OUTPUT
71+
echo "skip=false" >> $GITHUB_OUTPUT
72+
73+
- name: Create release branch and commit changes
74+
if: steps.prepare-release.outputs.skip != 'true'
5075
env:
76+
NEW_VERSION: ${{ steps.prepare-release.outputs.new_version }}
77+
RELEASE_BRANCH: ${{ steps.prepare-release.outputs.release_branch }}
5178
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52-
# We run 'npx semantic-release' which uses the config from .releaserc.json
53-
run: npx semantic-release
79+
run: |
80+
# Create the temporary release branch
81+
git checkout -b "$RELEASE_BRANCH"
82+
83+
# Run semantic-release for real.
84+
# Because of our .releaserc.json, this will:
85+
# 1. Run bump_version.sh (Update Gradle files)
86+
# 2. Generate CHANGELOG.md
87+
npx semantic-release --no-ci
88+
89+
# Manually stage the changed files
90+
# Manually stage the changed files
91+
# We now update gradle.properties instead of build.gradle
92+
git add gradle.properties CHANGELOG.md
93+
# Verify there are changes
94+
if git diff --staged --quiet; then
95+
echo "Error: No changes generated."
96+
exit 1
97+
fi
98+
99+
# Commit and Push
100+
git commit -m "chore(release): $NEW_VERSION [skip ci]"
101+
git push -u origin "$RELEASE_BRANCH"
102+
103+
- name: Create Pull Request
104+
if: steps.prepare-release.outputs.skip != 'true'
105+
env:
106+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
107+
NEW_VERSION: ${{ steps.prepare-release.outputs.new_version }}
108+
RELEASE_BRANCH: ${{ steps.prepare-release.outputs.release_branch }}
109+
run: |
110+
gh pr create \
111+
--base master \
112+
--head "$RELEASE_BRANCH" \
113+
--title "chore(release): $NEW_VERSION" \
114+
--body "Automated release PR for **v$NEW_VERSION**. Merge this PR to trigger the official release (Tag & JitPack)."

.releaserc.json

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
2-
"branches": [
3-
"master"
4-
],
2+
"branches": ["master", "test/dry-run"],
53
"plugins": [
64
[
75
"@semantic-release/commit-analyzer",
@@ -45,26 +43,6 @@
4543
{
4644
"prepareCmd": "./scripts/bump_version.sh ${nextRelease.version}"
4745
}
48-
],
49-
[
50-
"@semantic-release/git",
51-
{
52-
"assets": [
53-
"**/build.gradle",
54-
"CHANGELOG.md",
55-
"package.json",
56-
"package-lock.json"
57-
],
58-
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
59-
}
60-
],
61-
[
62-
"@semantic-release/github",
63-
{
64-
"successComment": false,
65-
"failComment": false,
66-
"releasedLabels": false
67-
}
6846
]
6947
]
7048
}

NRExoPlayerTracker/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
minSdkVersion 16
99
targetSdkVersion 33
1010
versionCode 6
11-
versionName "4.0.3"
11+
versionName GLOBAL_VERSION_NAME
1212
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1313
consumerProguardFiles "consumer-rules.pro"
1414
}

NRIMATracker/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
minSdkVersion 16
99
targetSdkVersion 33
1010
versionCode 6
11-
versionName "4.0.3"
11+
versionName GLOBAL_VERSION_NAME
1212
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1313
consumerProguardFiles "consumer-rules.pro"
1414
}

scripts/bump_version.sh

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,20 @@
11
#!/bin/bash
2-
set -e
32

4-
# Capturing the new version passed by Semantic Release
3+
# Get the new version passed from semantic-release
54
NEW_VERSION=$1
65

76
if [ -z "$NEW_VERSION" ]; then
8-
echo "Error: No version argument supplied."
7+
echo "Error: No version argument provided."
98
exit 1
109
fi
1110

12-
echo "🚀 Bumping Android version to: $NEW_VERSION"
11+
echo "Bumping version to $NEW_VERSION in gradle.properties..."
1312

14-
MODULE_FILES=(
15-
"NewRelicVideoCore/build.gradle"
16-
"NRExoPlayerTracker/build.gradle"
17-
"NRIMATracker/build.gradle"
18-
)
13+
# Use sed to find the line starting with GLOBAL_VERSION_NAME and replace it
14+
# This syntax works on both Linux (GitHub Actions) and Mac (Locally)
15+
sed -i.bak "s/^GLOBAL_VERSION_NAME=.*/GLOBAL_VERSION_NAME=$NEW_VERSION/" gradle.properties
1916

20-
# 3. Loop through each file and update the version
21-
for GRADLE_FILE in "${MODULE_FILES[@]}"; do
17+
# Remove the backup file created by sed
18+
rm -f gradle.properties.bak
2219

23-
if [ -f "$GRADLE_FILE" ]; then
24-
echo " 👉 Updating $GRADLE_FILE..."
25-
26-
sed -i.bak "s/versionName \".*\"/versionName \"$NEW_VERSION\"/" "$GRADLE_FILE"
27-
28-
# Clean up backup file created by sed
29-
rm "${GRADLE_FILE}.bak"
30-
else
31-
echo " ⚠️ Warning: Could not find file $GRADLE_FILE. Skipping."
32-
fi
33-
34-
done
35-
36-
echo "✅ All modules updated to version $NEW_VERSION"
20+
echo "Successfully updated gradle.properties to $NEW_VERSION"

0 commit comments

Comments
 (0)