Update README.md #14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: ci | |
on: | |
push: | |
branches: [ "main", "develop" ] | |
tags: [ "v*.*.*" ] | |
pull_request: | |
branches: [ "main", "develop" ] | |
workflow_dispatch: | |
permissions: | |
contents: read | |
security-events: write | |
env: | |
REGISTRY: docker.io | |
IMAGE_NAME: snmp-olt-zte-c320 | |
GO_VERSION: '1.25' | |
jobs: | |
# ======================== | |
# Job 1: Lint, Test & Govulncheck | |
# ======================== | |
test: | |
name: Lint, Test & Govulncheck | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Go | |
uses: actions/setup-go@v4 | |
with: | |
go-version: ${{ env.GO_VERSION }} | |
cache: true | |
cache-dependency-path: go.sum | |
- name: Download Go modules | |
run: go mod download | |
- name: Run Linter | |
uses: golangci/golangci-lint-action@v3 | |
with: | |
version: v1.64.8 | |
args: --timeout=5m --verbose | |
- name: Run Govulncheck | |
run: | | |
go install golang.org/x/vuln/cmd/govulncheck@latest | |
govulncheck ./... | |
- name: Run Unit Tests with Coverage | |
run: go test -v -race -coverprofile=coverage.out -count=1 ./... | |
- name: Upload Coverage Report to Codecov | |
uses: codecov/codecov-action@v3 | |
with: | |
token: ${{ secrets.CODECOV_TOKEN }} | |
files: ./coverage.out | |
# ======================== | |
# Job 2: Build & Push Docker Image | |
# ======================== | |
build-and-push: | |
name: Build & Push Docker Image | |
runs-on: ubuntu-latest | |
needs: test | |
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop' || startsWith(github.ref, 'refs/tags/v')) | |
permissions: | |
contents: read | |
packages: write | |
security-events: write | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Extract metadata for Docker | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/${{ secrets.DOCKERHUB_USERNAME }}/${{ env.IMAGE_NAME }} | |
tags: | | |
type=raw,value=latest,enable={{is_default_branch}} | |
type=raw,value=develop,enable=${{ github.ref == 'refs/heads/develop' }} | |
type=semver,pattern={{version}} | |
type=semver,pattern={{major}}.{{minor}} | |
type=raw,value=1.0.0 | |
- name: Build & Push Docker Image | |
id: build | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
platforms: linux/amd64,linux/arm64,linux/arm/v7 | |
push: ${{ github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
- name: Scan Image with Trivy | |
uses: aquasecurity/trivy-action@master | |
with: | |
image-ref: ${{ env.REGISTRY }}/${{ secrets.DOCKERHUB_USERNAME }}/${{ env.IMAGE_NAME }}:develop | |
format: 'sarif' | |
output: 'trivy-results.sarif' | |
ignore-unfixed: true | |
exit-code: 0 | |
- name: Check if trivy results file exists | |
run: | | |
if [ -f "trivy-results.sarif" ]; then | |
echo "trivy-results.sarif file exists" | |
ls -la trivy-results.sarif | |
else | |
echo "trivy-results.sarif file does not exist" | |
# Create empty file to prevent upload error | |
echo '{"version": "2.1.0","$schema": "https://json.schemastore.org/sarif-2.1.0.json","runs": []}' > trivy-results.sarif | |
fi | |
- name: Upload Vulnerability Report | |
uses: github/codeql-action/upload-sarif@v3 | |
if: always() && hashFiles('trivy-results.sarif') != '' | |
with: | |
sarif_file: 'trivy-results.sarif' |