Skip to content

Instantly share code, notes, and snippets.

@nrobinaubertin
Created February 7, 2024 00:18

Revisions

  1. nrobinaubertin created this gist Feb 7, 2024.
    95 changes: 95 additions & 0 deletions go.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    name: Build and unit test

    on:
    push:
    branches: [ "main" ]
    pull_request:
    branches: [ "main" ]

    env:
    TINYGO_VERSION: '0.30.0'
    GOLANGCI_LINT_VERSION: '1.55.2'

    jobs:
    build:
    runs-on: ubuntu-latest
    strategy:
    matrix:
    go-version: ['1.21']
    steps:
    - uses: actions/checkout@v3

    - name: Cache Go modules
    uses: actions/cache@v3
    with:
    path: ~/go/pkg/mod
    key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
    restore-keys: |
    ${{ runner.os }}-go-
    - name: Set up Go
    uses: actions/setup-go@v4
    with:
    go-version: ${{ matrix.go-version }}

    - name: Build
    run: go build -v ./...

    - name: Check code formatting
    run: |
    if [ -n "$(gofmt -l .)" ]; then
    echo "Code is not formatted according to gofmt standards."
    exit 1
    fi
    - name: Install golangci-lint
    run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v${{ env.GOLANGCI_LINT_VERSION }}

    - name: Run golangci-lint
    run: golangci-lint run ./...

    - name: Test with coverage
    run: |
    go test -coverprofile=coverage.out ./...
    go tool cover -func=coverage.out -o coverage.txt
    awk '/^total:/ {print $3}' coverage.txt | sed 's/%//' | {
    read COVERAGE
    echo "Code coverage: $COVERAGE%"
    MIN_COVERAGE=20
    echo "Minimum coverage: $MIN_COVERAGE%"
    if (( $(echo "$COVERAGE < $MIN_COVERAGE" | bc -l) )); then
    echo "Coverage is below minimum."
    exit 1
    fi
    }
    - name: Install deadcode
    run: go install golang.org/x/tools/cmd/deadcode@latest

    - name: Run deadcode and fail if dead code is found
    run: |
    DEADCODE_OUTPUT=$(deadcode ./...)
    if [ -n "$DEADCODE_OUTPUT" ]; then
    echo "Dead code detected:"
    echo "$DEADCODE_OUTPUT"
    exit 1
    else
    echo "No dead code found."
    fi
    - name: Cache TinyGo
    uses: actions/cache@v3
    with:
    path: ~/tinygo
    key: ${{ runner.os }}-tinygo-${{ env.TINYGO_VERSION }}
    restore-keys: |
    ${{ runner.os }}-tinygo-
    - name: Set up TinyGo
    run: |
    wget -nv https://github.com/tinygo-org/tinygo/releases/download/v${{ env.TINYGO_VERSION }}/tinygo_${{ env.TINYGO_VERSION }}_amd64.deb
    sudo dpkg -i tinygo_${{ env.TINYGO_VERSION }}_amd64.deb
    - name: Compile to WASM with TinyGo
    run: |
    tinygo build -o main.wasm -target wasm ./...