Skip to content

Commit

Permalink
Github Action publish to npm for libms (#1024)
Browse files Browse the repository at this point in the history
Publishes libms npm package to both github and npmjs.com
  • Loading branch information
nichlaes authored Oct 31, 2024
1 parent d7afd83 commit 7a07bc2
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 48 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ on:
paths:
- 'client/**'
workflow_dispatch:
paths:
- 'client/**'

jobs:
client:
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ on:
paths:
- 'docs/**'
workflow_dispatch:
paths:
- 'docs/**'

jobs:
docs:
Expand Down
77 changes: 38 additions & 39 deletions .github/workflows/lib-ms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ on:
- 'servers/lib/**'
workflow_dispatch:

env:
NODE_VERSION: 20

jobs:
test-lib-ms:
name: Test library microservice
Expand All @@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -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: |
Expand Down Expand Up @@ -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 }}
58 changes: 58 additions & 0 deletions .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions .github/workflows/runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ on:
paths:
- 'servers/execution/**'
workflow_dispatch:
paths:
- 'servers/execution/**'

jobs:
test-runner:
Expand Down
3 changes: 2 additions & 1 deletion docs/developer/github-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
5 changes: 3 additions & 2 deletions servers/lib/package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"contributors": [
"Prasad Talasila",
"Mads Kelberg",
"Linda Nguyen"
"Linda Nguyen",
"Nichlaes H. Sørensen"
],
"private": false,
"license": "SEE LICENSE IN <LICENSE.md>",
Expand Down

0 comments on commit 7a07bc2

Please sign in to comment.