-
Notifications
You must be signed in to change notification settings - Fork 0
153 lines (150 loc) · 6.32 KB
/
release.yml
File metadata and controls
153 lines (150 loc) · 6.32 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
name: Release
on:
push:
tags:
- "v*"
permissions:
contents: write
jobs:
release:
runs-on: ubuntu-latest
env:
MARKETPLACE_PR_TOKEN: ${{ secrets.MARKETPLACE_PR_TOKEN }}
PUBLISH_COOLDOWN_MINUTES: "10"
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm
registry-url: https://npm.pkg.github.com/
scope: '@napgram'
- run: pnpm i
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: pnpm build
- run: pnpm validate
- name: Check publish window
run: |
node --input-type=module <<'NODE'
import fs from 'fs';
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
const name = pkg.name;
const version = pkg.version;
const cooldown = Number(process.env.PUBLISH_COOLDOWN_MINUTES || '10');
if (!Number.isFinite(cooldown) || cooldown <= 0) {
console.log('Publish cooldown disabled');
process.exit(0);
}
const res = await fetch(`https://npm.pkg.github.com/@napgram%2f${encodeURIComponent(name.replace('@napgram/', ''))}`, {
headers: { Accept: 'application/vnd.npm.install-v1+json' }
});
if (res.status === 404) {
console.log('Package not found on registry; skip cooldown check');
process.exit(0);
}
if (!res.ok) {
throw new Error(`Registry request failed: ${res.status}`);
}
const data = await res.json();
if (data?.versions?.[version]) {
console.error(`Version ${version} already published for ${name}`);
process.exit(1);
}
const versionCount = data?.versions ? Object.keys(data.versions).length : 0;
if (versionCount <= 1) {
console.log('Only one version on registry; skip cooldown check');
process.exit(0);
}
const time = data?.time || {};
const ignore = new Set(['created', 'modified']);
const dates = Object.entries(time)
.filter(([key]) => !ignore.has(key))
.map(([, value]) => new Date(value))
.filter((date) => !Number.isNaN(date.valueOf()));
if (!dates.length) {
console.log('No publish timestamps found');
process.exit(0);
}
const last = new Date(Math.max(...dates.map((date) => date.valueOf())));
const ageMinutes = (Date.now() - last.getTime()) / 60000;
if (ageMinutes < cooldown) {
console.error(`Last publish was ${ageMinutes.toFixed(1)} minutes ago; cooldown is ${cooldown} minutes`);
process.exit(1);
}
console.log('Publish window ok');
NODE
- run: pnpm pack:zip
- run: pnpm pack:tgz
- run: pnpm -s marketplace:snippet > marketplace-index-snippet.json
- id: marketplace-meta
run: |
node --input-type=module -e "import fs from 'fs'; const pkg = JSON.parse(fs.readFileSync('package.json','utf8')); const meta = JSON.parse(fs.readFileSync('napgram-plugin.json','utf8')); const id = (meta.id || pkg.name); console.log('id=' + id); console.log('version=' + pkg.version);" >> $GITHUB_OUTPUT
- if: ${{ env.MARKETPLACE_PR_TOKEN != '' }}
name: Ensure marketplace fork
uses: actions/github-script@v7
with:
github-token: ${{ env.MARKETPLACE_PR_TOKEN }}
script: |
const upstream = { owner: 'NapGram', repo: 'marketplace' };
const forkOwner = context.actor;
try {
await github.rest.repos.get({ owner: forkOwner, repo: upstream.repo });
return;
} catch (error) {
if (error.status !== 404) throw error;
}
await github.rest.repos.createFork({ owner: upstream.owner, repo: upstream.repo });
for (let i = 0; i < 10; i++) {
try {
await github.rest.repos.get({ owner: forkOwner, repo: upstream.repo });
return;
} catch {
await new Promise(resolve => setTimeout(resolve, 3000));
}
}
core.setFailed('marketplace fork not ready');
- if: ${{ env.MARKETPLACE_PR_TOKEN != '' }}
name: Check marketplace workflow access
uses: actions/github-script@v7
with:
github-token: ${{ env.MARKETPLACE_PR_TOKEN }}
script: |
const forkOwner = context.actor;
const repo = 'marketplace';
const path = '.github/workflows/validate-pr.yml';
try {
await github.rest.repos.getContent({ owner: forkOwner, repo, path });
return;
} catch (error) {
if (error.status === 404) {
core.setFailed('Marketplace fork missing .github/workflows/validate-pr.yml. Sync the fork or use a token with workflow scope to allow pushing workflow files.');
return;
}
throw error;
}
- if: ${{ env.MARKETPLACE_PR_TOKEN != '' }}
uses: actions/checkout@v4
with:
repository: NapGram/marketplace
token: ${{ env.MARKETPLACE_PR_TOKEN }}
path: marketplace
- if: ${{ env.MARKETPLACE_PR_TOKEN != '' }}
run: node scripts/marketplace-upsert.mjs --index marketplace/index.json --snippet marketplace-index-snippet.json
- if: ${{ env.MARKETPLACE_PR_TOKEN != '' }}
uses: peter-evans/create-pull-request@v6
with:
token: ${{ env.MARKETPLACE_PR_TOKEN }}
path: marketplace
commit-message: "marketplace: update ${{ steps.marketplace-meta.outputs.id }} ${{ steps.marketplace-meta.outputs.version }}"
branch: "marketplace/${{ steps.marketplace-meta.outputs.id }}-${{ steps.marketplace-meta.outputs.version }}"
title: "marketplace: update ${{ steps.marketplace-meta.outputs.id }} ${{ steps.marketplace-meta.outputs.version }}"
body: "Auto-generated marketplace submission."
base: main
push-to-fork: ${{ github.actor }}/marketplace
- uses: softprops/action-gh-release@v2
with:
files: |
release/*
marketplace-index-snippet.json