Skip to content

Commit

Permalink
ci: 👷 add ci pr release script
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustav-Eikaas committed Dec 18, 2023
1 parent 04209ea commit afbda49
Show file tree
Hide file tree
Showing 18 changed files with 633 additions and 38 deletions.
19 changes: 19 additions & 0 deletions .github/actions/pnpm-setup/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: PNPM setup
description: 'Set up pnpm with cache'

runs:
using: 'composite'
steps:
- uses: pnpm/action-setup@v2.2.4
with:
version: 8.0.0

- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: 18
cache: 'pnpm'

- name: Install dependencies
shell: bash
run: pnpm install
13 changes: 13 additions & 0 deletions .github/helpers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "fusion-workspace-helpers",
"version": "0.0.0",
"type": "module",
"private": true,
"dependencies": {
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0",
"commander": "^11.1.0",
"markdown-table": "^3.0.3",
"tsx": "^4.6.2"
}
}
97 changes: 97 additions & 0 deletions .github/helpers/src/pre-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env node

const defaultReleaseSchema: ReleaseSchema = {
packages: [],
};

import { Command } from 'commander';
import { execSync } from 'child_process';
import { setSecret } from '@actions/core';
import { writeFileSync, existsSync, readFileSync } from 'fs';
import { parsePackageJson } from './utils/parse-package-json.js';
import { logInfo } from './utils/logInfo.js';

const program = new Command();

program.name('PR');

program
.command('publish')
.option('-T, --token <token>', 'NPM auth token')
.action(async (args) => {
if (!args.token) {
throw new Error('Missing npm token');
}
setSecret(args.token);
execSync(`pnpm config set '//registry.npmjs.org/:_authToken' "${args.token}"`, { stdio: 'inherit' });
createFusionApp();
});

await program.parseAsync();

/**
* Returns the next patch of packagejson version
*/
function packageJsonNextPatch() {
const packageJson = parsePackageJson();
let version = packageJson.version.split('-')[0];

const splitVersion = version.split('.');

splitVersion[2] = (parseInt(splitVersion[2]) + 1).toString();
return splitVersion.join('.');
}

export async function createFusionApp() {
preparePackageForVerBump();
bumpPackageJson();
publishPrPackage();
}

function publishPrPackage() {
const releaseMessage = execSync('pnpm publish --tag pr --json --dry-run --no-git-checks');

const npmRelease: NpmRelease = JSON.parse(releaseMessage.toString('utf-8'));
logInfo(npmRelease.id, 'Green');
appendReleaseLog(npmRelease);
}

type ReleaseSchema = {
packages: string[];
};

function appendReleaseLog(npmRelease: NpmRelease) {
const fileName = '../../release.json';
if (!existsSync(fileName)) {
writeFileSync(fileName, JSON.stringify(defaultReleaseSchema, null, 2));
}
const release = readFileSync(fileName).toString('utf-8');
const contents: ReleaseSchema = JSON.parse(release);
contents.packages.push(npmRelease.id);
writeFileSync(fileName, JSON.stringify(contents));
}

function preparePackageForVerBump() {
const packageJson = parsePackageJson();
const nextPatch = packageJsonNextPatch();
const maybeVersion = execSync(`pnpm show ${packageJson.name} --json dist-tags.pr`).toString('utf-8');

if (maybeVersion === '') return;

//There is a pr tag release including the package json ver
if (maybeVersion.includes(nextPatch)) {
packageJson.version = maybeVersion.replaceAll(`"`, '');
writeFileSync('./package.json', JSON.stringify(packageJson, null, 2));
}
}

function bumpPackageJson() {
execSync('pnpm version prerelease --preid pr');
}

type NpmRelease = {
id: string;
name: string;
version: number;
unpackedSize: number;
};
26 changes: 26 additions & 0 deletions .github/helpers/src/utils/logInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const ColorReset = '\x1b[0m';
type TextColor =
| 'Red'
| 'Green'
| 'Black'
| 'Yellow'
| 'Blue'
| 'Magenta'
| 'Cyan'
| 'White';

const textColor = {
Red: '\x1b[31m',
Black: '\x1b[30m',
Green: '\x1b[32m',
Yellow: '\x1b[33m',
Blue: '\x1b[34m',
Magenta: '\x1b[35m',
Cyan: '\x1b[36m',
White: '\x1b[37m',
} satisfies Record<TextColor, string>;

export function logInfo(message: string, color: TextColor): void {
console.log(`${textColor[color]}${message}${ColorReset}`);
}

12 changes: 12 additions & 0 deletions .github/helpers/src/utils/parse-package-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from 'fs';

export type PackageJson = {
name?: string;
version?: string;
type?: 'module' | 'commonjs';
} & Record<string, any>;

export function parsePackageJson(path: string = './package.json'): PackageJson {
const blob = fs.readFileSync(path);
return JSON.parse(blob.toString('utf-8'));
}
8 changes: 8 additions & 0 deletions .github/helpers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ESNext"
},
"include": ["src"]
}
16 changes: 2 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,7 @@ jobs:
with:
fetch-depth: 0

