-
Notifications
You must be signed in to change notification settings - Fork 8
180 lines (151 loc) · 8.22 KB
/
release.yml
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
name: "Release Workflow"
# Manual trigger workflow to automatically setup, build and push a new release (or prerelease) on NPM
on:
workflow_dispatch:
inputs:
releaseType:
description: 'Release type'
required: true
default: 'prerelease'
type: choice
options:
- prerelease
- patch
- minor
- major
prereleaseName:
description: 'Prerelease name (ignore if release type is not `prerelease`)'
default: 'alpha'
type: string
concurrency:
group: "${{ github.workflow }}-${{ github.ref_name }}"
cancel-in-progress: true
jobs:
publish_version:
name: "Publish version"
runs-on: ubuntu-latest
steps:
- name: "Checkout repository"
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: "Setup"
uses: ./.github/actions/setup
- name: "Create version"
id: version
uses: actions/github-script@v6
with:
script: |
const refType = '${{ github.ref_type }}';
const refName = '${{ github.ref_name }}';
if (refType !== 'branch') {
console.log(`Release workflow can only be run from a branch.\n`);
process.exit(1);
}
const run = require('util').promisify(require('child_process').exec);
const lodash = require('lodash');
const semver = require('semver');
const releaseType = '${{ inputs.releaseType }}' || 'prerelease';
console.log(`Release type: ${releaseType}`);
const prereleaseName = lodash.kebabCase('${{ inputs.prereleaseName }}' || 'alpha');
const getVersion = (tag) => run(`npm view @lumx/core@${tag} version`).then(({ stdout }) => stdout.trim());
const npmLatestVersion = await getVersion('latest');
if (releaseType !== 'prerelease') {
// Exit if not on master
if (refName !== 'master') {
console.log(`New ${releaseType} release can only be created from master.\n`);
process.exit(1);
}
const localVersion = require('./lerna.json').version;
if (localVersion !== npmLatestVersion) {
console.log(`NPM latest version (${npmLatestVersion}) does not match the local latest version (${localVersion}).\n`);
process.exit(1);
}
// Check that the changelog has unreleased changes.
try {
await run('yarn changelog-verify --unreleased');
} catch(err) {
console.log(err);
process.exit(1);
}
} else {
console.log(`Prerelease name: ${prereleaseName}`);
}
// Get NPM dist tag
const distTag = releaseType === 'prerelease' ? prereleaseName : 'latest';
console.log(`NPM dist tag: ${distTag}`);
// Get base version (version from which to increment)
let baseVersion = await getVersion(distTag)
// Prerelease should start with latest NPM version + patch (ex: 'v1.0.0' => 'v1.0.1-alpha.0')
if (releaseType === 'prerelease' && (!baseVersion || !baseVersion.startsWith(semver.inc(npmLatestVersion, 'patch')))) {
baseVersion = npmLatestVersion;
}
console.log(`${releaseType} base version: ${baseVersion}`);
// Increment version
const nextVersion = semver.inc(baseVersion, releaseType, prereleaseName);
console.log(`New ${releaseType} version: ${nextVersion}`);
// Checkout new branch (lerna version requires this)
const branch = `release/${nextVersion}`;
await run(`git checkout -b ${branch}`);
// Update version in all packages
await run(`yarn lerna version --no-git-tag-version --yes ${nextVersion}`);
// Update yarn.lock with new package versions
await run(`YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn install`);
// Update version in changelog (need to run in a package which has a version, so not the root package)
await run(`yarn workspace @lumx/core update-version-changelog`);
return { nextVersion, distTag, branch, releaseType, refName };
- name: "Build libs"
run: yarn build:libs
- name: "Publish version to NPM"
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
DIST_TAG: ${{ fromJSON(steps.version.outputs.result).distTag }}
run: |
for package in $(echo packages/lumx-*);
do
(echo "Publishing $package"; cd $package/dist; npm publish --tag $DIST_TAG)
done
- name: "Git commit, tag & push"
if: ${{ fromJSON(steps.version.outputs.result).releaseType != 'prerelease' }}
id: git
run: |
git config --global user.name "github-actions"
git config --global user.email "github-actions@users.noreply.github.com"
branch="${{ fromJSON(steps.version.outputs.result).branch }}"
echo "branch=$branch" >> $GITHUB_OUTPUT
tag="v${{ fromJSON(steps.version.outputs.result).nextVersion }}"
echo "tag=$tag" >> $GITHUB_OUTPUT
# Commit and push branch
commit="chore(release): release $tag"
echo "commit=$commit" >> $GITHUB_OUTPUT
git commit -am "$commit"
git push origin "$branch"
# TODO: need to a new git user to push tags
# Tag and push tag
#git tag "$tag"
#git push origin "$tag"
outputs:
releaseType: ${{ fromJSON(steps.version.outputs.result).releaseType }}
baseRef: ${{ fromJSON(steps.version.outputs.result).refName }}
commit: ${{ steps.git.outputs.commit }}
branch: ${{ steps.git.outputs.branch }}
versionTag: ${{ steps.git.outputs.tag }}
create_gh_pull_request:
if: ${{ needs.publish_version.outputs.releaseType != 'prerelease' }}
name: "Create pull request"
runs-on: ubuntu-latest
needs: [ publish_version ]
steps:
- name: "Create pull request"
uses: actions/github-script@v6
with:
script: |
github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: '${{ needs.publish_version.outputs.commit }}',
head: '${{ needs.publish_version.outputs.branch }}',
base: '${{ needs.publish_version.outputs.baseRef }}',
body: 'https://github.com/lumapps/design-system/releases/tag/${{ needs.publish_version.outputs.versionTag }}\nTODO: `git tag ${{ needs.publish_version.outputs.versionTag }}` & `git push origin ${{ needs.publish_version.outputs.versionTag }}`',
draft: false,
});