name: Build and Publish Docker Image on: push: branches: [main] tags: ["v*"] pull_request: branches: [main] env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} jobs: test: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: Setup Rust toolchain uses: actions-rs/toolchain@v1 with: toolchain: 1.88 override: true components: rustfmt, clippy - name: Cache cargo registry uses: actions/cache@v3 with: path: | ~/.cargo/registry ~/.cargo/git target key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} - name: Run tests run: GITHUB_TOKEN="${{ secrets.GITHUB_TOKEN }}"cargo test --verbose - name: Run clippy run: cargo clippy -- -D warnings - name: Check formatting run: cargo fmt -- --check build-and-push: needs: test runs-on: ubuntu-latest permissions: contents: read packages: write outputs: version: ${{ steps.meta.outputs.version }} tags: ${{ steps.meta.outputs.tags }} steps: - name: Checkout repository uses: actions/checkout@v4 - name: Setup Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to Container Registry if: github.event_name != 'pull_request' uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata id: meta uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=ref,event=branch type=ref,event=pr type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}} type=sha - name: Build and push Docker image uses: docker/build-push-action@v5 with: context: . platforms: linux/amd64,linux/arm64 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: Test Docker image if: github.event_name != 'pull_request' run: | docker run --rm \ -e RUST_LOG=info \ ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} \ docker-compose-updater check security-scan: needs: build-and-push runs-on: ubuntu-latest if: github.event_name != 'pull_request' permissions: contents: read packages: read security-events: write steps: - name: Checkout repository uses: actions/checkout@v4 - name: Run Trivy vulnerability scanner uses: aquasecurity/trivy-action@master with: image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.build-and-push.outputs.version }} format: "sarif" output: "trivy-results.sarif" - name: Upload Trivy scan results to GitHub Security tab uses: github/codeql-action/upload-sarif@v2 with: sarif_file: "trivy-results.sarif"