Release and Publish #16
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 and Publish | |
on: | |
workflow_dispatch: | |
inputs: | |
semver_type: | |
description: "What semver type is this release?" | |
type: choice | |
required: true | |
options: | |
- patch | |
- minor | |
prerelease: | |
description: "Pre Release" | |
type: boolean | |
required: false | |
jobs: | |
create-release: | |
if: github.event_name != 'workflow_dispatch' || github.ref == 'refs/heads/main' | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
outputs: | |
npm_package_version: ${{ steps.update_version.outputs.version_number }} | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
ssh-key: ${{secrets.DEPLOY_TO_MAIN_KEY}} | |
- uses: actions/setup-node@v4 | |
- run: npm ci | |
- run: npm run build | |
- run: npm run lint | |
- run: npm test | |
- name: Import GPG key | |
uses: crazy-max/ghaction-import-gpg@v6 | |
with: | |
gpg_private_key: ${{ secrets.GPG_SIGNING_KEY }} | |
passphrase: ${{ secrets.GPG_PASSPHRASE }} | |
git_user_signingkey: true | |
git_commit_gpgsign: true | |
- id: update_version | |
name: Update Package Version | |
env: | |
PREID: rc | |
run: | | |
echo foo > bar.txt | |
set -e | |
if [[ ${{ inputs.prerelease }} ]]; then | |
current_version=$(npm pkg get version | sed 's/"//g') | |
echo "Creating prerelease version. Current version is ${current_version}" | |
if [[ ${current_version} =~ ${PREID} ]]; then | |
echo "subsequent prerelease version" | |
# as current version is a prerelease, we can just bump the prerelease number | |
new_version=$(npm version prerelease --sign-git-tag -m "Create prererelease version %s" ) | |
else | |
echo "first prerelease version" | |
# if not a prerelease, we need to create a prerelease version | |
new_version=$(npm version --preid=${PREID} pre${{ inputs.semver_type }} --sign-git-tag -m "Create prererelease version %s" ) | |
fi | |
else | |
echo "creating new release" | |
new_version=$(npm version ${{ inputs.semver_type }} --sign-git-tag -m "Release version %s" ) | |
fi | |
echo "Created version ${new_version}" | |
git push && git push --tags | |
echo "version_number=$new_version" >> $GITHUB_OUTPUT | |
- id: pack_tar | |
run: | | |
npm run build | |
echo "tar_name=$(npm pack)" >> $GITHUB_OUTPUT | |
- uses: ncipollo/release-action@v1 | |
with: | |
allowUpdates: true | |
artifacts: "${{ steps.pack_tar.outputs.tar_name }}" | |
tag: "${{ steps.update_version.outputs.version_number }}" | |
generateReleaseNotes: true | |
prerelease: ${{ inputs.prerelease }} | |
publish-npm: | |
needs: create-release | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
# setup .npmrc file to publish to npm | |
- uses: actions/setup-node@v4 | |
with: | |
registry-url: 'https://registry.npmjs.org' | |
- run: npm ci | |
- run: npm run compile | |
# Publish to npm | |
- run: | | |
if [[ ${{ inputs.prerelease }} == 'true' ]]; then | |
npm publish --access public --dry-run | |
else | |
npm publish --access public | |
fi | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.PUBLISH_NPM_TOKEN }} | |
publish-gpr: | |
needs: create-release | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-node@v4 | |
with: | |
registry-url: https://npm.pkg.github.com/ | |
scope: '@emandm' | |
# Publish to GitHub Packages | |
- run: npm ci | |
- run: npm run compile | |
- name: Update Package Name and Registry, and Publish | |
run: | | |
sed -i 's,"name": "ts-mock-imports","name": "@emandm/ts-mock-imports",' package.json | |
cat package.json | |
echo registry=https://npm.pkg.github.com/emandm >> .npmrc | |
if [[ ${{ inputs.prerelease }} == 'true' ]]; then | |
npm publish --dry-run | |
else | |
npm publish | |
fi | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
external_test: | |
runs-on: ubuntu-latest | |
needs: [create-release, publish-npm] | |
if: ${{ !inputs.prerelease }} | |
steps: | |
- uses: actions/setup-node@v4 | |
- uses: actions/checkout@v4 | |
with: | |
repository: EmandM/import-test | |
- run: npm ci | |
- run: npm uninstall ts-mock-imports # Ensure no contamination from existing version | |
- run: npm install ts-mock-imports@${{ needs.create-release.outputs.npm_package_version }} | |
- run: npm test |