-
Notifications
You must be signed in to change notification settings - Fork 1
134 lines (128 loc) · 4.71 KB
/
on_release_pull_merged.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
# This workflow provided by GitHub Actions Manager
# Documentation: https://github.com/vemel/github_actions_js/blob/main/nodejs_workflows/README.md
#
# - Runs only if `NPM_TOKEN` secret is set
# - Starts on Pull Request merge for `release/*` branch
# - Uses Pull Request branch for deployment, so released version contains only changes
# from base branch when Release had been published
# - Builds package if `build` script is available in `package.json`
# - Publishes new version to [npm](https://www.npmjs.com/)
#
# Secrets:
# NPM_TOKEN - Token for npm publishing https://docs.npmjs.com/creating-and-viewing-access-tokens
name: Publish to NPM
"on":
pull_request:
types:
- closed
workflow_dispatch:
inputs:
number:
description: Pull Request number
required: true
jobs:
publish-to-npm:
runs-on: ubuntu-latest
env:
BUILD_SCRIPT: build
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
steps:
- name: Get Pull Request
id: pull-request
if: env.NPM_TOKEN
uses: actions/github-script@v6
with:
script: |
// github-actions-managed: true
let pullRequest = null;
if (context.payload.inputs && context.payload.inputs.number) {
// find Pull Request by number fom input
const number = context.payload.inputs.number;
try {
pullRequest = (await github.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: number,
})).data;
} catch(e) {
core.error(`Cannot get Pull Request #${number}: ${e}`);
return;
}
} else {
// get Pull Request from event payload
pullRequest = context.payload.pull_request;
}
if (!pullRequest) {
core.error(`Cannot find Pull Request`);
return;
}
// Stop if Pull request is not merged
if (!pullRequest.merged) {
core.info(`Pull Request ${pullRequest.number} is not merged, skipping`);
return;
}
// Stop if Pull Request branch name starts with `release`
const branch = pullRequest.head.ref;
if (!branch.startsWith('release')) {
core.info(`This is not a release branch ${branch}, skipping`);
return;
}
core.setOutput('branch', branch);
core.setOutput('base', pullRequest.base.ref);
core.setOutput('number', pullRequest.number);
core.setOutput('url', pullRequest.html_url);
core.setOutput('body', pullRequest.body);
core.setOutput('suffix', `[#${pullRequest.number}](${pullRequest.html_url})`);
return true;
- name: Checkout Pull Request base branch
id: checkout
if: steps.pull-request.outputs.result
uses: actions/checkout@v3
with:
github-actions-managed: true
ref: ${{ steps.pull-request.outputs.base }}
- name: Get available scripts
id: scripts
if: steps.pull-request.outputs.result
uses: actions/github-script@v6
with:
script: |
// github-actions-managed: true
const output = (await exec.getExecOutput('npm run')).stdout;
const scripts = output.split(/\r?\n/)
.filter(line => line.startsWith(' ') && !line.startsWith(' '))
.map(line => line.trim());
if (scripts.includes(process.env.BUILD_SCRIPT)) {
core.setOutput('build', process.env.BUILD_SCRIPT);
}
return true;
- name: Set up Node.js
uses: actions/setup-node@v3
id: setup-node
if: steps.pull-request.outputs.result
with:
github-actions-managed: true
registry-url: https://registry.npmjs.org/
node-version: 18
- name: Install package
id: install
if: steps.pull-request.outputs.result
run: |
# github-actions-managed: true
npm install -g npm
npm ci
- name: Build package
id: build
if: steps.pull-request.outputs.result && steps.scripts.outputs.build
run: |
# github-actions-managed: true
echo "Running: npm run ${{ steps.scripts.outputs.build }}"
npm run ${{ steps.scripts.outputs.build }}
- name: Publish new version to NPM
id: publish
if: steps.pull-request.outputs.result
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
# github-actions-managed: true
npm publish --access public --verbose