Merge pull request #106 from sambauers/dependabot/npm_and_yarn/jest-2… #112
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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@v2 | |
with: | |
version: 8.7.4 | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
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@v6 | |
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@v6 | |
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}`) |