Skip to content

SFD-136 Set up workflow to publish npm package #12

SFD-136 Set up workflow to publish npm package

SFD-136 Set up workflow to publish npm package #12

Workflow file for this run

name: Publish new package
on:
workflow_dispatch:
pull_request: #TODO - For testing, remove before final merge
env:
PACKAGE_PREFIX: scottlogic-tech-carbon-estimator-
jobs:
package:
runs-on: ubuntu-latest
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
overwrite: true #TODO - For testing, remove before final merge
- 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
release:
runs-on: ubuntu-latest
needs:
- package
#- publish #TODO - include once publish passes
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
run: |
gh release create "v${VERSION}" \
${PACKAGE_PREFIX}${VERSION}.tgz \
--draft \
--prerelease \
--generate-notes