name: CI/CD Pipeline on: push: branches: [ main ] tags: [ 'v*' ] pull_request: branches: [ main ] jobs: test: name: Test runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Go uses: actions/setup-go@v5 with: go-version: '1.21' - name: Run tests run: go test ./... -v -race -coverprofile=coverage.txt -covermode=atomic - name: Check code coverage run: | total=$(go tool cover -func=coverage.txt | grep total | awk '{print $3}' | sed 's/%//') echo "Total coverage: $total%" if (( $(echo "$total < 50" | bc -l) )); then echo "Error: Code coverage is below 50%" exit 1 fi lint: name: Lint runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Go uses: actions/setup-go@v5 with: go-version: '1.21' - name: Run go vet run: go vet ./... - name: Run go fmt run: | if [ -n "$(gofmt -s -l .)" ]; then echo "Go code is not formatted:" gofmt -s -d . exit 1 fi build: name: Build and Push Docker Image runs-on: ubuntu-latest needs: [test, lint] if: github.event_name == 'push' permissions: contents: read packages: write steps: - uses: actions/checkout@v4 - name: Login to GitHub Container Registry uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata id: meta uses: docker/metadata-action@v5 with: images: ghcr.io/${{ github.repository }} tags: | type=ref,event=branch type=ref,event=tag type=sha type=raw,value=latest,enable={{is_default_branch}} - name: Build and push uses: docker/build-push-action@v5 with: context: . push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }}