|
| 1 | +name: Docker Template |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + environment: |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + default: "dev" |
| 10 | + description: "Specifies the environment of the deployment." |
| 11 | + working_directory: |
| 12 | + required: false |
| 13 | + type: string |
| 14 | + default: ./code/function |
| 15 | + description: "Specifies the uri of the container registry." |
| 16 | + registry_uri: |
| 17 | + required: false |
| 18 | + type: string |
| 19 | + default: ghcr.io |
| 20 | + description: "Specifies the uri of the container registry." |
| 21 | + image_name: |
| 22 | + required: true |
| 23 | + type: string |
| 24 | + description: "Specifies the name of the image." |
| 25 | + secrets: |
| 26 | + USER_NAME: |
| 27 | + required: true |
| 28 | + description: "Specifies the user name for the container registry." |
| 29 | + PASSWORD: |
| 30 | + required: true |
| 31 | + description: "Specifies the password for the container registry." |
| 32 | + |
| 33 | +jobs: |
| 34 | + deployment: |
| 35 | + name: Container Build & Push |
| 36 | + runs-on: ubuntu-latest |
| 37 | + continue-on-error: false |
| 38 | + environment: ${{ inputs.environment }} |
| 39 | + |
| 40 | + steps: |
| 41 | + # Check Out Repository |
| 42 | + - name: Check Out Repository |
| 43 | + id: checkout_repository |
| 44 | + uses: actions/checkout@v4 |
| 45 | + |
| 46 | + # Install cosign |
| 47 | + - name: Install cosign |
| 48 | + uses: sigstore/cosign-installer@v3.1.2 |
| 49 | + id: install_cosign |
| 50 | + if: github.event_name != 'pull_request' |
| 51 | + with: |
| 52 | + cosign-release: 'v2.2.0' |
| 53 | + |
| 54 | + # Install QEMU |
| 55 | + - name: Set up QEMU |
| 56 | + id: install_qemu |
| 57 | + uses: docker/setup-qemu-action@v3 |
| 58 | + |
| 59 | + # Install BuildKit |
| 60 | + - name: Install Buildx |
| 61 | + id: install_buildx |
| 62 | + uses: docker/setup-buildx-action@v3.0.0 |
| 63 | + |
| 64 | + # Login Container Registry |
| 65 | + - name: Login Container Registry |
| 66 | + uses: docker/login-action@v3.0.0 |
| 67 | + id: registry_login |
| 68 | + if: github.event_name != 'pull_request' |
| 69 | + with: |
| 70 | + registry: ${{ inputs.registry_uri }} |
| 71 | + username: ${{ secrets.USER_NAME }} |
| 72 | + password: ${{ secrets.PASSWORD }} |
| 73 | + |
| 74 | + # Extract Metadata (tags, labels) |
| 75 | + - name: Extract Metadata |
| 76 | + id: metadata |
| 77 | + uses: docker/metadata-action@v5.0.0 |
| 78 | + with: |
| 79 | + context: workflow |
| 80 | + images: | |
| 81 | + ${{ inputs.registry_uri }}/${{ inputs.image_name }} |
| 82 | + tags: | |
| 83 | + type=ref,event=branch |
| 84 | + type=ref,event=pr |
| 85 | + type=semver,pattern={{version}} |
| 86 | + type=semver,pattern={{major}}.{{minor}} |
| 87 | +
|
| 88 | + # Build and Push Docker Image with Buildx |
| 89 | + - name: Build and push Docker image |
| 90 | + id: build_push |
| 91 | + uses: docker/build-push-action@v5.0.0 |
| 92 | + with: |
| 93 | + context: ${{ inputs.working_directory }} |
| 94 | + file: ${{ inputs.working_directory }}/Dockerfile |
| 95 | + push: ${{ github.event_name != 'pull_request' }} |
| 96 | + tags: ${{ steps.metadata.outputs.tags }} |
| 97 | + labels: ${{ steps.metadata.outputs.labels }} |
| 98 | + cache-from: type=gha |
| 99 | + cache-to: type=gha,mode=max |
| 100 | + |
| 101 | + # Sign container image |
| 102 | + # This step uses the identity token to provision an ephemeral certificate against the sigstore community Fulcio instance. |
| 103 | + - name: Sign container image |
| 104 | + id: sign |
| 105 | + if: ${{ github.event_name != 'pull_request' }} |
| 106 | + run: | |
| 107 | + echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST} |
| 108 | + env: |
| 109 | + # https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable |
| 110 | + TAGS: ${{ steps.metadata.outputs.tags }} |
| 111 | + DIGEST: ${{ steps.build_push.outputs.digest }} |
0 commit comments