From 9b4c73bc445d71913c905219f64fb212512f7da7 Mon Sep 17 00:00:00 2001 From: geekgeekrun Date: Mon, 8 Apr 2024 09:35:20 +0800 Subject: [PATCH] add release ci --- .../{build-linux.yml => build-ui.yml} | 13 +++-- .github/workflows/release-ui.yml | 54 +++++++++++++++++++ packages/ui/package.json | 3 +- packages/ui/scripts/build.mjs | 26 --------- packages/ui/scripts/github-ci-build.mjs | 3 ++ packages/ui/scripts/release-new-version.mjs | 8 +++ packages/ui/scripts/steps/build.mjs | 50 +++++++++++++++++ .../steps/increase-package-version.mjs | 32 +++++++++++ packages/ui/scripts/steps/release-version.mjs | 11 ++++ 9 files changed, 169 insertions(+), 31 deletions(-) rename .github/workflows/{build-linux.yml => build-ui.yml} (84%) create mode 100644 .github/workflows/release-ui.yml delete mode 100644 packages/ui/scripts/build.mjs create mode 100644 packages/ui/scripts/github-ci-build.mjs create mode 100644 packages/ui/scripts/release-new-version.mjs create mode 100644 packages/ui/scripts/steps/build.mjs create mode 100644 packages/ui/scripts/steps/increase-package-version.mjs create mode 100644 packages/ui/scripts/steps/release-version.mjs diff --git a/.github/workflows/build-linux.yml b/.github/workflows/build-ui.yml similarity index 84% rename from .github/workflows/build-linux.yml rename to .github/workflows/build-ui.yml index 368c00f..508fd55 100644 --- a/.github/workflows/build-linux.yml +++ b/.github/workflows/build-ui.yml @@ -32,17 +32,22 @@ jobs: build: # The type of runner that the job will run on runs-on: ubuntu-latest - + env: + PUPPETEER_SKIP_DOWNLOAD: 'true' # Steps represent a sequence of tasks that will be executed as part of the job steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v2 - - name: Install & Build + - name: Install dependencies run: | sudo apt update sudo apt install rpm -y - npm i pnpm -g + npm i pnpm@^8.6.9 -g cd packages/ui pnpm i --force - pnpm run build:linux + + - name: Build + run: | + cd packages/ui + node ./scripts/github-ci-build.mjs \ No newline at end of file diff --git a/.github/workflows/release-ui.yml b/.github/workflows/release-ui.yml new file mode 100644 index 0000000..c29c8e4 --- /dev/null +++ b/.github/workflows/release-ui.yml @@ -0,0 +1,54 @@ +name: Release UI + +on: + push: + # Sequence of patterns matched against refs/tags + tags: + - "ui-v*" # Push events to matching v*, i.e. v1.0, v20.15.10 + +jobs: + build: + env: + PUPPETEER_SKIP_DOWNLOAD: 'true' + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - name: Checkout source + uses: actions/checkout@v2 + + - name: Install dependencies + run: | + sudo apt update + sudo apt install rpm -y + npm i pnpm@^8.6.9 -g + cd packages/ui + pnpm i --force + + - name: Build + run: | + cd packages/ui + node ./scripts/github-ci-build.mjs + + - name: Create Release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: Release ${{ github.ref }} + body: TODO New Release. + draft: false + prerelease: false + + - name: Upload Release Asset + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./${{ secrets.ReleaseZipName }}.zip + asset_name: ${{ secrets.ReleaseZipName }}.zip + asset_content_type: application/zip \ No newline at end of file diff --git a/packages/ui/package.json b/packages/ui/package.json index b036088..5645285 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -24,7 +24,8 @@ "build:win": "npm run build && electron-builder --win", "build:mac": "npm run build && electron-builder --mac", "build:linux": "npm run build && electron-builder --linux", - "install": "cd ./external-node-runtime-dependencies && cross-env PUPPETEER_SKIP_DOWNLOAD=1 npm install --omit=dev" + "install": "cd ./external-node-runtime-dependencies && cross-env PUPPETEER_SKIP_DOWNLOAD=1 npm install --omit=dev", + "release": "node ./scripts/release-new-version.mjs" }, "dependencies": { "@electron-toolkit/preload": "^3.0.0", diff --git a/packages/ui/scripts/build.mjs b/packages/ui/scripts/build.mjs deleted file mode 100644 index e286fae..0000000 --- a/packages/ui/scripts/build.mjs +++ /dev/null @@ -1,26 +0,0 @@ -import childProcess from "node:child_process" -import fs from "node:fs" -import path from "node:path" -import os from "node:os" -import url from 'node:url' - -const rawCwd = process.cwd() -const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) - -const sqlitePluginDirPath = path.join(__dirname, '../../sqlite-plugin') -process.chdir(sqlitePluginDirPath) -try { - const sqlitePluginBuildProcess = childProcess.spawnSync('pnpm run build', { - stdio: ['inherit', 'inherit', 'inherit'], - shell: true - }) - process.chdir(rawCwd) - if (sqlitePluginBuildProcess.error) { - throw sqlitePluginBuildProcess.error - } -} catch(error) { - process.chdir(rawCwd) - console.error('error encouter when build sqlite plugin:') - console.error(error) - process.exit(1) -} diff --git a/packages/ui/scripts/github-ci-build.mjs b/packages/ui/scripts/github-ci-build.mjs new file mode 100644 index 0000000..1887c24 --- /dev/null +++ b/packages/ui/scripts/github-ci-build.mjs @@ -0,0 +1,3 @@ +import build from './steps/build.mjs' + +build() diff --git a/packages/ui/scripts/release-new-version.mjs b/packages/ui/scripts/release-new-version.mjs new file mode 100644 index 0000000..447cb23 --- /dev/null +++ b/packages/ui/scripts/release-new-version.mjs @@ -0,0 +1,8 @@ +import build from './steps/build.mjs' +import increasePackageVersion from './steps/increase-package-version.mjs' +import releaseVersion from './steps/release-version.mjs' +;(async () => { + await increasePackageVersion() + await build() + await releaseVersion() +})() diff --git a/packages/ui/scripts/steps/build.mjs b/packages/ui/scripts/steps/build.mjs new file mode 100644 index 0000000..1508854 --- /dev/null +++ b/packages/ui/scripts/steps/build.mjs @@ -0,0 +1,50 @@ +import childProcess from 'node:child_process' +import path from 'node:path' +import os from 'node:os' +import url from 'node:url' + +const currentOsPlatform = os.platform() +const osPlatformToBuildCommandMap = { + darwin: 'mac', + linux: 'linux', + win32: 'win' +} + +export default function build() { + const rawCwd = process.cwd() + const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) + + const sqlitePluginDirPath = path.join(__dirname, '../../sqlite-plugin') + process.chdir(sqlitePluginDirPath) + try { + const sqlitePluginBuildProcess = childProcess.spawnSync('pnpm run build', { + stdio: ['inherit', 'inherit', 'inherit'], + shell: true + }) + process.chdir(rawCwd) + if (sqlitePluginBuildProcess.error) { + throw sqlitePluginBuildProcess.error + } + } catch (error) { + process.chdir(rawCwd) + console.error('error encounter when build sqlite plugin:') + console.error(error) + process.exit(1) + } + try { + const uiBuildProcess = childProcess.spawnSync( + `pnpm run build:${osPlatformToBuildCommandMap[currentOsPlatform]}`, + { + stdio: ['inherit', 'inherit', 'inherit'], + shell: true + } + ) + if (uiBuildProcess.error) { + throw uiBuildProcess.error + } + } catch (error) { + console.error('error encounter when build ui:') + console.error(error) + process.exit(1) + } +} diff --git a/packages/ui/scripts/steps/increase-package-version.mjs b/packages/ui/scripts/steps/increase-package-version.mjs new file mode 100644 index 0000000..6441f5f --- /dev/null +++ b/packages/ui/scripts/steps/increase-package-version.mjs @@ -0,0 +1,32 @@ +import path from 'path' +import fs from 'fs' +import { execSync } from 'child_process' +import * as url from 'url' +import semver from 'semver' + +const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) + +export const PATH_TO_PACKAGE_JSON = path.join(__dirname, '../package.json') +export const PATH_TO_BUILD_INFO_JSON = path.join(__dirname, '../src/common/build-info.json') + +export const getPackageInfo = () => fs.readFileSync(PATH_TO_PACKAGE_JSON) +export const getRuntimeConfig = () => fs.readFileSync(PATH_TO_BUILD_INFO_JSON) + +/** + * @param {semver.ReleaseType} releaseType + */ +export default async function increasePackageVersion(releaseType = 'patch') { + const runtimeConfig = JSON.parse(getRuntimeConfig().toString('utf-8')) + const packageInfo = JSON.parse(getPackageInfo().toString('utf-8')) + packageInfo.version = semver.inc(packageInfo.version, releaseType) + + fs.writeFileSync(PATH_TO_PACKAGE_JSON, JSON.stringify(packageInfo, null, 2)) + + runtimeConfig.name = packageInfo.name + runtimeConfig.version = packageInfo.version + runtimeConfig.buildVersion = + typeof runtimeConfig.buildVersion === 'number' ? runtimeConfig.buildVersion + 1 : 1 + runtimeConfig.buildTime = Number(new Date()) + runtimeConfig.buildHash = execSync('git rev-parse HEAD').toString().trim() + fs.writeFileSync(PATH_TO_BUILD_INFO_JSON, JSON.stringify(runtimeConfig, null, 2)) +} diff --git a/packages/ui/scripts/steps/release-version.mjs b/packages/ui/scripts/steps/release-version.mjs new file mode 100644 index 0000000..60e055a --- /dev/null +++ b/packages/ui/scripts/steps/release-version.mjs @@ -0,0 +1,11 @@ +import { execSync } from 'child_process' +import { getPackageInfo } from './increase-package-version.mjs' + +export default async function releaseVersion() { + execSync(`git add -A`) + const packageInfo = JSON.parse(getPackageInfo().toString('utf-8')) + const tagName = `ui-v${packageInfo.version}` + execSync(`git commit -m ${tagName}`, { stdio: ['inherit', 'inherit', 'inherit'] }) + execSync(`git tag ${tagName}`, { stdio: ['inherit', 'inherit', 'inherit'] }) + execSync(`git push origin ${tagName}`, { stdio: ['inherit', 'inherit', 'inherit'] }) +}