From a365b24c674e64d36037bc7dcfd2b0acc41af8ff Mon Sep 17 00:00:00 2001 From: Adelin Niculae <43118261+adelinn@users.noreply.github.com> Date: Fri, 26 Jan 2024 14:32:57 +0200 Subject: [PATCH 1/2] chore: add publish pipeline --- .github/workflows/create-version.yml | 53 +++++++++++++++++++++++++++ .github/workflows/prepare-release.yml | 38 +++++++++++++++++++ .github/workflows/release.yml | 41 +++++++++++++++++++++ package.json | 3 +- scripts/version.cjs | 39 ++++++++++++++++++++ 5 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/create-version.yml create mode 100644 .github/workflows/prepare-release.yml create mode 100644 .github/workflows/release.yml create mode 100644 scripts/version.cjs diff --git a/.github/workflows/create-version.yml b/.github/workflows/create-version.yml new file mode 100644 index 0000000..f8241f1 --- /dev/null +++ b/.github/workflows/create-version.yml @@ -0,0 +1,53 @@ +name: Create New Version +run-name: Creating new ${{ github.event.inputs.version }} ${{ github.event.inputs.channel }} version + +on: + workflow_dispatch: + inputs: + version: + description: Version + type: choice + options: + - major + - minor + - patch + default: minor + channel: + description: Channel + type: choice + options: + - release + - beta + default: release + +env: + CI: true + +permissions: + contents: write + +jobs: + version: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + token: ${{ secrets.BCC_BOT_ACCESS_TOKEN }} + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + registry-url: 'https://registry.npmjs.org' + + - name: Set commit author to bcc-bot + run: | + git config --global user.name "bcc-bot" + git config --global user.email "84040471+bcc-bot@users.noreply.github.com" + + - name: Version new ${{ github.event.inputs.version }} version + run: npm run create-version -- ${{ github.event.inputs.version }} ${{ github.event.inputs.channel }} + + - name: Push + run: git push --follow-tags \ No newline at end of file diff --git a/.github/workflows/prepare-release.yml b/.github/workflows/prepare-release.yml new file mode 100644 index 0000000..8f6fd3d --- /dev/null +++ b/.github/workflows/prepare-release.yml @@ -0,0 +1,38 @@ +name: "Prepare release" + +on: + workflow_dispatch: + push: + tags: + - 'v*' + +env: + CI: true + +permissions: + contents: read + +jobs: + build: + permissions: + contents: write # for softprops/action-gh-release to create GitHub release + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - run: git fetch --tags -f + + - name: Resolve version + id: vars + run: | + echo "TAG_NAME=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV + + - name: Create Release + uses: softprops/action-gh-release@v1 + with: + draft: true + name: "schema-sync ${{ env.TAG_NAME }}" + tag_name: ${{ env.TAG_NAME }} + generate_release_notes: true \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..f3a7201 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,41 @@ +name: Release + +on: + release: + types: [published] + +permissions: + contents: read + +env: + CI: true + +jobs: + build: + if: contains(github.event.release.name, 'schema-sync') + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + registry-url: 'https://registry.npmjs.org' + cache: 'npm' + + - name: Install dependencies + run: npm ci + + - name: Build Extension + run: npm build + + - name: Set release channel + run: | + echo "RELEASE_CHANNEL=$(npm run release-channel --silent)" >> $GITHUB_ENV + + - name: Publish Extension + run: npm publish --tag ${{ env.RELEASE_CHANNEL }} + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} \ No newline at end of file diff --git a/package.json b/package.json index 1d0d122..378d2fd 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ "dev": "directus-extension build -w --no-minify", "link": "directus-extension link", "pre-test": "tsc -p tsconfig.test.json", - "test": "npm run pre-test && node --test dist-test/" + "test": "npm run pre-test && node --test dist-test/", + "create-version": "node ./scripts/version.cjs" }, "devDependencies": { "@directus/extensions-sdk": "10.1.11", diff --git a/scripts/version.cjs b/scripts/version.cjs new file mode 100644 index 0000000..3f2e4a7 --- /dev/null +++ b/scripts/version.cjs @@ -0,0 +1,39 @@ +const { exec } = require("node:child_process"); +const { exit } = require("node:process"); +const currentVersion = require("../package.json").version; + +let increment = process.argv[2]; +let channel = process.argv[3] ?? "release"; + +if (!increment) { + console.log("No version increment given! Exiting..."); + exit(); +} + +let update = `pre${increment}`; +if (currentVersion.includes(channel)) { + update = "prerelease"; +} + +const versionParts = /(\d+\.\d+\.\d+)-(.*)\.\d+/g.exec(currentVersion); + +// If there is a prerelease tag in the name but the channel is for public release, +// just strip the prerelease tag from the name +if (versionParts && channel == "release") { + increment = versionParts[1]; +} + +const command = `npm version ${ + channel == "release" ? increment : `${update} --preid ${channel}` +} --no-git-tag-version`; + +// Version package +exec(command, (error, newVersion) => { + if (error) console.error(error); + const tagVersion = newVersion.replace("\n", ""); + exec( + `git add . && git commit -m "schema-sync ${tagVersion}" && git tag -am ${tagVersion} "${tagVersion}"` + ); + + console.log(`Tagged new version ${tagVersion}`) +}); \ No newline at end of file From 2382c82985a455eff2d184f224ff672b68fee48b Mon Sep 17 00:00:00 2001 From: Adelin Niculae <43118261+adelinn@users.noreply.github.com> Date: Mon, 29 Jan 2024 11:30:28 +0200 Subject: [PATCH 2/2] add maintenance docs and missing script --- MAINTENANCE.md | 9 +++++++++ package.json | 3 ++- scripts/release-channel.js | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 MAINTENANCE.md create mode 100644 scripts/release-channel.js diff --git a/MAINTENANCE.md b/MAINTENANCE.md new file mode 100644 index 0000000..fa5fa36 --- /dev/null +++ b/MAINTENANCE.md @@ -0,0 +1,9 @@ +# Maintenance instructions + +## Releasing a new version + +A new version can be released by running the [Create New Version](https://github.com/bcc-code/directus-schema-sync/actions/workflows/create-version.yml) workflow from GitHub. + +This will update the version in the `package.json`, push a Git commit and tag, and create a new [release](https://github.com/bcc-code/directus-schema-sync/releases) in GitHub. + +Maintainers can publish this release, after which the new version will be pushed to npm with the `latest` tag. diff --git a/package.json b/package.json index 378d2fd..b454d3c 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "link": "directus-extension link", "pre-test": "tsc -p tsconfig.test.json", "test": "npm run pre-test && node --test dist-test/", - "create-version": "node ./scripts/version.cjs" + "create-version": "node ./scripts/version.cjs", + "release-channel": "node ./scripts/release-channel.js" }, "devDependencies": { "@directus/extensions-sdk": "10.1.11", diff --git a/scripts/release-channel.js b/scripts/release-channel.js new file mode 100644 index 0000000..70bbc90 --- /dev/null +++ b/scripts/release-channel.js @@ -0,0 +1,19 @@ +// Given a version, figure out what the release channel is so that we can publish to the correct +// channel on npm. +// Copied from https://github.com/tailwindlabs/heroicons/blob/eee05eb77af6cdedfb97f128d1998773fc984b28/scripts/release-channel.js +// +// E.g.: +// +// 1.2.3 -> latest (default) +// 0.0.0-insiders.ffaa88 -> insiders +// 4.1.0-alpha.4 -> alpha + +let version = + process.argv[2] || process.env.npm_package_version || require("../package.json").version; + +let match = /\d+\.\d+\.\d+-(.*)\.\d+/g.exec(version); +if (match) { + console.log(match[1]); +} else { + console.log("latest"); +} \ No newline at end of file