106 lines (96 loc) · 4.11 KB
/
publish.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
name: Publish a New Release
on:
pull_request:
branches: [main]
types: [closed]
jobs:
publish_to_npm:
name: Publish to NPM and GitHub
runs-on: ubuntu-latest
steps:
# Determine if PR was a valid merged release PR. If so, publish the new release. Otherwise, do nothing.
# This is a valid release PR if the following 3 things are true:
# 1. The head (source) branch of the PR matches the expected "release/x.y.z" pattern
# 2. The PR has been merged, rather than closed
# 3. The head (source) repo of the PR is not a fork, or in other words the PR is made from a branch within the main repo
- name: Validate Release PR
id: validate_pr
run: |
merged=${{ github.event.pull_request.merged }}
forked=${{ github.event.pull_request.head.repo.fork }}
echo "Source branch is ${{ github.head_ref }}; merged = $merged; from a fork = $forked"
if [ $(grep -E '^release/[0-9]+\.[0-9]+\..+$' <<< '${{ github.head_ref }}') ] && [ $merged = true ] && [ $forked = false ]
then
echo "::set-output name=is_release::true"
fi
# Get release version and draft release tag from PR metadata
- name: Get Release Metadata
if: steps.validate_pr.outputs.is_release
id: metadata
run: |
version=$(cut -d'/' -f2 <<< '${{ github.head_ref }}')
echo "::set-output name=version::$version"
- name: Checkout Repository
if: steps.validate_pr.outputs.is_release
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Node
if: steps.validate_pr.outputs.is_release
uses: actions/setup-node@v2
with:
node-version: '14.x'
registry-url: 'https://registry.npmjs.org'
- name: Cache Dependencies
if: steps.validate_pr.outputs.is_release
uses: actions/cache@v2
with:
path: |
node_modules
package-lock.json
detectors/node/*/node_modules
metapackages/*/node_modules
packages/*/node_modules
plugins/node/*/node_modules
plugins/web/*/node_modules
propagators/*/node_modules
key: ${{ runner.os }}-${{ matrix.container }}-${{ hashFiles('**/package.json') }}
- name: Build Packages
if: steps.validate_pr.outputs.is_release
run: |
npm install
npx lerna bootstrap --no-ci
- name: Publish to NPM
if: steps.validate_pr.outputs.is_release
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx lerna publish ${{ steps.metadata.outputs.version }} --no-push --no-git-tag-version --no-verify-access --yes
# Parses the CHANGELOG.md file for the latest version's entry, to be used in GitHub release body
# Assumes the CHANGELOG's first H2 entry is "Unreleased", followed by version entries
- name: Get Release Body
if: steps.validate_pr.outputs.is_release
id: get-changelog
run: |
echo "Getting changelog entry for version ${{ steps.metadata.outputs.version }}"
startline=$(sed -n "/## ${{ steps.metadata.outputs.version }}/=" CHANGELOG.md)
startline=$((startline+2))
endline=$(sed -n '/^## /=' CHANGELOG.md | head -n 3 | tail -n 1)
endline=$((endline-2))
description=$(sed -n "$startline,$endline p" CHANGELOG.md)
description="${description//'%'/'%25'}"
description="${description//$'\n'/'%0A'}"
description="${description//$'\r'/'%0D'}"
echo "::set-output name=CHANGELOG::$description"
- name: Create GitHub Release
if: steps.validate_pr.outputs.is_release
id: make-release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: 'v${{ steps.metadata.outputs.version }}'
commitish: main
release_name: 'v${{ steps.metadata.outputs.version }} Release'
body: "${{ steps.get-changelog.outputs.CHANGELOG }}"
draft: false
prerelease: false