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:

env:
  NODE_VERSION: 20

jobs:
  test-lib-ms:
    name: Test library microservice
    strategy:
      matrix:
        os: [windows-latest, ubuntu-latest]
    runs-on: ${{ matrix.os }}
    defaults:
      run:
        working-directory: servers/lib

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup node
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          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 and formatting
        run: |
          yarn install
          yarn syntax
          yarn format

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

      - name: Run all tests
        env:
          PORT: 4001
          LOCAL_PATH: ${{ github.workspace }}/files
          MODE: local
          LOG_LEVEL: debug
          APOLLO_PATH: /lib
        run: |
          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 build
          yarn start:pm2
      
      - name: Check server is running (Windows)
        if: runner.os == 'Windows'
        env:
          PORT: 4003
        uses: nick-fields/retry@v3
        with:
          timeout_seconds: 10
          max_attempts: 4
          command: "Invoke-WebRequest -Uri http://localhost:${{ env.PORT }}/lib/files -Method Head -UseBasicParsing"

      - name: Check server is running (Linux)
        if: runner.os != 'Windows'
        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

  get_version:
    name: Get version and variables
    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
            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: | 
      github.event_name == 'push' &&
      (startsWith(github.ref, 'refs/heads/feature/') || startsWith(github.ref, 'refs/heads/release-v'))

    name: Publish Docker image (GHCR)
    needs: [get_version, test-lib-ms]
    uses: ./.github/workflows/docker-ghcr.yml
    with: 
      image-name: libms
      version: ${{ needs.get_version.outputs.version }}
      dockerfile: libms.dockerfile
    secrets: inherit

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

    name: Publish Docker image (DockerHub)
    needs: [get_version, test-lib-ms]
    uses: ./.github/workflows/docker-dockerhub.yml
    with: 
      image-name: libms
      version: ${{ needs.get_version.outputs.version }}
      dockerfile: libms.dockerfile
      readme-file: servers/lib/DOCKER.md
    secrets:
      DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
      DOCKERHUB_SCOPE: ${{ secrets.DOCKERHUB_SCOPE }}
      DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}