Skip to content

Commit 8fb2beb

Browse files
authored
Merge pull request #5 from hoangndst/ci
ci: pr and release
2 parents dfe0825 + 538e4d2 commit 8fb2beb

File tree

9 files changed

+920
-7
lines changed

9 files changed

+920
-7
lines changed

.changeset/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changesets
2+
3+
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
4+
with multi-package repos, or single-package repos to help you version and publish your code. You can
5+
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
6+
7+
We have a quick list of common questions to get you started engaging with this project in
8+
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)

.changeset/config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
3+
"changelog": "@changesets/cli/changelog",
4+
"commit": false,
5+
"fixed": [],
6+
"linked": [],
7+
"access": "restricted",
8+
"baseBranch": "main",
9+
"updateInternalDependencies": "patch",
10+
"ignore": []
11+
}

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* @hoangndst @Yangyang96
2+
3+
/plugins/scaffolder-backend-module-kusion @hoangndst

.github/workflows/ci.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: PR CI
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
push:
8+
branches:
9+
- main
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
15+
env:
16+
CI: true
17+
NODE_OPTIONS: --max-old-space-size=4096
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
- uses: actions/setup-node@v4
24+
with:
25+
node-version: 20.x
26+
cache: 'yarn'
27+
- name: cache all node_modules
28+
id: cache-modules
29+
uses: actions/cache@v4 # v4
30+
with:
31+
path: '**/node_modules'
32+
key: ${{ runner.os }}-node_modules-${{ hashFiles('yarn.lock', '**/package.json') }}
33+
- name: find location of global yarn cache
34+
id: yarn-cache
35+
if: steps.cache-modules.outputs.cache-hit != 'true'
36+
run: echo "dir=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT
37+
- name: cache global yarn cache
38+
uses: actions/cache@v4 # v4
39+
if: steps.cache-modules.outputs.cache-hit != 'true'
40+
with:
41+
path: ${{ steps.yarn-cache.outputs.dir }}
42+
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
43+
restore-keys: |
44+
${{ runner.os }}-yarn-
45+
- name: install dependencies
46+
run: yarn install --immutable
47+
- name: lint
48+
run: yarn lint
49+
- name: tsc
50+
run: yarn tsc
51+
- name: test
52+
run: yarn test:all
53+
- name: build
54+
run: yarn build:all

