From 7a07bc2ec9a9b70cf22fbbb13aaaeeb5aca97b7f Mon Sep 17 00:00:00 2001 From: nichlaes <44668005+nichlaes@users.noreply.github.com> Date: Thu, 31 Oct 2024 16:14:28 +0100 Subject: [PATCH] Github Action publish to npm for libms (#1024) Publishes libms npm package to both github and npmjs.com --- .github/workflows/client.yml | 2 - .github/workflows/docs.yml | 2 - .github/workflows/lib-ms.yml | 77 +++++++++++++++---------------- .github/workflows/publish-npm.yml | 58 +++++++++++++++++++++++ .github/workflows/runner.yml | 2 - docs/developer/github-actions.md | 3 +- servers/lib/package.json | 5 +- 7 files changed, 101 insertions(+), 48 deletions(-) create mode 100644 .github/workflows/publish-npm.yml diff --git a/.github/workflows/client.yml b/.github/workflows/client.yml index 265cb00c2..971fa99d7 100644 --- a/.github/workflows/client.yml +++ b/.github/workflows/client.yml @@ -8,8 +8,6 @@ on: paths: - 'client/**' workflow_dispatch: - paths: - - 'client/**' jobs: client: diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 5fbbdfad2..9746da98d 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -8,8 +8,6 @@ on: paths: - 'docs/**' workflow_dispatch: - paths: - - 'docs/**' jobs: docs: diff --git a/.github/workflows/lib-ms.yml b/.github/workflows/lib-ms.yml index 7c0dc0df6..5c85714b4 100644 --- a/.github/workflows/lib-ms.yml +++ b/.github/workflows/lib-ms.yml @@ -9,6 +9,9 @@ on: - 'servers/lib/**' workflow_dispatch: +env: + NODE_VERSION: 20 + jobs: test-lib-ms: name: Test library microservice @@ -27,7 +30,7 @@ jobs: - name: Setup node uses: actions/setup-node@v4 with: - node-version: 20 + node-version: ${{ env.NODE_VERSION }} cache: "yarn" cache-dependency-path: "**/yarn.lock" @@ -108,44 +111,8 @@ jobs: files: servers/lib/coverage/clover.xml flags: lib-microservice-tests - publish-npm-package: - if: | - github.event_name == 'push' && - (startsWith(github.ref, 'refs/heads/feature/') || startsWith(github.ref, 'refs/heads/release-v')) - - name: Publish NPM Package to GitHub Packages - runs-on: ubuntu-latest - needs: test-lib-ms - permissions: - packages: write - contents: read - - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Setup node - uses: actions/setup-node@v4 - with: - node-version: 20 - cache: "yarn" - cache-dependency-path: "**/yarn.lock" - always-auth: true - registry-url: 'https://npm.pkg.github.com/' - - - name: Publish npm package - run: | - # copy README.md to project root - cp servers/lib/README.md . - cd servers/lib - yarn install - yarn build - yarn publish --access public - env: - NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - get_version: - name: Get version + name: Get version and variables runs-on: ubuntu-latest steps: - name: Checkout Repository @@ -157,8 +124,40 @@ jobs: run: | version=`cat ./servers/lib/package.json | jq -r '.version'` echo "version=$version" >> $GITHUB_OUTPUT + echo "node-version=$NODE_VERSION" >> $GITHUB_OUTPUT outputs: version: ${{ steps.get_version.outputs.version }} + node-version: ${{ steps.get_version.outputs.node-version }} + + publish-package-github: + if: | + (startsWith(github.ref, 'refs/heads/feature/') || startsWith(github.ref, 'refs/heads/release-v')) + + name: Publish to GitHub Packages + needs: [get_version,test-lib-ms] + uses: ./.github/workflows/publish-npm.yml + with: + node-version: ${{ needs.get_version.outputs.node-version }} + registry-url: 'https://npm.pkg.github.com/' + directory: servers/lib + package-name: ${{ vars.PACKAGE_NAME || '' }} + secrets: + NPM_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + publish-package-npm: + if: | + (startsWith(github.ref, 'refs/heads/feature/') || startsWith(github.ref, 'refs/heads/release-v')) + + name: Publish to NPM + needs: [get_version,test-lib-ms] + uses: ./.github/workflows/publish-npm.yml + with: + node-version: ${{ needs.get_version.outputs.node-version }} + registry-url: 'https://registry.npmjs.org' + directory: servers/lib + package-name: ${{ vars.PACKAGE_NAME || '' }} + secrets: + NPM_TOKEN: ${{ secrets.NPM_TOKEN}} publish-docker-image-ghcr: if: | @@ -190,4 +189,4 @@ jobs: secrets: DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} DOCKERHUB_SCOPE: ${{ secrets.DOCKERHUB_SCOPE }} - DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/publish-npm.yml b/.github/workflows/publish-npm.yml new file mode 100644 index 000000000..b5133a684 --- /dev/null +++ b/.github/workflows/publish-npm.yml @@ -0,0 +1,58 @@ +name: Test and Publish + +on: + workflow_call: + inputs: + registry-url: + required: true + type: string + directory: + required: true + type: string + node-version: + required: true + type: string + package-name: + default: '' + type: string + secrets: + NPM_TOKEN: + required: true + +jobs: + publish-package: + runs-on: ubuntu-latest + permissions: + packages: write + contents: read + defaults: + run: + working-directory: ${{ inputs.directory }} + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup node + uses: actions/setup-node@v4 + with: + node-version: ${{ inputs.node-version }} + always-auth: true + registry-url: ${{ inputs.registry-url }} + + - name: Rename package name in package.json + if: ${{ inputs.package-name != '' }} + run: | + sudo apt-get install -y jq + jq '.name = "${{inputs.package-name}}"' package.json > temp.json + mv temp.json package.json + + - name: Publish npm package + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + run: | + yarn install + yarn build + yarn publish --access public + + diff --git a/.github/workflows/runner.yml b/.github/workflows/runner.yml index fbfe22377..efb004b66 100644 --- a/.github/workflows/runner.yml +++ b/.github/workflows/runner.yml @@ -8,8 +8,6 @@ on: paths: - 'servers/execution/**' workflow_dispatch: - paths: - - 'servers/execution/**' jobs: test-runner: diff --git a/docs/developer/github-actions.md b/docs/developer/github-actions.md index f8d707b51..44554673f 100644 --- a/docs/developer/github-actions.md +++ b/docs/developer/github-actions.md @@ -8,7 +8,8 @@ from [docker hub](hub.docker.com): | `DOCKERHUB_SCOPE` | Username or organization name on docker hub | | `DOCKERHUB_USERNAME` | Username on docker hub | | `DOCKERHUB_TOKEN` | API token to publish images to docker hub, with `Read`, `Write` and `Delete` permissions | +| `NPM_TOKEN` | Token to publish npm package to Nodejs registry. | Remember to add these secrets to [Github Secrets Setting](https://docs.github.com/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository) -of your fork. +of your fork. \ No newline at end of file diff --git a/servers/lib/package.json b/servers/lib/package.json index 08d389aec..d925ac4fa 100644 --- a/servers/lib/package.json +++ b/servers/lib/package.json @@ -1,12 +1,13 @@ { "name": "@into-cps-association/libms", - "version": "0.4.6", + "version": "0.4.7", "description": "microservices that handles request by fetching and returning the file-names and folders of given directory", "author": "phillip.boe.jensen@gmail.com", "contributors": [ "Prasad Talasila", "Mads Kelberg", - "Linda Nguyen" + "Linda Nguyen", + "Nichlaes H. Sørensen" ], "private": false, "license": "SEE LICENSE IN ",