INTO-CPS-Association/DTaaS

View on GitHub
.github/workflows/lib-ms.yml

Summary

Maintainability
Test Coverage
name: Library microservice

on:
  push:
    paths:
      - 'servers/lib/**'
  pull_request:
    paths:
      - 'servers/lib/**'
  workflow_dispatch:

jobs:
  test-lib-ms:
    name: Test library microservice
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: servers/lib

    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"

      - name: Install pm2 and jest
        run: |
          npm install -g pm2
          npm install -g jest

      - name: Run the linting checks
        run: |
          yarn install
          yarn syntax

      - name: Build the lib microservice
        run: |
          yarn install    
          yarn build

      - name: Run all tests
        env:
          PORT: 4001
          LOCAL_PATH: ${{ github.workspace }}/files
          MODE: local
          LOG_LEVEL: debug
          APOLLO_PATH: /lib
        run: |
          yarn install
          yarn build
          yarn test:all

      - name: Start http mode
        env:
          PORT: 4003
          LOCAL_PATH: ${{ github.workspace }}/files
          MODE: local
          LOG_LEVEL: debug
          APOLLO_PATH: /lib
        run: |
          yarn install
          yarn build
          yarn start:pm2
      
      - name: Check server is running
        env:
          PORT: 4003
        uses: nick-fields/retry@v3
        with:
          timeout_seconds: 10
          max_attempts: 4
          command: "curl -f -LI http://localhost:${{ env.PORT }}/lib/files"
          
      - name: Run http test
        env:
          PORT: 4003
          LOCAL_PATH: ${{ github.workspace }}/files
          MODE: local
          LOG_LEVEL: debug
          APOLLO_PATH: /lib
        run: |
          yarn test:http-github

      - name: Stop http mode
        run: |
          yarn stop:pm2

      - name: Upload test coverage to Codecov
        uses: codecov/codecov-action@v3
        with:
          files: servers/lib/coverage/clover.xml
          flags: lib-microservice-tests


  publish-package:

    if: | 
      github.event_name == 'push' &&
      (startsWith(github.ref, 'refs/heads/feature/') || startsWith(github.ref, 'refs/heads/release-v'))

    name: Publish 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
    runs-on: ubuntu-latest
    steps:
        - name: Checkout Repository
          uses: actions/checkout@v4
        - name: Install jq
          run: sudo apt-get install -y jq
        - name: get version
          id: get_version
          run: | 
            version=`cat ./servers/lib/package.json | jq -r '.version'`
            echo "version=$version" >> $GITHUB_OUTPUT
    outputs:
      version: ${{ steps.get_version.outputs.version }}

  publish-docker-image:
    name: Publish Docker image
    needs: [ publish-package, get_version]
    uses: ./.github/workflows/docker.yml
    with: 
        registry: ghcr.io
        image-name: into-cps-association/libms
        version: ${{ needs.get_version.outputs.version }}
        dockerfile: libms.dockerfile