Skip to content

Merge pull request #408 from sambauers/dependabot/npm_and_yarn/typesc… #196

Merge pull request #408 from sambauers/dependabot/npm_and_yarn/typesc…

Merge pull request #408 from sambauers/dependabot/npm_and_yarn/typesc… #196

Workflow file for this run

name: Release
on:
push:
branches: [ main ]
jobs:
release:
runs-on: ubuntu-latest
steps:
# checkout
- name: Checkout repository code
uses: actions/checkout@v4
# install
- name: Setup PNPM
if:
uses: pnpm/action-setup@v4
with:
version: 9.7.0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: 'pnpm'
registry-url: 'https://registry.npmjs.org'
- name: Install dependencies
run: pnpm install --frozen-lockfile
# get latest version number
- name: Install semver package
run: pnpm add semver
- name: Retrieve the latest release and current package versions
id: get-releases
uses: actions/github-script@v7
with:
script: |
const semver = require('semver')
// get latest version
let latestVersion = 'v0.0.0'
try {
const latestRelease = await github.rest.repos.getLatestRelease({
owner: context.repo.owner,
repo: context.repo.repo,
})
latestVersion =
typeof latestRelease === 'undefined' ||
typeof latestRelease.data === 'undefined'
? 'v0.0.0'
: `v${semver.clean(latestRelease.data.tag_name)}`
console.info(`Found latest release version: ${latestVersion}`)
} catch (e) {
console.info(
'Could not evaluate latest release version, using v0.0.0'
)
}
// get version from package json
let packageVersion = 'v0.0.0'
try {
const packageJsonRecord = await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: 'package.json',
ref: context.ref,
})
const packageJson =
typeof packageJsonRecord === 'undefined' ||
typeof packageJsonRecord.data === 'undefined'
? '{ "version": "0.0.0" }'
: Buffer
.from(packageJsonRecord.data.content, 'base64')
.toString()
const package = JSON.parse(packageJson)
packageVersion = `v${semver.clean(package.version)}`
console.info(`Found current package version: ${packageVersion}`)
} catch (e) {
console.info(
'Could not evaluate current package version, using v0.0.0'
)
}
return {
package: packageVersion,
latest: latestVersion,
latestIsNew: semver.gt(packageVersion, latestVersion)
}
- name: Uninstall semver package
run: git restore . && pnpm install --frozen-lockfile
# release to npm
- name: Release to NPM
if: ${{ fromJSON(steps.get-releases.outputs.result).latestIsNew }}
run: pnpm release
env:
NODE_AUTH_TOKEN: ${{ secrets.NPMJS_ACCESS_TOKEN }}
# tag release in github
- name: Tag release in GitHub
if: ${{ fromJSON(steps.get-releases.outputs.result).latestIsNew }}
uses: actions/github-script@v7
env:
RELEASE_TAG_NAME: ${{ fromJSON(steps.get-releases.outputs.result).package }}
with:
result-encoding: string
script: |
const { RELEASE_TAG_NAME: tag_name } = process.env
await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name,
target_commitish: context.sha,
name: tag_name,
})
console.info(`Created release ${tag_name}`)