-
Notifications
You must be signed in to change notification settings - Fork 14
167 lines (147 loc) · 5.83 KB
/
Copy pathdeploy.yml
File metadata and controls
167 lines (147 loc) · 5.83 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
name: Deploy to npm
on:
push:
branches:
- main
permissions:
id-token: write # Required for OIDC authentication
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v7
- name: Setup pnpm
uses: pnpm/action-setup@v6
- name: Set up Node.js
uses: actions/setup-node@v6
with:
node-version: "24"
registry-url: "https://registry.npmjs.org"
cache: pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Build project
run: pnpm run build
- name: Check if version exists
id: check-version
run: |
if npm view $(node -p "require('./package.json').name")@$(node -p "require('./package.json').version") > /dev/null 2>&1; then
echo "exists=true" >> $GITHUB_ENV
else
echo "exists=false" >> $GITHUB_ENV
fi
- name: Publish to npm
id: publish_step
if: env.exists == 'false'
run: pnpm publish --provenance --access public
- name: Notify Slack (success)
if: env.exists == 'false' && steps.publish_step.outcome == 'success'
env:
SLACK_DEPLOY_WEBHOOK_URL: ${{ secrets.SLACK_DEPLOY_WEBHOOK_URL }}
shell: bash
run: |
set -euo pipefail
if [ -z "${SLACK_DEPLOY_WEBHOOK_URL:-}" ]; then
echo "SLACK_DEPLOY_WEBHOOK_URL not set; skipping Slack notification"
exit 0
fi
node <<'JS'
const fs = require('node:fs');
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
const version = packageJson.version;
const packageName = packageJson.name;
let latestChanges = 'See CHANGELOG for details.';
if (fs.existsSync('CHANGELOG.md')) {
const lines = fs.readFileSync('CHANGELOG.md', 'utf8').split(/\r?\n/);
const bullets = [];
let inTargetSection = false;
for (const line of lines) {
if (line.startsWith(`## [${version}]`)) {
inTargetSection = true;
continue;
}
if (inTargetSection && line.startsWith('## [')) break;
if (inTargetSection && line.startsWith('- ')) {
bullets.push(line.slice(2).trim());
}
}
if (bullets.length) {
latestChanges = bullets.slice(0, 5).map((bullet) => `• ${bullet}`).join('\n');
}
}
fs.writeFileSync('/tmp/slack_payload.json', JSON.stringify({
text: `Facturapi Node SDK ${version} published to npm`,
blocks: [
{
type: 'header',
text: {
type: 'plain_text',
text: `Node SDK ${version} published to npm`,
},
},
{
type: 'section',
fields: [
{ type: 'mrkdwn', text: `*Package:* \`${packageName}@${version}\`` },
{ type: 'mrkdwn', text: `*Branch:* \`${process.env.GITHUB_REF_NAME}\`` },
{ type: 'mrkdwn', text: `*Commit:* \`${process.env.GITHUB_SHA}\`` },
{ type: 'mrkdwn', text: `*Actor:* \`${process.env.GITHUB_ACTOR}\`` },
],
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: [
'*Useful links*',
`• npm: <https://www.npmjs.com/package/${packageName}/v/${version}|View package>`,
`• Workflow run: <${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}|Open run>`,
`• Changelog: <${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/blob/${process.env.GITHUB_SHA}/CHANGELOG.md|Read changes>`,
].join('\n'),
},
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Latest changes*\n${latestChanges}`,
},
},
],
}));
JS
curl -sS -X POST -H "Content-type: application/json" --data "@/tmp/slack_payload.json" "$SLACK_DEPLOY_WEBHOOK_URL" || true
- name: Notify Slack (failure)
if: failure() && env.exists == 'false' && steps.publish_step.outcome == 'failure'
env:
SLACK_DEPLOY_WEBHOOK_URL: ${{ secrets.SLACK_DEPLOY_WEBHOOK_URL }}
shell: bash
run: |
set -euo pipefail
if [ -z "${SLACK_DEPLOY_WEBHOOK_URL:-}" ]; then
echo "SLACK_DEPLOY_WEBHOOK_URL not set; skipping Slack notification"
exit 0
fi
node <<'JS'
const fs = require('node:fs');
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
fs.writeFileSync('/tmp/slack_payload_failure.json', JSON.stringify({
text: `Facturapi Node SDK ${packageJson.version} publish failed`,
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: [
`*Node SDK ${packageJson.version} publish failed*`,
`• Workflow run: <${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}|Open run>`,
`• Commit: \`${process.env.GITHUB_SHA}\``,
].join('\n'),
},
},
],
}));
JS
curl -sS -X POST -H "Content-type: application/json" --data "@/tmp/slack_payload_failure.json" "$SLACK_DEPLOY_WEBHOOK_URL" || true