.github/workflows/release.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: Release Plugins
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
9+
jobs:
10+
changesets-pr:
11+
name: Update Version Packages PR
12+
runs-on: ubuntu-latest
13+
outputs:
14+
needs_release: ${{ steps.release_check.outputs.needs_release }}
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4
18+
19+
- name: Set up Node
20+
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4
21+
with:
22+
node-version: 20
23+
registry-url: https://registry.npmjs.org/ # Needed for auth
24+
25+
- name: Fetch previous commit for check
26+
run: git fetch origin '${{ github.event.before }}'
27+
28+
- name: Check if release
29+
id: release_check
30+
run: |
31+
yarn install
32+
node scripts/ci/check-if-release.js
33+
working-directory: ./
34+
env:
35+
COMMIT_SHA_BEFORE: '${{ github.event.before }}'
36+
37+
- name: Update Version Packages PR
38+
uses: backstage/changesets-action@291bfc1f76d1dcfbf967f5810dc0423592eae09a # v2.3.1
39+
with:
40+
title: Version Packages
41+
version: yarn changeset version
42+
skipRootChangelogUpdate: true
43+
env:
44+
GITHUB_TOKEN: ${{ secrets.KUSIONSTACK_BOT_TOKEN }}
45+
46+
release:
47+
name: Release
48+
runs-on: ubuntu-latest
49+
needs: changesets-pr
50+
if: needs.changesets-pr.outputs.needs_release == 'true'
51+
env:
52+
CI: true
53+
NODE_OPTIONS: --max-old-space-size=4096
54+
55+
steps:
56+
- name: Checkout
57+
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4
58+
59+
- name: Set up Node
60+
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4
61+
with:
62+
node-version: 20
63+
registry-url: https://registry.npmjs.org/ # Needed for auth
64+
65+
- name: Install root dependencies
66+
run: yarn install --immutable
67+
68+
- name: Compile TypeScript
69+
run: yarn tsc
70+
71+
- name: Build all packages
72+
run: yarn build:all
73+
74+
- name: publish
75+
run: |
76+
yarn config set -H 'npmAuthToken' "${{secrets.NPM_TOKEN}}"
77+
yarn workspaces foreach -W -v --no-private npm publish --access public --tolerate-republish
78+
env:
79+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
80+
81+
- name: Create tag
82+
working-directory: ${{ github.workspace }}/scripts/ci
83+
run: node create-tag.js
84+
env:
85+
GITHUB_TOKEN: ${{ secrets.KUSIONSTACK_BOT_TOKEN }}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
"devDependencies": {
3434
"@backstage/cli": "^0.29.0",
3535
"@backstage/e2e-test-utils": "^0.1.1",
36+
"@changesets/cli": "^2.29.4",
37+
"@octokit/rest": "^21.1.1",
3638
"@playwright/test": "^1.32.3",
3739
"@spotify/prettier-config": "^12.0.0",
3840
"node-gyp": "^10.0.0",

scripts/ci/check-if-release.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#!/usr/bin/env node
2+
/* eslint-disable @backstage/no-undeclared-imports */
3+
/*
4+
* Copyright 2020 The Backstage Authors
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// This script is used to determine whether a particular commit has changes
20+
// that should lead to a release. It is run as part of the main master build
21+
// to determine whether the release flow should be run as well.
22+
//
23+
// It has the following output which can be used later in GitHub actions:
24+
//
25+
// needs_release = 'true' | 'false'
26+
27+
import { execFile as execFileCb } from 'child_process';
28+
import { promises as fs } from 'fs';
29+
import { promisify } from 'util';
30+
import { resolve as resolvePath } from 'path';
31+
import { EOL } from 'os';
32+
33+
import * as url from 'url';
34+
35+
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
36+
37+
const parentRef = process.env.COMMIT_SHA_BEFORE || 'HEAD^';
38+
const targetBranch = process.env.TARGET_BRANCH || 'main';
39+
40+
const execFile = promisify(execFileCb);
41+
42+
async function runPlain(cmd, ...args) {
43+
try {
44+
const { stdout } = await execFile(cmd, args, { shell: true });
45+
return stdout.trim();
46+
} catch (error) {
47+
if (error.stderr) {
48+
process.stderr.write(error.stderr);
49+
}
50+
if (!error.code) {
51+
throw error;
52+
}
53+
throw new Error(
54+
`Command '${[cmd, ...args].join(' ')}' failed with code ${error.code}`,
55+
);
56+
}
57+
}
58+
59+
async function main() {
60+
process.cwd(resolvePath(__dirname));
61+
62+
if (!process.env.GITHUB_OUTPUT) {
63+
throw new Error('GITHUB_OUTPUT environment variable not set');
64+
}
65+
66+
// Ensure we have fetched the targetBranch
67+
await runPlain('git', 'fetch', 'origin', targetBranch);
68+
69+
const diff = await runPlain(
70+
'git',
71+
'diff',
72+
'--name-only',
73+
parentRef,
74+
"'*/package.json'", // Git treats this as what would usually be **/package.json
75+
);
76+
77+
const packageList = diff
78+
.split('\n')
79+
.filter(path => path.match(/^(packages|plugins)\/[^/]+\/package\.json$/));
80+
81+
const packageVersions = await Promise.all(
82+
packageList.map(async path => {
83+
let name;
84+
let newVersion;
85+
let oldVersion;
86+
87+
try {
88+
const data = JSON.parse(
89+
await runPlain('git', 'show', `${parentRef}:${path}`),
90+
);
91+
name = data.name;
92+
oldVersion = data.version;
93+
} catch {
94+
oldVersion = '<none>';
95+
}
96+
97+
try {
98+
const data = JSON.parse(await fs.readFile(path, 'utf8'));
99+
name = data.name;
100+
newVersion = data.version;
101+
} catch (error) {
102+
if (error.code === 'ENOENT') {
103+
newVersion = '<none>';
104+
}
105+
}
106+
107+
return { name, oldVersion, newVersion };
108+
}),
109+
);
110+
111+
const newVersions = packageVersions.filter(
112+
({ oldVersion, newVersion }) =>
113+
oldVersion !== newVersion &&
114+
oldVersion !== '<none>' &&
115+
newVersion !== '<none>',
116+
);
117+
118+
if (newVersions.length === 0) {
119+
console.log('No package version bumps detected, no release needed');
120+
await fs.appendFile(process.env.GITHUB_OUTPUT, `needs_release=false${EOL}`);
121+
return;
122+
}
123+
124+
console.log('Package version bumps detected, a new release is needed');
125+
const maxLength = Math.max(...newVersions.map(_ => _.name.length));
126+
for (const { name, oldVersion, newVersion } of newVersions) {
127+
console.log(
128+
` ${name.padEnd(maxLength, ' ')} ${oldVersion} to ${newVersion}`,
129+
);
130+
}
131+
await fs.appendFile(process.env.GITHUB_OUTPUT, `needs_release=true${EOL}`);
132+
}
133+
134+
main().catch(error => {
135+
console.error(error.stack);
136+
process.exit(1);
137+
});

0 commit comments

Comments
 (0)