codekie/openapi-examples-validator

View on GitHub
.github/workflows/test-and-deploy-docker.yml

Summary

Maintainability
Test Coverage
name: Run tests

on: [push]

env:
  DOCKER_IMAGE: codekie/openapi-examples-validator
  DOCKER_IMAGE_TEST: codekie/openapi-examples-validator:test
  PATH_DOCKERFILE: dist/Dockerfile
  PATH_IMAGE_ARTIFACT: /tmp/myimage.tar

jobs:
  install:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16.x, 18.x, 20.x]
    steps:
      - uses: actions/checkout@v2
      - name: Prepare NodeJS environment
        uses: ./.github/actions/setup-js
        with:
          node-version: ${{ matrix.node-version }}

  build:
    needs: install
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16.x, 18.x, 20.x]
    steps:
      - uses: actions/checkout@v2
      - name: Prepare NodeJS environment
        uses: ./.github/actions/setup-js
        with:
          node-version: ${{ matrix.node-version }}
      - name: Build
        run: npm run build
      - name: Create build-artifact
        uses: actions/upload-artifact@v2
        with:
          name: build-${{ matrix.node-version }}
          path: ./dist/
          retention-days: 1

  test:
    needs: install
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16.x, 18.x, 20.x]
    steps:
      - uses: actions/checkout@v2
      - name: Prepare NodeJS environment
        uses: ./.github/actions/setup-js
        with:
          node-version: ${{ matrix.node-version }}
      - name: Build and run tests with coverage
        run: npm run coverage
      - name: Coveralls Parallel
        uses: coverallsapp/github-action@v1.1.2
        with:
          github-token: ${{ secrets.github_token }}
          flag-name: run-${{ matrix.node-version }}
          parallel: true

  finish-send-coveralls:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - name: Push coverage to coveralls
        uses: coverallsapp/github-action@v1.1.2
        with:
          github-token: ${{ secrets.github_token }}
          parallel-finished: true

  test-mutations:
    needs: build
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16.x, 18.x, 20.x]
    env:
      STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
    steps:
      - uses: actions/checkout@v2
      - name: Prepare NodeJS environment
        uses: ./.github/actions/setup-js
        with:
          node-version: ${{ matrix.node-version }}
      - name: Download build-artifacts
        uses: actions/download-artifact@v2
        with:
          name: build-${{ matrix.node-version }}
          path: ./dist/
      - name: Run mutation tests
        run: npm run test-mutations

  # Build image and create artifact
  docker-build:
    if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')
    needs:
      - test
    runs-on: ubuntu-latest
    steps:
      # An explicit checkout is required, due to the `file`- and `context`-option, in the "Build and export to Docker"
      # See: https://github.com/docker/build-push-action/issues/51#issuecomment-724662235
      - uses: actions/checkout@v2
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      - name: Build and export to Docker
        uses: docker/build-push-action@v2
        with:
          context: .
          load: true
          file: ${{ env.PATH_DOCKERFILE }}
          tags: ${{ env.DOCKER_IMAGE_TEST }}
          outputs: type=docker,dest=${{ env.PATH_IMAGE_ARTIFACT }}
      # Upload docker-image as artifact, so it can be used in succeeding jobs
      - name: Upload artifact
        uses: actions/upload-artifact@v2
        with:
          name: myimage
          path: ${{ env.PATH_IMAGE_ARTIFACT }}

  # Test built image
  docker-test:
    needs:
      - docker-build
    runs-on: ubuntu-latest
    steps:
      # Get the docker-image from the artifacts and load it
      - name: Download artifact
        uses: actions/download-artifact@v2
        with:
          name: myimage
          path: /tmp
      - name: Load image
        run: |
          docker load --input ${{ env.PATH_IMAGE_ARTIFACT }}
          docker image ls -a
      # Run the container
      - name: Test
        run: docker run --rm ${{ env.DOCKER_IMAGE_TEST }}

  # Push image `latest` for every push on the default-branch and for every tag
  docker-push-image:
    needs:
      - docker-test
    runs-on: ubuntu-latest
    steps:
      # An explicit checkout is required, due to the `file`- and `context`-option, in the "Build and export to Docker"
      # See: https://github.com/docker/build-push-action/issues/51#issuecomment-724662235
      - uses: actions/checkout@v2
      # Get the docker-image from the artifacts and load it (as cache for the builds)
      - name: Download artifact
        uses: actions/download-artifact@v2
        with:
          name: myimage
          path: /tmp
      - name: Load image
        run: |
          docker load --input ${{ env.PATH_IMAGE_ARTIFACT }}
          docker image ls -a
      # Prepare tags, based on the metadata of the git reference
      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: ${{ env.DOCKER_IMAGE }}
          # generate Docker tags based on the following events/attributes
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}
      # Log in to DockerHub
      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      # Prepare Docker Buildx
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      # Push, based on tags
      - name: Build and push for tags
        if: startsWith(github.ref, 'refs/tags/')
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ${{ env.PATH_DOCKERFILE }}
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
      # Push `latest`, based on pushes on the default-branch
      - name: Build and push for "latest"
        if: github.ref == 'refs/heads/main'
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ${{ env.PATH_DOCKERFILE }}
          push: true
          tags: ${{ env.DOCKER_IMAGE }}:latest
          labels: ${{ steps.meta.outputs.labels }}