name: Docker Image Build and Analysis on: schedule: - cron: "0 0 * * *" # Schedule the workflow to run daily at midnight (UTC time). Adjust the time if needed. workflow_dispatch: # Manual run trigger inputs: trigger-build: description: 'Trigger a manual build and push' default: 'true' env: REGISTRY: docker.io IMAGE_NAME: ${{ github.repository }} SHA: ${{ github.event.pull_request.head.sha || github.event.after }} jobs: build-and-analyze: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - name: Log in to Docker Hub uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Extract Docker metadata id: meta uses: docker/metadata-action@v4.4.0 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} labels: | org.opencontainers.image.revision=${{ env.SHA }} tags: | type=edge,branch=$repo.default_branch type=semver,pattern=v{{version}} type=sha,prefix=,suffix=,format=short - name: Build and push Docker image id: build-and-push uses: docker/build-push-action@v4.0.0 with: context: . push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max - name: Docker Scout id: docker-scout if: ${{ github.event_name == 'pull_request' }} uses: docker/scout-action@dd36f5b0295baffa006aa6623371f226cc03e506 with: command: cves image: ${{ steps.meta.outputs.tags }} exit-code: true - name: Create build summary if: ${{ always() }} uses: actions/github-script@v6 with: script: | const summary = ` ### Docker Image Build and Analysis Summary - **Image Name:** ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} - **Commit SHA:** ${{ env.SHA }} - **Build Status:** ${{ job.status }} - **Docker Scout Results:** - **CVE Analysis:** ${{ steps.docker-scout.outputs.result || 'No results' }} `; const comment = { body: summary }; if (process.env.GITHUB_EVENT_NAME === 'pull_request') { const prNumber = process.env.GITHUB_REF.split('/')[2]; await github.rest.issues.createComment({ issue_number: prNumber, owner: context.repo.owner, repo: context.repo.repo, ...comment }); } else { await github.rest.repos.createCommitStatus({ owner: context.repo.owner, repo: context.repo.repo, sha: process.env.GITHUB_SHA, state: job.status === 'success' ? 'success' : 'failure', description: 'Docker image build and analysis complete', context: 'docker-build' }); } env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}