Skip to content

Commit 48b7920

Browse files
committed
Add automated release workflow (#1572)
1 parent ed8eb22 commit 48b7920

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

.github/workflows/release.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: release
2+
3+
on:
4+
push:
5+
tags:
6+
- v*
7+
8+
jobs:
9+
release-notes:
10+
runs-on: ubuntu-latest
11+
12+
if: github.repository_owner == 'visgl'
13+
14+
env:
15+
ADMIN_TOKEN: ${{ secrets.ADMIN_TOKEN }}
16+
17+
steps:
18+
- uses: actions/[email protected]
19+
20+
- name: Get git tags (https://github.com/actions/checkout/issues/206)
21+
run: git fetch --tags -f
22+
23+
- name: Use Node.js
24+
uses: actions/setup-node@v1
25+
with:
26+
node-version: '12.x'
27+
28+
- name: Publish release
29+
run: |
30+
body=$(node scripts/github-release.js) &&
31+
curl \
32+
-X POST \
33+
-H "Accept: application/vnd.github.v3+json" \
34+
https://api.github.com/repos/visgl/react-map-gl/releases \
35+
-d "${body}" \
36+
-H "Authorization: token ${ADMIN_TOKEN}"

scripts/github-release.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const {execSync} = require('child_process');
2+
const {readFileSync} = require('fs');
3+
4+
// Get the latest tag
5+
const tag = getGitTag();
6+
if (!tag) {
7+
console.error('TAG NOT FOUND');
8+
process.exit(1);
9+
}
10+
11+
// Parse changelog
12+
const changelog = getReleaseNotes(tag);
13+
if (!changelog) {
14+
console.error('CHANGELOG NOT FOUND');
15+
process.exit(1);
16+
}
17+
18+
// Publish release notes to GitHub
19+
// https://docs.github.com/en/rest/reference/repos#create-a-release
20+
const requestBody = {
21+
tag_name: tag,
22+
name: tag,
23+
body: changelog,
24+
prerelease: tag.search(/alpha|beta|rc/) > 0
25+
};
26+
27+
console.log(JSON.stringify(requestBody));
28+
29+
function getGitTag() {
30+
try {
31+
return execSync('git describe --exact-match HEAD', {
32+
stdio: [null, 'pipe', null],
33+
encoding: 'utf-8'
34+
}).trim();
35+
} catch (err) {
36+
// not tagged
37+
return null;
38+
}
39+
}
40+
41+
function getReleaseNotes(version) {
42+
let changelog = readFileSync('CHANGELOG.md', 'utf-8');
43+
const header = changelog.match(new RegExp(`^##.*\\b${version.replace('v', '')}\\b.*$`, 'm'));
44+
if (!header) {
45+
return null;
46+
}
47+
changelog = changelog.slice(header.index + header[0].length);
48+
const endIndex = changelog.search(/^#/m);
49+
if (endIndex > 0) {
50+
changelog = changelog.slice(0, endIndex);
51+
}
52+
return changelog.trim();
53+
}

0 commit comments

Comments
 (0)