-
Notifications
You must be signed in to change notification settings - Fork 5
141 lines (120 loc) · 5.5 KB
/
Copy pathsync.yml
File metadata and controls
141 lines (120 loc) · 5.5 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
name: Sync with React Release
on:
schedule:
- cron: "0 0 * * *" # daily
workflow_dispatch:
jobs:
sync-and-publish:
permissions:
contents: write
id-token: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "24"
- name: Install semver
run: npm install semver
- name: Check React version
id: react-version
uses: actions/github-script@v6
with:
result-encoding: string
script: |
const semver = require('semver');
try {
const reactResponse = await fetch('https://registry.npmjs.org/react');
const reactData = await reactResponse.json();
const reactVersion = reactData['dist-tags'].latest;
const packageResponse = await fetch('https://registry.npmjs.org/umd-react');
const packageData = await packageResponse.json();
const publishedVersion = packageData['dist-tags'].latest;
const fs = require('fs');
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
const tagName = `v${reactVersion}`;
let releaseExists = false;
try {
await github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag: tagName,
});
releaseExists = true;
} catch (error) {
if (error.status !== 404) {
throw error;
}
}
console.log(`Current package.json version: "${pkg.version}"`);
console.log(`Published npm version: "${publishedVersion}"`);
console.log(`Latest React version: "${reactVersion}"`);
const isNewer = semver.gt(reactVersion, publishedVersion);
const isDowngradeFromPrerelease = publishedVersion.includes('-') &&
semver.coerce(publishedVersion) &&
semver.eq(semver.coerce(publishedVersion).version, reactVersion);
const needsVersionSync = pkg.version !== reactVersion;
const needsPublish = isNewer && !isDowngradeFromPrerelease;
const needsRelease = !releaseExists &&
(needsPublish || publishedVersion === reactVersion || needsVersionSync);
const needsBuild = needsPublish || needsRelease;
console.log(`Is newer than published: ${isNewer}`);
console.log(`Is downgrade from prerelease: ${isDowngradeFromPrerelease}`);
console.log(`Needs version sync: ${needsVersionSync}`);
console.log(`Release exists for ${tagName}: ${releaseExists}`);
console.log(`Needs publish: ${needsPublish}`);
console.log(`Needs release: ${needsRelease}`);
let result = 'unchanged';
if (needsVersionSync || needsPublish || needsRelease) {
console.log(`✅ Preparing release ${reactVersion}`);
core.setOutput('newVersion', reactVersion);
core.setOutput('needsVersionSync', String(needsVersionSync));
core.setOutput('needsPublish', String(needsPublish));
core.setOutput('needsRelease', String(needsRelease));
core.setOutput('needsBuild', String(needsBuild));
result = 'updated';
} else {
console.log(`❌ No update needed`);
}
return result;
} catch (error) {
console.error('Error in version check:', error);
return 'unchanged';
}
- name: Sync package version
if: steps.react-version.outputs.needsVersionSync == 'true'
run: npm version "${{ steps.react-version.outputs.newVersion }}" --no-git-tag-version
- name: Install dependencies
if: steps.react-version.outputs.needsBuild == 'true'
run: npm ci
- name: Build
if: steps.react-version.outputs.needsBuild == 'true'
run: npm run build
- name: Publish to NPM
if: steps.react-version.outputs.needsPublish == 'true'
run: npm publish
- name: Commit and push if changed
if: steps.react-version.outputs.needsVersionSync == 'true'
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add package.json package-lock.json
git commit -m "chore: update version to match React ${{ steps.react-version.outputs.newVersion }}"
git push
- name: Capture release commit SHA
if: steps.react-version.outputs.needsRelease == 'true'
id: release-sha
run: echo "value=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
- name: Create GitHub release
if: steps.react-version.outputs.needsRelease == 'true'
uses: softprops/action-gh-release@v2
with:
tag_name: "v${{ steps.react-version.outputs.newVersion }}"
target_commitish: "${{ steps.release-sha.outputs.value }}"
generate_release_notes: true
files: |
dist/react.development.js
dist/react.production.min.js
dist/react-dom.development.js
dist/react-dom.production.min.js