-
Notifications
You must be signed in to change notification settings - Fork 0
184 lines (153 loc) · 7.12 KB
/
release-pr.yml
File metadata and controls
184 lines (153 loc) · 7.12 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
name: Release Process
on:
workflow_dispatch:
inputs:
type:
type: choice
description: Choose release type
options:
- patch
- minor
- major
default: patch
beta:
type: boolean
description: Prerelease
default: false
pull_request:
types: [closed]
branches:
- main
permissions:
contents: write
pull-requests: write
issues: write
jobs:
# This job prepares a release PR
prepare-release:
if: github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: git config
run: |
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
# Prepare the version change and update files
- name: Prepare release changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TYPE_ARG: ${{ inputs.type }}
BETA_ARG: ${{ inputs.beta == true && '--preRelease=beta' || '' }}
run: |
# First, get the current version
CURRENT_VERSION=$(node -p "require('./package.json').version")
# Run release-it to get the new version information, but don't make changes
NEW_VERSION_INFO=$(npm run release -- $TYPE_ARG --ci --verbose --no-git.push --no-git.commit --no-git.tag --no-github --no-npm $BETA_ARG --dry-run)
# Extract the new version from the output
NEW_VERSION=$(echo "$NEW_VERSION_INFO" | grep -o "Let's release docker-to-iac ([^)]*)" | sed 's/.*\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/')
echo "Current version: $CURRENT_VERSION"
echo "New version: $NEW_VERSION"
# Update package.json with the new version
sed -i "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" package.json
# Extract changelog data from the dry run output
CHANGELOG=$(echo "$NEW_VERSION_INFO" | sed -n '/^Changelog:/,/^\$ npm run build/p' | grep -v '^\$ npm run build' | grep -v '^\$ ' | grep -v '^!' | grep -v 'Done')
CHANGELOG=$(echo "$CHANGELOG" | sed '1d')
# First, get all the dependabot commits since the last tag
DEPENDABOT_COMMITS=$(git log v$CURRENT_VERSION..HEAD --author="dependabot" --format="* %s [%h]")
if [ ! -z "$DEPENDABOT_COMMITS" ]; then
# Format dependabot commits for the changelog
DEPENDABOT_SECTION="### Chores\n\n$DEPENDABOT_COMMITS\n\n"
# Add the dependabot section to the changelog
CHANGELOG=$(echo "$CHANGELOG" | awk -v deps="$DEPENDABOT_SECTION" '{print} /\(.*\)/ && !printed {print deps; printed=1}')
fi
# Get the existing changelog content
EXISTING_CHANGELOG=$(cat CHANGELOG.md)
# Check if changelog already has the new version header
if ! grep -q "## \[$NEW_VERSION\]" CHANGELOG.md; then
# Insert the new changelog after the header
echo -e "# Changelog\n\n$CHANGELOG\n$(echo "$EXISTING_CHANGELOG" | tail -n +2)" > CHANGELOG.md
fi
# Build the project after changes
npm run build
# Get the version from package.json (which should now be updated)
- name: Get version
id: package-version
uses: martinbeentjes/npm-get-version-action@main
# Create a branch with the changes and push it
- name: Create and push branch
run: |
# Get the new version that was prepared
NEW_VERSION=$(node -p "require('./package.json').version")
echo "New version is: $NEW_VERSION"
# Create a new branch using the NEW_VERSION
BRANCH_NAME="release-v$NEW_VERSION"
git checkout -b $BRANCH_NAME
# Add the changes
git add .
git commit -m "chore(release): prepare release v$NEW_VERSION"
git push origin $BRANCH_NAME
# Output the PR creation URL to the logs
echo "::notice::✅ Branch created and pushed: $BRANCH_NAME"
echo "::notice::⚠️ You must manually create a PR from this branch at this URL:"
echo "::notice::➡️ https://github.com/${{ github.repository }}/compare/main...$BRANCH_NAME?expand=1"
echo "::notice::📝 Important: Add the 'release' label to your PR for the release workflow to run when merged"
# Also output to step summary for better visibility
echo "# Release Branch Created" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "The release branch has been created and pushed as: \`$BRANCH_NAME\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Next Steps" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "1. [Create a pull request](https://github.com/${{ github.repository }}/compare/main...$BRANCH_NAME?expand=1) from this branch" >> $GITHUB_STEP_SUMMARY
echo "2. Add the 'release' label to your PR" >> $GITHUB_STEP_SUMMARY
echo "3. Get the PR reviewed and merged" >> $GITHUB_STEP_SUMMARY
echo "4. Once merged, the release will automatically be published" >> $GITHUB_STEP_SUMMARY
# This job performs the actual release after a PR has been merged
publish-release:
if: github.event_name == 'pull_request' && github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'release')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: git config
run: |
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
registry-url: 'https://registry.npmjs.org'
- run: npm ci
# Create GitHub release tag
- name: Create GitHub release tag
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Create a tag for the version
VERSION=$(node -p "require('./package.json').version")
git tag -a "v$VERSION" -m "Release v$VERSION"
git push origin "v$VERSION"
# Extract changelog entries for this version
CHANGELOG_ENTRY=$(sed -n "/## \[$VERSION\]/,/## \[/p" CHANGELOG.md | sed '1p;/## \[/d')
# Create GitHub release
gh release create "v$VERSION" --title "v$VERSION" --notes "$CHANGELOG_ENTRY"
- name: Build project
run: npm run build
- name: Publish to npm
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
# Publish the package to npm
npm publish --access public