- uses: nrwl/nx-set-shas@v4

- name: Setup PNPM
uses: pnpm/action-setup@v2
with:
version: 8

- uses: actions/setup-node@v4
with:
cache: 'pnpm'

- run: pnpm config set auto-install-peers true

- run: npm run first-time-setup
- name: PNPM setup
uses: ./.github/actions/pnpm-setup

- run: pnpm build
31 changes: 31 additions & 0 deletions .github/workflows/pre-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Publish PR build
on:
workflow_dispatch:
permissions:
actions: read
checks: write
contents: read
deployments: write
id-token: write
statuses: write
jobs:
publish:
runs-on: ubuntu-latest
environment: npm
env:
NPM_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: PNPM setup
uses: ./.github/actions/pnpm-setup

- name: Build monorepo
run: pnpm build

- name: Publish PR version
run: |
pnpm config set '//registry.npmjs.org/:_authToken' "${NPM_TOKEN}"
pnpm preship --no-git-checks
20 changes: 11 additions & 9 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ on:
push:
branches:
- main
- preview/server-side
jobs:
publish:
runs-on: ubuntu-latest
Expand All @@ -14,11 +13,14 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- run: npm run first-time-setup
#... previous steps
# Run `build` script in every projects
- run: pnpm run build
# Set token
- run: pnpm config set '//registry.npmjs.org/:_authToken' "${NPM_TOKEN}"
# Publish all packages in the workspace which version still does not exist
- run: pnpm shipit --no-git-checks

- name: PNPM setup
uses: ./.github/actions/pnpm-setup

- name: Build monorepo
run: pnpm build

- name: Publish NPM
run: |
pnpm config set '//registry.npmjs.org/:_authToken' "${NPM_TOKEN}"
pnpm shipit --no-git-checks
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"build": "turbo run build",
"test": "turbo run test",
"documentation": "pnpx astro dev --root ./documentation",
"shipit": "pnpm build && pnpm -r publish",
"shipit": "turbo run build && pnpm -r publish",
"pr-shipit": "turbo run build && pnpm --filter ...[origin/main] pr-release",
"dev": "turbo run dev --filter ./apps/**"
},
"private": true,
Expand All @@ -23,6 +24,7 @@
"@typescript-eslint/parser": "~5.24.0",
"prettier": "^2.6.2",
"tsup": "^8.0.1",
"tsx": "^4.6.2",
"turbo": "^1.9.3",
"typescript": "~5.0.2",
"typescript-plugin-styled-components": "^2.0.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/ag-grid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"license": "MIT",
"scripts": {
"build": "tsup ./src/index.ts ./src/modules/index.ts --format cjs,esm --dts --clean",
"build:prod": "tsup ./src/index.ts ./src/modules/index.ts --format cjs,esm --dts --clean"
"build:prod": "tsup ./src/index.ts ./src/modules/index.ts --format cjs,esm --dts --clean",
"pr-release": "tsx ../../.github/helpers/src/pre-version.ts publish"
},
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down
3 changes: 2 additions & 1 deletion packages/filter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"license": "MIT",
"scripts": {
"build": "tsup ./src/index.ts --format cjs,esm --dts --clean",
"build:prod": "tsup ./src/index.ts --format cjs,esm --dts --clean"
"build:prod": "tsup ./src/index.ts --format cjs,esm --dts --clean",
"pr-release": "tsx ../../.github/helpers/src/pre-version.ts publish"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 2 additions & 1 deletion packages/garden/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"license": "MIT",
"scripts": {
"build": "tsup ./src/index.ts --format cjs,esm --dts --clean",
"build:prod": "tsup ./src/index.ts --format cjs,esm --dts --clean"
"build:prod": "tsup ./src/index.ts --format cjs,esm --dts --clean",
"pr-release": "tsx ../../.github/helpers/src/pre-version.ts publish"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 2 additions & 1 deletion packages/power-bi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"license": "MIT",
"scripts": {
"build": "tsup ./src/index.ts --format cjs,esm --dts --clean",
"build:prod": "tsup ./src/index.ts --format cjs,esm --dts --clean"
"build:prod": "tsup ./src/index.ts --format cjs,esm --dts --clean",
"pr-release": "tsx ../../.github/helpers/src/pre-version.ts publish"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 2 additions & 1 deletion packages/workspace-fusion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"license": "MIT",
"scripts": {
"build": "tsup",
"build:prod": "tsup"
"build:prod": "tsup",
"pr-release": "tsx ../../.github/helpers/src/pre-version.ts publish"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 2 additions & 1 deletion packages/workspace-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"license": "MIT",
"scripts": {
"build": "tsup ./src/index.ts --format cjs,esm --dts --clean",
"build:prod": "tsup ./src/index.ts --format cjs,esm --dts --clean"
"build:prod": "tsup ./src/index.ts --format cjs,esm --dts --clean",
"pr-release": "tsx ../../.github/helpers/src/pre-version.ts publish"
},
"publishConfig": {
"access": "public"
Expand Down
Loading

0 comments on commit afbda49

Please sign in to comment.