1+ name : Create Release on Version Change
2+
3+ on :
4+ push :
5+ branches :
6+ - main
7+ paths :
8+ - ' package.json'
9+
10+ jobs :
11+ check-version-and-release :
12+ runs-on : ubuntu-latest
13+ permissions :
14+ contents : write
15+ pull-requests : read
16+ steps :
17+ - name : Checkout repository
18+ uses : actions/checkout@v4
19+ with :
20+ fetch-depth : 0
21+
22+ - name : Setup Node.js
23+ uses : actions/setup-node@v4
24+ with :
25+ node-version : ' 18'
26+
27+ - name : Get current version from package.json
28+ id : current_version
29+ run : |
30+ VERSION=$(jq -r '.version' package.json)
31+ echo "version=$VERSION" >> $GITHUB_OUTPUT
32+ echo "Current version: $VERSION"
33+
34+ - name : Check if tag already exists
35+ id : check_tag
36+ run : |
37+ VERSION="${{ steps.current_version.outputs.version }}"
38+ if git rev-parse "v$VERSION" >/dev/null 2>&1; then
39+ echo "Tag v$VERSION already exists"
40+ echo "tag_exists=true" >> $GITHUB_OUTPUT
41+ else
42+ echo "Tag v$VERSION does not exist"
43+ echo "tag_exists=false" >> $GITHUB_OUTPUT
44+ fi
45+
46+ - name : Get previous version tag
47+ id : previous_version
48+ if : steps.check_tag.outputs.tag_exists == 'false'
49+ run : |
50+ # Get the most recent tag
51+ PREVIOUS_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
52+ if [ -z "$PREVIOUS_TAG" ]; then
53+ # If no previous tag, get first commit
54+ PREVIOUS_REF=$(git rev-list --max-parents=0 HEAD)
55+ echo "previous_tag=" >> $GITHUB_OUTPUT
56+ echo "previous_ref=$PREVIOUS_REF" >> $GITHUB_OUTPUT
57+ echo "No previous tag found, using first commit: $PREVIOUS_REF"
58+ else
59+ echo "previous_tag=$PREVIOUS_TAG" >> $GITHUB_OUTPUT
60+ echo "previous_ref=$PREVIOUS_TAG" >> $GITHUB_OUTPUT
61+ echo "Previous tag: $PREVIOUS_TAG"
62+ fi
63+
64+ - name : Get commits since last tag
65+ id : commits
66+ if : steps.check_tag.outputs.tag_exists == 'false'
67+ run : |
68+ PREVIOUS_REF="${{ steps.previous_version.outputs.previous_ref }}"
69+
70+ if [ -z "$PREVIOUS_REF" ]; then
71+ PREVIOUS_REF="HEAD~10"
72+ fi
73+
74+ # Get commit messages since last tag
75+ COMMITS=$(git log ${PREVIOUS_REF}..HEAD --pretty=format:"- %s (%h)" --no-merges)
76+
77+ # Save to file to handle multiline
78+ echo "$COMMITS" > commits.txt
79+
80+ echo "Commits since last release:"
81+ cat commits.txt
82+
83+ - name : Extract PR numbers and get PR details
84+ id : pr_details
85+ if : steps.check_tag.outputs.tag_exists == 'false'
86+ run : |
87+ PREVIOUS_REF="${{ steps.previous_version.outputs.previous_ref }}"
88+
89+ if [ -z "$PREVIOUS_REF" ]; then
90+ PREVIOUS_REF="HEAD~10"
91+ fi
92+
93+ # Get merge commits with PR numbers
94+ MERGE_COMMITS=$(git log ${PREVIOUS_REF}..HEAD --merges --pretty=format:"%s" | grep -oP '#\K[0-9]+' || echo "")
95+
96+ PR_DETAILS=""
97+
98+ if [ -n "$MERGE_COMMITS" ]; then
99+ echo "Found PR numbers: $MERGE_COMMITS"
100+
101+ for PR_NUM in $MERGE_COMMITS; do
102+ echo "Fetching details for PR #$PR_NUM"
103+
104+ # Get PR details using GitHub API
105+ PR_INFO=$(gh pr view $PR_NUM --json title,author,url,body 2>/dev/null || echo "")
106+
107+ if [ -n "$PR_INFO" ]; then
108+ PR_TITLE=$(echo "$PR_INFO" | jq -r '.title')
109+ PR_AUTHOR=$(echo "$PR_INFO" | jq -r '.author.login')
110+ PR_URL=$(echo "$PR_INFO" | jq -r '.url')
111+
112+ PR_DETAILS="${PR_DETAILS}\n- **PR #${PR_NUM}**: ${PR_TITLE} by @${PR_AUTHOR} - [View PR](${PR_URL})"
113+ else
114+ PR_DETAILS="${PR_DETAILS}\n- **PR #${PR_NUM}**"
115+ fi
116+ done
117+ fi
118+
119+ # Save PR details to file
120+ echo -e "$PR_DETAILS" > pr_details.txt
121+
122+ echo "PR Details:"
123+ cat pr_details.txt
124+ env :
125+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
126+
127+ - name : Get version changes from CHANGELOG
128+ id : changelog
129+ if : steps.check_tag.outputs.tag_exists == 'false'
130+ run : |
131+ VERSION="${{ steps.current_version.outputs.version }}"
132+
133+ # Extract the section for current version from CHANGELOG.md
134+ CHANGELOG_SECTION=$(awk "/^## $VERSION\$/,/^## [0-9]/" CHANGELOG.md | sed '$d' | tail -n +2)
135+
136+ if [ -z "$CHANGELOG_SECTION" ]; then
137+ echo "No changelog entry found for version $VERSION"
138+ CHANGELOG_SECTION="No changelog entry found."
139+ fi
140+
141+ # Save to file
142+ echo "$CHANGELOG_SECTION" > changelog_section.txt
143+
144+ echo "Changelog for $VERSION:"
145+ cat changelog_section.txt
146+
147+ - name : Generate release notes
148+ id : release_notes
149+ if : steps.check_tag.outputs.tag_exists == 'false'
150+ run : |
151+ VERSION="${{ steps.current_version.outputs.version }}"
152+ PREVIOUS_TAG="${{ steps.previous_version.outputs.previous_tag }}"
153+
154+ # Start building release notes
155+ cat > release_notes.md <<'EOF'
156+ # Release v$VERSION
157+
158+ EOF
159+
160+ # Add changelog section
161+ echo "## What's Changed" >> release_notes.md
162+ echo "" >> release_notes.md
163+
164+ if [ -f changelog_section.txt ]; then
165+ cat changelog_section.txt >> release_notes.md
166+ fi
167+
168+ echo "" >> release_notes.md
169+
170+ # Add PR details if available
171+ if [ -f pr_details.txt ] && [ -s pr_details.txt ]; then
172+ echo "## Pull Requests Merged" >> release_notes.md
173+ echo "" >> release_notes.md
174+ cat pr_details.txt >> release_notes.md
175+ echo "" >> release_notes.md
176+ fi
177+
178+ # Add commit history
179+ echo "## Commits" >> release_notes.md
180+ echo "" >> release_notes.md
181+
182+ if [ -f commits.txt ]; then
183+ cat commits.txt >> release_notes.md
184+ fi
185+
186+ echo "" >> release_notes.md
187+
188+ # Add footer
189+ cat >> release_notes.md <<'EOF'
190+
191+ ---
192+
193+ ## Installation
194+
195+ To install or update to this version:
196+
197+ ### Via Unity Package Manager
198+
199+ 1. In Unity, go to **Window > Package Manager**
200+ 2. Click the **+** button and select **Add package from git URL**
201+ 3. Enter: `https://github.com/newrelic/newrelic-unity-agent.git#v$VERSION`
202+
203+ ### Requirements
204+
205+ - **Unity**: 2019.1 or later
206+ - **Android**: API 24+ (AGP 7 and Higher)
207+ - **iOS**: iOS 10 or later
208+
209+ ## Documentation
210+
211+ - [Installation Guide](https://github.com/newrelic/newrelic-unity-agent#installation)
212+ - [Usage Documentation](https://github.com/newrelic/newrelic-unity-agent#usage)
213+ - [New Relic Unity Docs](https://docs.newrelic.com/docs/mobile-monitoring/new-relic-mobile-unity/monitor-your-unity-application/)
214+
215+ ---
216+
217+ **Full Changelog**: https://github.com/${{ github.repository }}/compare/$PREVIOUS_TAG...v$VERSION
218+ EOF
219+
220+ # Replace $VERSION placeholder
221+ sed -i "s/\$VERSION/$VERSION/g" release_notes.md
222+
223+ # Replace $PREVIOUS_TAG placeholder
224+ if [ -n "$PREVIOUS_TAG" ]; then
225+ sed -i "s|\$PREVIOUS_TAG|$PREVIOUS_TAG|g" release_notes.md
226+ else
227+ # Remove the Full Changelog line if no previous tag
228+ sed -i '/Full Changelog:/d' release_notes.md
229+ fi
230+
231+ echo "Generated release notes:"
232+ cat release_notes.md
233+
234+ - name : Create Git Tag
235+ id : create_tag
236+ if : steps.check_tag.outputs.tag_exists == 'false'
237+ run : |
238+ VERSION="${{ steps.current_version.outputs.version }}"
239+
240+ git config user.name "$GITHUB_ACTOR"
241+ git config user.email "gh-actions-${GITHUB_ACTOR}@github.com"
242+
243+ # Create annotated tag
244+ git tag -a "v$VERSION" -m "Release version $VERSION"
245+
246+ # Push tag to remote
247+ git push origin "v$VERSION"
248+
249+ echo "Created and pushed tag v$VERSION"
250+
251+ - name : Create GitHub Release
252+ if : steps.check_tag.outputs.tag_exists == 'false'
253+ run : |
254+ VERSION="${{ steps.current_version.outputs.version }}"
255+
256+ # Create release using gh CLI
257+ gh release create "v$VERSION" \
258+ --title "Release v$VERSION" \
259+ --notes-file release_notes.md \
260+ --target main
261+
262+ echo "Created GitHub release for v$VERSION"
263+ env :
264+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
265+
266+ - name : Summary
267+ if : steps.check_tag.outputs.tag_exists == 'false'
268+ run : |
269+ VERSION="${{ steps.current_version.outputs.version }}"
270+
271+ echo "## Release Created Successfully! 🎉" >> $GITHUB_STEP_SUMMARY
272+ echo "" >> $GITHUB_STEP_SUMMARY
273+ echo "**Version**: v$VERSION" >> $GITHUB_STEP_SUMMARY
274+ echo "" >> $GITHUB_STEP_SUMMARY
275+ echo "**Tag**: \`v$VERSION\`" >> $GITHUB_STEP_SUMMARY
276+ echo "" >> $GITHUB_STEP_SUMMARY
277+ echo "**Release URL**: https://github.com/${{ github.repository }}/releases/tag/v$VERSION" >> $GITHUB_STEP_SUMMARY
278+ echo "" >> $GITHUB_STEP_SUMMARY
279+ echo "### Release Notes Preview" >> $GITHUB_STEP_SUMMARY
280+ echo "" >> $GITHUB_STEP_SUMMARY
281+ cat release_notes.md >> $GITHUB_STEP_SUMMARY
282+
283+ - name : Tag Already Exists
284+ if : steps.check_tag.outputs.tag_exists == 'true'
285+ run : |
286+ VERSION="${{ steps.current_version.outputs.version }}"
287+
288+ echo "## Tag Already Exists ℹ️" >> $GITHUB_STEP_SUMMARY
289+ echo "" >> $GITHUB_STEP_SUMMARY
290+ echo "Tag \`v$VERSION\` already exists. No new release created." >> $GITHUB_STEP_SUMMARY
291+ echo "" >> $GITHUB_STEP_SUMMARY
292+ echo "If you need to update the release, please:" >> $GITHUB_STEP_SUMMARY
293+ echo "1. Delete the existing tag and release" >> $GITHUB_STEP_SUMMARY
294+ echo "2. Re-run this workflow" >> $GITHUB_STEP_SUMMARY
0 commit comments