|
| 1 | +name: Build and Push Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + image_name: |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + platforms: |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + default: linux/amd64,linux/arm64 |
| 13 | + secrets: |
| 14 | + DOCKER_USERNAME: |
| 15 | + required: true |
| 16 | + DOCKER_TOKEN: |
| 17 | + required: true |
| 18 | + GITHUB_TOKEN: |
| 19 | + required: true |
| 20 | + |
| 21 | +jobs: |
| 22 | + build-and-push: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + permissions: |
| 25 | + contents: write |
| 26 | + packages: write |
| 27 | + steps: |
| 28 | + - name: Checkout repository |
| 29 | + uses: actions/checkout@v4 |
| 30 | + |
| 31 | + - name: Update version |
| 32 | + id: version |
| 33 | + env: |
| 34 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 35 | + run: | |
| 36 | + pip install requests |
| 37 | + chmod +x .github/workflows/build-push/update_version.py |
| 38 | + .github/workflows/build-push/update_version.py |
| 39 | +
|
| 40 | + - name: Set up QEMU |
| 41 | + uses: docker/setup-qemu-action@v3 |
| 42 | + |
| 43 | + - name: Set up Docker Buildx |
| 44 | + uses: docker/setup-buildx-action@v3 |
| 45 | + |
| 46 | + - name: Log in to Docker Hub |
| 47 | + uses: docker/login-action@v3 |
| 48 | + with: |
| 49 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 50 | + password: ${{ secrets.DOCKER_TOKEN }} |
| 51 | + |
| 52 | + - name: Log in to GitHub Container Registry |
| 53 | + uses: docker/login-action@v3 |
| 54 | + with: |
| 55 | + registry: ghcr.io |
| 56 | + username: ${{ github.actor }} |
| 57 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + |
| 59 | + - name: Generate Docker Tags |
| 60 | + id: meta |
| 61 | + run: | |
| 62 | + TAGS_JSON='${{ steps.version.outputs.tags }}' |
| 63 | + REPO_OWNER=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]') |
| 64 | + DOCKERHUB_TAGS=$(echo "$TAGS_JSON" | jq -r '.[] | "${{ secrets.DOCKER_USERNAME }}/${{ inputs.image_name }}:" + .') |
| 65 | + GHCR_TAGS=$(echo "$TAGS_JSON" | jq -r '.[] | "ghcr.io/$REPO_OWNER/${{ inputs.image_name }}:" + .') |
| 66 | + ALL_TAGS=$(echo -e "${DOCKERHUB_TAGS}\n${GHCR_TAGS}" | paste -sd "," -) |
| 67 | + echo "tags=${ALL_TAGS}" >> $GITHUB_OUTPUT |
| 68 | +
|
| 69 | + - name: Build and push Docker image |
| 70 | + uses: docker/build-push-action@v5 |
| 71 | + with: |
| 72 | + context: src |
| 73 | + platforms: ${{ inputs.platforms }} |
| 74 | + push: ${{ github.event_name != 'pull_request' }} |
| 75 | + tags: ${{ steps.meta.outputs.tags }} |
| 76 | + labels: | |
| 77 | + org.opencontainers.image.title=${{ inputs.image_name }} |
| 78 | + org.opencontainers.image.version=${{ steps.version.outputs.full_version }} |
| 79 | + org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} |
| 80 | + cache-from: type=gha |
| 81 | + cache-to: type=gha,mode=max |
| 82 | + |
| 83 | + - name: Save Docker image |
| 84 | + run: | |
| 85 | + set -e |
| 86 | + VERSION="${{ steps.version.outputs.full_version }}" |
| 87 | + REPO_OWNER=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]') |
| 88 | + GHCR_TAG="ghcr.io/$REPO_OWNER/${{ inputs.image_name }}:$VERSION" |
| 89 | + docker pull $GHCR_TAG |
| 90 | + docker save $GHCR_TAG -o "${{ inputs.image_name }}.tar" |
| 91 | +
|
| 92 | + - name: Generate SBOM |
| 93 | + env: |
| 94 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 95 | + FULL_VERSION: ${{ steps.version.outputs.full_version }} |
| 96 | + run: | |
| 97 | + chmod +x .github/workflows/build-push/generate_sbom.py |
| 98 | + .github/workflows/build-push/generate_sbom.py |
| 99 | +
|
| 100 | + - name: Generate Vulnerability Report |
| 101 | + env: |
| 102 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 103 | + FULL_VERSION: ${{ steps.version.outputs.full_version }} |
| 104 | + run: | |
| 105 | + chmod +x .github/workflows/build-push/generate_vulnerability_report.py |
| 106 | + .github/workflows/build-push/generate_vulnerability_report.py |
| 107 | +
|
| 108 | + - name: Create Release |
| 109 | + id: release |
| 110 | + env: |
| 111 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 112 | + run: | |
| 113 | + chmod +x .github/workflows/build-push/publish_release.py |
| 114 | + .github/workflows/build-push/publish_release.py |
| 115 | +
|
| 116 | + - name: Upload release assets |
| 117 | + env: |
| 118 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 119 | + run: | |
| 120 | + VERSION="${{ steps.version.outputs.full_version }}" |
| 121 | + cp .vulnerability_report.txt vulnerability_report.txt |
| 122 | + gh release upload "$VERSION" \ |
| 123 | + "${{ inputs.image_name }}.tar" \ |
| 124 | + ".sbom/sbom.json" \ |
| 125 | + ".sbom/sbom.txt" \ |
| 126 | + "vulnerability_report.txt" |
0 commit comments