QutBioacoustics/baw-server

View on GitHub
.github/workflows/ci.yml

Summary

Maintainability
Test Coverage
name: Run tests, publish docker image

on:
  push:
    paths-ignore:
      - "swagger/**"
  pull_request:
    paths-ignore:
      - "swagger/**"

env:
  # TODO: Change variable to your image's name.
  IMAGE_NAME: workbench-server
  DOCKER_BUILDKIT: 1


jobs:
  # blocked by https://github.com/docker/build-push-action/issues/493
  # # Build the image and cache it for later use
  # build: 
  #   runs-on: ubuntu-latest

  #   steps:
  #     - name: Checkout
  #       uses: actions/checkout@v2
  #       with:
  #         lfs: true

  #     - name: Ensure buildkit is used
  #       uses: docker/setup-buildx-action@v1
  #       id: buildx
  #       with:
  #         install: true
      
  #     - name: Build and cache
  #       uses: docker/build-push-action@v2
  #       with:
  #         context: .
  #         push: false
  #         cache-from: type=gha
  #         cache-to: type=gha,mode=max
      

  # Run tests.
  # See also https://docs.docker.com/docker-hub/builds/automated-testing/
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        with:
          lfs: true

      - uses: docker/setup-buildx-action@v1
        id: buildx
        with:
          install: true


      # by default the user on GH seems to have the UID 1001
      # however our docker container expects a uid of 1000.
      # This is not an issue in Docker since we chown files when they are added.
      # However when docker compose bind mounts the volumes for development
      # the permissions of the files reflect those that are on the host, in the
      # GH action case uid 1001 and gid 116.
      # The easiest way I can think of to get this working is to just set the
      # uid to 1000.
      # - name: Set permissions for checked out files
      #   run: sudo chown -R 1000 .
      # there is a bug where docker build and docker compose don't produce the
      # same results
      # see https://github.com/docker/compose/issues/883
      # So we use docker-compose build instead
      - name: Build image
        #run: docker build .
        run: docker compose build


      - name: Start docker compose
        run: docker compose up --detach --wait --timeout 60

      - name: Debugging
        run: |
          ls -la
          docker compose ps
          docker ps
        if: ${{ always() }}

      - name: prepare test database
        run: docker compose run web echo 'database created'
        env:
          RAILS_ENV: test

      - name: Run tests
        run: docker compose run web rspec --format progress --format html --out rspec_results.html
        env:
          RAILS_ENV: test

      - name: Upload test results
        uses: actions/upload-artifact@v2
        if: ${{ always() }}
        with:
          name: rspec test results
          path: rspec_results.html

      - name: Upload test logs
        uses: actions/upload-artifact@v2
        if: ${{ always() }}
        with:
          name: rspec test results
          path: log/*test.log

      # update api docs
      - name: Generate API docs
        run: docker compose run web generate_docs.sh

      - name: Stop docker compose
        if: ${{ always() }}
        run: docker compose stop

      # Undo permissions change
      # - name: Undo Set permissions for checked out files
        # run: sudo chown -R $(id -u) .

      - name: Commit swagger doc changes
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: |
            Update API docs

            [skip_ci]
          file_pattern: swagger/**

  # Push image to GitHub Packages.
  # See also https://docs.docker.com/docker-hub/builds/
  push:
    # Ensure test job passes before pushing image.
    needs: test

    runs-on: ubuntu-latest
    if: github.event_name == 'push' && (github.ref == 'refs/heads/master' || contains(github.ref, 'refs/tag'))

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 200

      - name: Calculate git describe
        id: tagger
        shell: pwsh
        # the `@()` forces array output (even for single line) and the `[-1]` selects the last line
        run: |
          $result = @( git fetch --prune && git describe 'HEAD~' )
          $last_line = $result[-1]
          if ([string]::IsNullOrWhiteSpace($last_line)) {
            Write-Output "failed to get git describe, result was: $result"
            exit 1
          }
          echo "::set-output name=tag::$last_line"

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: atruskie
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v2
        with:
          build-args: |
            trimmed=true
            version=${{steps.tagger.outputs.tag}}
          context: .
          push: true
          tags: user/app:latest
          labels: |
             version:${{steps.tagger.outputs.tag}}

      - name: Build and push Docker image
        uses: docker/build-push-action@v1.1.0
        with:
          # Username used to log in to a Docker registry. If not set then no login will occur
          username: atruskie
          # Password or personal access token used to log in to a Docker registry. If not set then no login will occur
          password: ${{ secrets.DOCKER_HUB_TOKEN }}
          # Server address of Docker registry. If not set then will default to Docker Hub
          #registry: # optional
          # Docker repository to tag the image with
          repository: qutecoacoustics/workbench-server
          # Comma-delimited list of tags. These will be added to the registry/repository to form the image's tags
          tags: "${{steps.tagger.outputs.tag}}"
          # Automatically tags the built image with the git reference as per the readme
          tag_with_ref: true
          # Automatically tags the built image with the git short SHA as per the readme
          #tag_with_sha: true
          # Path to the build context
          path: .
          # Path to the Dockerfile (Default is '{path}/Dockerfile')
          #dockerfile: # optional
          # Sets the target stage to build
          #target:
          # Always attempt to pull a newer version of the image
          always_pull: true
          # Comma-delimited list of build-time variables
          build_args: "version=${{steps.tagger.outputs.tag}},trimmed=true"
          # Comma-delimited list of images to consider as cache sources
          #cache_froms: # optional
          # Comma-delimited list of labels to add to the built image
          labels: "version=${{steps.tagger.outputs.tag}}"
          # Adds labels with git repository information to the built image
          add_git_labels: true
          # Whether to push the image
          push: true