octomation/go-service

View on GitHub
.github/workflows/ci.yml

Summary

Maintainability
Test Coverage
name: Continuous integration

on:
  pull_request:
    branches: [ main ]
    paths:
      - '.github/workflows/ci.yml'
      - '.golangci.yml'
      - '**.go'
      - 'go.{mod,sum}'
      - 'Makefile'
      - 'Taskfile'

  push:
    branches: [ main ]
    paths:
      - '.github/workflows/ci.yml'
      - '.golangci.yml'
      - '**.go'
      - 'go.{mod,sum}'
      - 'Makefile'
      - 'Taskfile'
    tags: [ 'v*' ]

  schedule:
    - cron: 0 7 1 * * # at 07:00 on day-of-month 1, UTC

  workflow_dispatch:
    inputs:
      reason:
        description: The reason for dispatching it manually.
        type: string
        default: manual healthcheck
        required: true

jobs:
  lint:
    name: Linting
    runs-on: ubuntu-latest

    steps:
      - name: Checkout the repository
        uses: actions/checkout@v4.1.1
        with: { fetch-depth: 0 }
      - name: Set up Go environment
        uses: actions/setup-go@v5.0.0
        with:
          go-version: 1.21.x
          check-latest: true

      - name: Run linter
        uses: golangci/golangci-lint-action@v3.7.0

  test:
    name: Testing
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        go:
          - 1.x
          - 1.20.x
          - 1.21.x

    steps:
      - name: Checkout the repository
        uses: actions/checkout@v4.1.1
        with: { fetch-depth: 0 }
      - name: Set up Go environment
        uses: actions/setup-go@v5.0.0
        with:
          go-version: '${{ matrix.go }}'
          check-latest: true
      - name: Set up environment
        run: make env deps

      - name: Run fast check source code
        run: make go-check
      - name: Run tests
        run: make test
        if: matrix.go != '1.21.x'
      - name: Run tests with coverage report
        run: make test-with-coverage
        if: matrix.go == '1.21.x'
      - name: Store code coverage report
        uses: actions/upload-artifact@v4.0.0
        with: { name: code-coverage-report, path: c.out }
        if: matrix.go == '1.21.x'

      - name: Check installation
        run: |
          make client install
          make server install
          [ $(ls bin/linux/*/* | wc -l) = 2 ]

  publish:
    name: Documentation
    needs: [ test ]
    uses: ./.github/workflows/cd.docs.yml
    permissions:
      id-token: write
      pages: write
    if: true

  release:
    name: Distribution
    needs: [ test ]
    uses: ./.github/workflows/cd.dist.yml
    secrets:
      GORELEASER_TOKEN: ${{ secrets.GORELEASER_TOKEN }}
    if: startsWith(github.ref, 'refs/tags/v')

  report:
    name: Reporting
    needs: [ test ]
    runs-on: ubuntu-latest

    steps:
      - name: Fetch code coverage report
        uses: actions/download-artifact@v4.1.0
        with: { name: code-coverage-report }
      - name: Send code coverage report to Codecov
        uses: codecov/codecov-action@v3.1.4
        with: { files: c.out }

  notify:
    name: Notifying
    needs: [ lint, report ]
    runs-on: ubuntu-latest
    if: github.event_name != 'pull_request'
      && (failure() || success())

    steps:
      - name: Checkout the repository
        uses: actions/checkout@v4.1.1
        with: { fetch-depth: 0 }

      - name: Send notification
        uses: ./.github/actions/notify
        with:
          emoji: ⚙️
          channel: ${{ secrets.SLACK_WEBHOOK }}
          success: ${{ ! contains(needs.*.result, 'failure') }}