-
-
Notifications
You must be signed in to change notification settings - Fork 0
113 lines (100 loc) · 4.3 KB
/
untagged.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
name: Automatic Semantic Versioning
# Restrict the workflow to the main branch and ignore tags
on:
workflow_dispatch:
push:
branches:
- main
jobs:
tag_version:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Cache node_modules
id: cache-node-modules
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
- name: Initialize NPM
if: steps.cache-node-modules.outputs.cache-hit != 'true'
run: npm install
- name: Semantic Versioning
id: get_version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Run semantic-release in dry-run mode and capture the next version and changelog notes
node -e "
const fs = require('fs');
const semanticRelease = require('semantic-release');
(async () => {
const result = await semanticRelease({
dryRun: true,
ci: false,
branches: ['main'],
repositoryUrl: 'https://github.com/${{ github.repository }}',
plugins: [
'@semantic-release/commit-analyzer',
'@semantic-release/release-notes-generator',
],
});
console.log('Result:', result);
if (result && result.nextRelease) {
const changelogNotes = JSON.stringify(result.nextRelease.notes);
console.log('Next version:', result.nextRelease.version);
console.log('Changelog:', changelogNotes);
fs.appendFileSync(process.env.GITHUB_OUTPUT, 'nextRelease=' + JSON.stringify(result.nextRelease) + '\n');
} else {
console.log('No release necessary.');
fs.appendFileSync(process.env.GITHUB_OUTPUT, 'new_version=\n');
fs.appendFileSync(process.env.GITHUB_OUTPUT, 'changelog=\n');
}
})();
"
- name: Configure Git
if: ${{ steps.get_version.outputs.nextRelease != '' }}
run: |
git config user.name "${{ github.actor }}"
git config user.email "${{ github.actor }}@users.noreply.github.com"
- name: Create Release Commit
if: ${{ steps.get_version.outputs.nextRelease != '' }}
env:
NEXT_RELEASE: ${{ steps.get_version.outputs.nextRelease }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Extract the next version and changelog notes from the output
NEW_VERSION=$(echo $NEXT_RELEASE | jq -r '.version')
CHANGELOG=$(echo $NEXT_RELEASE | jq -r '.notes')
# Update the Changelog to handle the new lines.
CHANGELOG_NEWLINES=$(echo -e "$CHANGELOG")
# Output everything.
echo "New version: $NEW_VERSION"
echo "Changelog: $CHANGELOG"
echo "Changelog with newlines: $CHANGELOG_NEWLINES"
# Create the commit message
COMMIT_MSG="chore(release): v$NEW_VERSION"
# Create an empty commit with the changelog as the commit body
git commit --allow-empty -m "$COMMIT_MSG" -m "$CHANGELOG_NEWLINES"
# Push the commit back to the branch
git push origin HEAD:${{ github.ref }}
# Create a new tag for the release
git tag -a "v$NEW_VERSION" -m "Release version $NEW_VERSION" -m "$$CHANGELOG_NEWLINES"
git push origin "v$NEW_VERSION"
- name: Create Release Draft
if: ${{ steps.get_version.outputs.nextRelease != '' }}
env:
NEXT_RELEASE: ${{ steps.get_version.outputs.nextRelease }}
GH_TOKEN: ${{ secrets.PAT }}
run: |
# Extract the next version and changelog notes from the output
NEW_VERSION=$(echo $NEXT_RELEASE | jq -r '.version')
CHANGELOG=$(echo $NEXT_RELEASE | jq -r '.notes')
# Update the Changelog to handle the new lines.
CHANGELOG_NEWLINES=$(echo -e "$CHANGELOG")
# Create the release draft
echo "Creating release draft for v$NEW_VERSION"
echo "Changelog: $CHANGELOG"
echo "Changelog with newlines: $CHANGELOG_NEWLINES"
gh release create "v$NEW_VERSION" --title "v$NEW_VERSION" --notes "$CHANGELOG_NEWLINES"