SFD-136 Set up workflow to publish npm package #16
Workflow file for this run
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: Publish new package | |
on: | |
workflow_dispatch: | |
pull_request: | |
env: | |
PACKAGE_PREFIX: scottlogic-tech-carbon-estimator- | |
concurrency: | |
group: 'publish' | |
cancel-in-progress: false | |
jobs: | |
test: | |
uses: ./.github/workflows/pull-request.yml | |
package: | |
runs-on: ubuntu-latest | |
needs: test | |
outputs: | |
version: ${{ steps.output.outputs.version}} | |
steps: | |
- name: Check out code | |
uses: actions/checkout@v4 | |
- name: Set up environment | |
uses: ./.github/actions/set-up-environment | |
- name: Create package | |
run: | | |
npm run prepare | |
cp README.md package.json ./dist/tech-carbon-estimator/ | |
cd dist/tech-carbon-estimator | |
npm pkg delete scripts private devDependencies files | |
npm pkg set main=main.js | |
npm pack | |
VERSION=$(npm pkg get version --workspaces=false | tr -d \") | |
echo "VERSION=$VERSION" >> $GITHUB_ENV | |
- name: Upload artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
if-no-files-found: error | |
name: ${{env.PACKAGE_PREFIX}}${{env.VERSION}} | |
path: ./dist/tech-carbon-estimator/${{env.PACKAGE_PREFIX}}${{env.VERSION}}.tgz | |
- name: Output version | |
id: output | |
run: echo "version=$VERSION" >> "$GITHUB_OUTPUT" | |
publish: | |
runs-on: ubuntu-latest | |
needs: package | |
env: | |
VERSION: ${{needs.package.outputs.version}} | |
permissions: | |
id-token: write | |
steps: | |
- name: Set up Node | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 18 | |
registry-url: 'https://registry.npmjs.org' | |
- name: Download package | |
uses: actions/download-artifact@v4 | |
with: | |
name: ${{env.PACKAGE_PREFIX}}${{env.VERSION}} | |
- name: Publish to NPM registry | |
env: | |
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} | |
run: | | |
npm publish ${PACKAGE_PREFIX}${VERSION}.tgz --provenance --access public --dry-run | |
release: | |
runs-on: ubuntu-latest | |
needs: | |
- package | |
- publish | |
env: | |
VERSION: ${{needs.package.outputs.version}} | |
permissions: | |
contents: write | |
steps: | |
- name: Download package | |
uses: actions/download-artifact@v4 | |
with: | |
name: ${{env.PACKAGE_PREFIX}}${{env.VERSION}} | |
- name: Create Github Release | |
env: | |
GH_TOKEN: ${{ github.token }} | |
run: | | |
gh release create "v${VERSION}" \ | |
${PACKAGE_PREFIX}${VERSION}.tgz \ | |
--repo="$GITHUB_REPOSITORY" \ | |
--prerelease \ | |
--draft \ | |
--generate-notes |