From acfba84a352e9dd33c816e3d23ef0ee5506a3c57 Mon Sep 17 00:00:00 2001 From: valknarness Date: Sat, 25 Oct 2025 23:43:32 +0200 Subject: [PATCH] feat: more workflows --- .github/workflows/cleanup-images.yml | 42 +++++++++ .github/workflows/docker-publish.yml | 132 ++++++++++++++++++--------- .github/workflows/docker-scan.yml | 71 ++++++++++++++ 3 files changed, 200 insertions(+), 45 deletions(-) create mode 100644 .github/workflows/cleanup-images.yml create mode 100644 .github/workflows/docker-scan.yml diff --git a/.github/workflows/cleanup-images.yml b/.github/workflows/cleanup-images.yml new file mode 100644 index 0000000..4e2a3ee --- /dev/null +++ b/.github/workflows/cleanup-images.yml @@ -0,0 +1,42 @@ +name: Cleanup Old Docker Images + +on: + schedule: + # Run weekly on Sunday at 3 AM UTC + - cron: '0 3 * * 0' + workflow_dispatch: + inputs: + keep_count: + description: 'Number of recent images to keep (per tag pattern)' + required: false + default: '10' + +env: + REGISTRY: ghcr.io + IMAGE_NAME: valknarness/awesome-app + +jobs: + cleanup: + runs-on: ubuntu-latest + permissions: + packages: write + + steps: + - name: Delete old container images + uses: actions/delete-package-versions@v5 + with: + package-name: 'awesome-app' + package-type: 'container' + min-versions-to-keep: ${{ github.event.inputs.keep_count || 10 }} + delete-only-untagged-versions: 'true' + + - name: Generate cleanup summary + run: | + echo "### Docker Image Cleanup :broom:" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Registry:** \`${{ env.REGISTRY }}\`" >> $GITHUB_STEP_SUMMARY + echo "**Image:** \`${{ env.IMAGE_NAME }}\`" >> $GITHUB_STEP_SUMMARY + echo "**Kept versions:** ${{ github.event.inputs.keep_count || 10 }}" >> $GITHUB_STEP_SUMMARY + echo "**Cleanup Date:** $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "Old untagged images have been removed to free up storage." >> $GITHUB_STEP_SUMMARY diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index 810b3e9..fb9c3da 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -3,13 +3,19 @@ name: Build and Push Docker Image on: push: branches: - - main + - main + - develop tags: - - 'v*' + - 'v*.*.*' pull_request: branches: - - main + - main workflow_dispatch: + inputs: + tag: + description: 'Custom tag for the image' + required: false + default: 'manual' env: REGISTRY: ghcr.io @@ -24,50 +30,86 @@ jobs: id-token: write steps: - - name: Checkout repository - uses: actions/checkout@v4 + - name: Checkout repository + uses: actions/checkout@v4 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + platforms: linux/amd64,linux/arm64 - - name: Log in to GitHub 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: Log in to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - - name: Extract metadata (tags, labels) - 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,prefix={{branch}}- - type=raw,value=latest,enable={{is_default_branch}} + - name: Extract metadata (tags, labels) + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + # Tag as 'latest' for main branch + type=raw,value=latest,enable={{is_default_branch}} + # Tag with branch name + type=ref,event=branch + # Tag with PR number + type=ref,event=pr + # Tag with git tag (semver) + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + # Tag with commit SHA + type=sha,prefix={{branch}}- + # Custom tag from workflow_dispatch + type=raw,value=${{ github.event.inputs.tag }},enable=${{ github.event_name == 'workflow_dispatch' }} + labels: | + org.opencontainers.image.title=awesome-app + org.opencontainers.image.description=Next.js application for exploring awesome lists + org.opencontainers.image.vendor=valknarness + org.opencontainers.image.source=https://github.com/${{ github.repository }} - - name: Build and push Docker image - uses: docker/build-push-action@v5 - with: - context: . - file: ./Dockerfile - 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 - platforms: linux/amd64,linux/arm64 + - 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 + build-args: | + NODE_ENV=production - - name: Generate artifact attestation - if: github.event_name != 'pull_request' - uses: actions/attest-build-provenance@v1 - with: - subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} - subject-digest: ${{ steps.meta.outputs.digest }} - push-to-registry: true + - name: Generate image digest + if: github.event_name != 'pull_request' + run: | + echo "### Docker Image Published :rocket:" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Registry:** \`${{ env.REGISTRY }}\`" >> $GITHUB_STEP_SUMMARY + echo "**Image:** \`${{ env.IMAGE_NAME }}\`" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Tags:**" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Pull command:**" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY + echo "docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + + - name: PR Comment - Image built but not pushed + if: github.event_name == 'pull_request' + run: | + echo "### Docker Image Built Successfully :white_check_mark:" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "Image was built successfully but **not pushed** (PR builds are not published)." >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Would be tagged as:**" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY diff --git a/.github/workflows/docker-scan.yml b/.github/workflows/docker-scan.yml new file mode 100644 index 0000000..be6efb5 --- /dev/null +++ b/.github/workflows/docker-scan.yml @@ -0,0 +1,71 @@ +name: Docker Image Security Scan + +on: + schedule: + # Run daily at 2 AM UTC + - cron: '0 2 * * *' + push: + branches: + - main + tags: + - 'v*.*.*' + workflow_dispatch: + +env: + REGISTRY: ghcr.io + IMAGE_NAME: valknarness/awesome-app + +jobs: + scan: + runs-on: ubuntu-latest + permissions: + contents: read + packages: read + security-events: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Log in to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Pull latest image + run: | + docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest || echo "Image not found, will skip scan" + + - name: Run Trivy vulnerability scanner + uses: aquasecurity/trivy-action@master + with: + image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest + format: 'sarif' + output: 'trivy-results.sarif' + severity: 'CRITICAL,HIGH' + + - name: Upload Trivy results to GitHub Security + uses: github/codeql-action/upload-sarif@v3 + if: always() + with: + sarif_file: 'trivy-results.sarif' + + - name: Run Trivy vulnerability scanner (table output) + uses: aquasecurity/trivy-action@master + with: + image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest + format: 'table' + severity: 'CRITICAL,HIGH,MEDIUM' + + - name: Generate scan summary + if: always() + run: | + echo "### Security Scan Results :shield:" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Image:** \`${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest\`" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "**Scan Date:** $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "Check the Security tab for detailed vulnerability reports." >> $GITHUB_STEP_SUMMARY