Skip to content

chore(release): v1.1.0 (#48) #21

chore(release): v1.1.0 (#48)

chore(release): v1.1.0 (#48) #21

name: Release Creation on GitHub & NPM Publishing
on:
push:
branches:
- main
workflow_dispatch: {}
jobs:
check_conditions:
name: Check conditions to create & publish a release
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
success: ${{ steps.check_workflow_conditions.outputs.success }}
appVersion: ${{ steps.check_workflow_conditions.outputs.appVersion }}
steps:
- name: Checkout the Codebase
uses: actions/checkout@v4
with:
# ref: main # branch to checkout, but we use the provided branch by the event
fetch-depth: 0
# - name: Get `package.json` version
# id: get_package_version
# run: echo appVersion=$(node -p "require('./package.json').version") >> $GITHUB_OUTPUT
- name: Check workflow conditions to create a release on the current commit
id: check_workflow_conditions
uses: actions/github-script@v7
with:
script: |
const fs = require('fs').promises
const versionRegex = "\\d+\\.\\d+\\.\\d+(-rc\\.\\d+)?" // allowed: `1.0.0`, `1.0.0-rc.1`
// set default output that may be overwritten later
setOutput("success", 0) // default: false
// helper function to set output with logging
function setOutput(key, value) {
console.dir(`:: set output: ${key}=${value}`)
core.setOutput(key, value)
}
// ==== commit message ====
// Get first line of last commit message
const lastCommitMessage = context?.payload?.head_commit.message ?? context?.payload?.commits[0]?.message
const lastCommitMessageFirstLine = lastCommitMessage?.split('\n')[0] ?? ''
const regex_commitMessage = new RegExp(`^chore\\(release\\): v(?<version>${versionRegex}) \\(#\\d+\\)$`, "s")
const match_commitMessage = lastCommitMessageFirstLine.match(regex_commitMessage)
if (!match_commitMessage || !match_commitMessage.groups?.version) {
console.dir(`Invalid commit message: ${lastCommitMessageFirstLine}`)
return
}
const version_byCommitMessage = match_commitMessage.groups.version
// ==== version in `package.json` ====
const packageJson = await fs.readFile('package.json', 'utf8')
const { version: version_byPackageJson } = JSON.parse(packageJson)
const regex_packageJson = new RegExp(`^(${versionRegex})$`, "s")
const match_packageJson = version_byPackageJson.match(regex_packageJson)
if (!match_packageJson) {
console.dir(`Invalid version in package.json: ${version_byPackageJson}`)
return
}
// ==== compare all versions ====
if (version_byCommitMessage !== version_byPackageJson) {
console.dir(`Versions are not equal: ${version_byCommitMessage} (commit message) !== ${version_byPackageJson} (package.json)`)
return
}
// the versions are equal, so just take one of them
const appVersion = version_byCommitMessage
// ==== set outputs ====
setOutput("appVersion", appVersion)
setOutput("success", 1)
dependence-lint:
name: Depend on Lint
uses: ./.github/workflows/lint.yml
create_release:
needs:
- check_conditions
- dependence-lint
# only run if the conditions are met, and bc booleans are not supported, we use integers (false=0, true=1)
if: needs.check_conditions.outputs.success == 1
name: Create GitHub Release & NPM Publish
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout the Codebase
uses: actions/checkout@v4
with:
# ref: main # branch to checkout, but the workflow already only runs on `main`
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 22
- name: Enable pnpm
run: |
corepack enable
corepack prepare [email protected] --activate
- name: Init git config for the bot by setting name & email
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "[email protected]"
# do all code related stuff early to catch if something is wrong and it fails
# TODO create a workflow like `Lint` to test install by lockfile and build
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Build the project
run: pnpm run build
- name: Create a tag
run: |
git tag -a "v${{ needs.check_conditions.outputs.appVersion }}" -m "Release v${{ needs.check_conditions.outputs.appVersion }}"
git push origin --tags
- name: Sync tags with GitHub releases & create the new release
# run: pnpm changelogen gh release all
run: pnpm changelogen gh release "v${{ needs.check_conditions.outputs.appVersion }}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# # pack the project in a archive for publishing, e.g. `todde.tv-gltf-type-toolkit-1.0.0.tgz`
# - name: Pack the project
# run: pnpm pack
# - name: Publish package to NPM
# if: github.ref == 'refs/heads/main'
# run: pnpm publish --access public
# env:
# NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Publish package to NPM
# if: github.ref == 'refs/heads/main'
uses: JS-DevTools/npm-publish@v3
with:
token: ${{ secrets.NPM_TOKEN }}
access: public
# registry: "https://npm.pkg.github.com/"
registry: https://registry.npmjs.org/