Skip to content

Commit 6752d71

Browse files
authored
Add release action (#113)
1 parent 27c5a9e commit 6752d71

File tree

5 files changed

+132
-23
lines changed

5 files changed

+132
-23
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: PR Title Check
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
types:
8+
- opened
9+
- synchronize
10+
- reopened
11+
- edited
12+
13+
jobs:
14+
check:
15+
name: Check
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v2
19+
with:
20+
fetch-depth: 0
21+
- name: Check
22+
run: npx ts-node --transpile-only scripts/check-pr-title.ts "$PR_TITLE"
23+
env:
24+
PR_TITLE: ${{ github.event.pull_request.title }}

.github/workflows/release.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Validate
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
env:
9+
FORCE_COLOR: true
10+
11+
jobs:
12+
release:
13+
name: Release
14+
runs-on: ubuntu-latest
15+
if: ${{ github.repository == 'cloud-annotations/docusaurus-plugin-openapi' && github.ref == 'refs/heads/main' && github.event_name == 'push' }}
16+
steps:
17+
- uses: actions/checkout@v2
18+
with:
19+
fetch-depth: 0
20+
- run: |
21+
git config user.name "github-actions[bot]"
22+
git config user.email "github-actions[bot]@users.noreply.github.com"
23+
- uses: actions/setup-node@v2
24+
with:
25+
node-version: "*"
26+
registry-url: "https://registry.npmjs.org"
27+
- name: Install
28+
run: yarn install --frozen-lockfile
29+
- name: Release
30+
run: yarn release:publish
31+
env:
32+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@typescript-eslint/eslint-plugin": "^4.0.0",
4141
"@typescript-eslint/parser": "^4.0.0",
4242
"babel-eslint": "^10.0.0",
43+
"chalk": "^4.1.2",
4344
"cross-env": "^7.0.3",
4445
"cypress": "^8.7.0",
4546
"eslint": "^7.5.0",

scripts/check-pr-title.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env ts-node
2+
/* ============================================================================
3+
* Copyright (c) Cloud Annotations
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
* ========================================================================== */
8+
9+
import { version } from "../lerna.json";
10+
import { getOutput } from "./utils/get-output";
11+
12+
// Makes the script crash on unhandled rejections instead of silently
13+
// ignoring them. In the future, promise rejections that are not handled will
14+
// terminate the Node.js process with a non-zero exit code.
15+
process.on("unhandledRejection", (err) => {
16+
throw err;
17+
});
18+
19+
function versions() {
20+
return getOutput(`git tag --list 'v*'`).split("\n");
21+
}
22+
23+
function main() {
24+
const actualTitle = process.argv[2];
25+
const expectedTitle = `Prepare release v${version}`;
26+
27+
if (!versions().includes(`v${version}`)) {
28+
if (actualTitle !== expectedTitle) {
29+
console.log(`\x1b[31mPR title should be: "${expectedTitle}"\x1b[0m`);
30+
process.exit(1);
31+
}
32+
}
33+
}
34+
35+
main();

scripts/publish.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import { execSync } from "child_process";
1010
import fs from "fs";
1111
import path from "path";
1212

13-
import pkg from "../lerna.json";
13+
import { yellow } from "chalk";
14+
15+
import { version } from "../lerna.json";
1416
import { createDryRun } from "./utils/dry-run";
1517
import { getOutput } from "./utils/get-output";
1618
import { printBanner } from "./utils/print-utils";
1719

1820
const ORG = "cloud-annotations";
1921
const REPO = "docusaurus-plugin-openapi";
20-
const BUILD_PATH = "build";
21-
const REPO_ROOT = path.join(BUILD_PATH, REPO);
22+
let REPO_ROOT = undefined;
2223

2324
// Makes the script crash on unhandled rejections instead of silently
2425
// ignoring them. In the future, promise rejections that are not handled will
@@ -28,7 +29,7 @@ process.on("unhandledRejection", (err) => {
2829
});
2930

3031
const safeExec = createDryRun(execSync);
31-
const safeRmdir = createDryRun(fs.rmdirSync);
32+
const safeRmdir = createDryRun(fs.rmSync);
3233
const safeMkdir = createDryRun(fs.mkdirSync);
3334

3435
function getGitUserName() {
@@ -40,21 +41,33 @@ function getGitUserEmail() {
4041
}
4142

4243
function ensureCleanDir(path: string) {
43-
safeRmdir(path, { recursive: true });
44-
safeMkdir(path);
44+
if (fs.existsSync(path)) {
45+
safeRmdir(path, { recursive: true });
46+
}
47+
safeMkdir(path, { recursive: true });
4548
}
4649

4750
function checkoutCode() {
4851
printBanner("Retrieving source code");
4952

53+
const BUILD_PATH = "build";
5054
ensureCleanDir(BUILD_PATH);
5155

52-
const gitUserName = getGitUserName();
53-
const gitUserEmail = getGitUserEmail();
54-
5556
safeExec(`git clone [email protected]:${ORG}/${REPO}.git ${REPO}`, {
5657
cwd: BUILD_PATH,
5758
});
59+
60+
REPO_ROOT = path.join(BUILD_PATH, REPO);
61+
62+
safeExec(`yarn install`, {
63+
cwd: REPO_ROOT,
64+
stdio: "ignore",
65+
});
66+
}
67+
68+
function configureGit() {
69+
const gitUserName = getGitUserName();
70+
const gitUserEmail = getGitUserEmail();
5871
safeExec(`git config user.name ${gitUserName}`, {
5972
cwd: REPO_ROOT,
6073
});
@@ -66,28 +79,20 @@ function checkoutCode() {
6679
function buildAndPublish() {
6780
printBanner("Building Packages");
6881

69-
safeExec(`yarn install`, {
70-
cwd: REPO_ROOT,
71-
stdio: "ignore",
72-
});
73-
7482
safeExec(`yarn lerna run build --no-private`, {
7583
cwd: REPO_ROOT,
7684
});
7785

7886
printBanner("Publishing Packages");
7987

80-
safeExec(
81-
`lerna publish --yes from-package --no-git-tag-version --no-verify-access --no-push`,
82-
{
83-
cwd: REPO_ROOT,
84-
}
85-
);
88+
safeExec(`lerna publish --yes from-package`, {
89+
cwd: REPO_ROOT,
90+
});
8691
}
8792

8893
function tag() {
89-
const tag = `v${pkg.version}`;
90-
const message = `Version ${pkg.version}`;
94+
const tag = `v${version}`;
95+
const message = `Version ${version}`;
9196
safeExec(`git tag -a ${tag} -m "${message}"`, {
9297
cwd: REPO_ROOT,
9398
});
@@ -96,8 +101,19 @@ function tag() {
96101
});
97102
}
98103

104+
function versions() {
105+
return getOutput(`git tag --list 'v*'`).split("\n");
106+
}
107+
99108
function main() {
100-
checkoutCode();
109+
if (versions().includes(`v${version}`)) {
110+
console.log(yellow(`SKIPPING: Version ${version} already exists.`));
111+
return;
112+
}
113+
if (!process.env.CI) {
114+
checkoutCode();
115+
}
116+
configureGit();
101117
buildAndPublish();
102118
tag();
103119
}

0 commit comments

Comments
 (0)