|
| 1 | +name: _docker-pipeline (reusable) |
| 2 | + |
| 3 | +# Reusable workflow β the single lego brick for all Docker CI steps. |
| 4 | +# |
| 5 | +# Called by smoke-test.yml (push: false) and publish-docker.yml (push: true). |
| 6 | +# Step visibility is controlled by the push/tag_push inputs; the caller sets permissions. |
| 7 | +# |
| 8 | +# Two modes: |
| 9 | +# push: false β build + smoke test + integration test (main image only) |
| 10 | +# push: true β above + push to GHCR/Docker Hub + update floating v-tag |
| 11 | +# |
| 12 | +# Permissions required from the calling workflow: |
| 13 | +# push: false β contents: read |
| 14 | +# push: true β contents: write, packages: write |
| 15 | + |
| 16 | +on: |
| 17 | + workflow_call: |
| 18 | + inputs: |
| 19 | + name: |
| 20 | + description: "Image name, e.g. socket-basics" |
| 21 | + type: string |
| 22 | + required: true |
| 23 | + dockerfile: |
| 24 | + description: "Path to Dockerfile relative to repo root" |
| 25 | + type: string |
| 26 | + required: true |
| 27 | + context: |
| 28 | + description: "Docker build context" |
| 29 | + type: string |
| 30 | + required: false |
| 31 | + default: "." |
| 32 | + check_set: |
| 33 | + description: "Smoke-test tool set: main or app-tests" |
| 34 | + type: string |
| 35 | + required: true |
| 36 | + push: |
| 37 | + description: "Push to GHCR and Docker Hub after testing" |
| 38 | + type: boolean |
| 39 | + required: false |
| 40 | + default: false |
| 41 | + tag_push: |
| 42 | + description: > |
| 43 | + True when the caller was triggered by a tag push (e.g. v2.0.0). |
| 44 | + Controls the floating major-version tag update and the 'latest' Docker tag. |
| 45 | + Passed explicitly rather than relying on github.ref_type inside the callee, |
| 46 | + since context propagation in reusable workflows can be ambiguous. |
| 47 | + type: boolean |
| 48 | + required: false |
| 49 | + default: false |
| 50 | + version: |
| 51 | + description: "Semver without v prefix (e.g. 2.0.0) β used for OCI labels and push tags" |
| 52 | + type: string |
| 53 | + required: false |
| 54 | + default: "dev" |
| 55 | + secrets: |
| 56 | + DOCKERHUB_USERNAME: |
| 57 | + required: false |
| 58 | + DOCKERHUB_TOKEN: |
| 59 | + required: false |
| 60 | + |
| 61 | +jobs: |
| 62 | + pipeline: |
| 63 | + runs-on: ubuntu-latest |
| 64 | + timeout-minutes: 60 |
| 65 | + |
| 66 | + steps: |
| 67 | + - name: Checkout |
| 68 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 69 | + |
| 70 | + - name: π¨ Set up Docker Buildx |
| 71 | + uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0 |
| 72 | + |
| 73 | + # Logins and metadata are only needed in push mode |
| 74 | + - name: Login to GHCR |
| 75 | + if: inputs.push |
| 76 | + uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0 |
| 77 | + with: |
| 78 | + registry: ghcr.io |
| 79 | + username: ${{ github.actor }} |
| 80 | + password: ${{ github.token }} |
| 81 | + |
| 82 | + - name: Login to Docker Hub |
| 83 | + if: inputs.push |
| 84 | + uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0 |
| 85 | + with: |
| 86 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 87 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 88 | + |
| 89 | + - name: Extract image metadata |
| 90 | + if: inputs.push |
| 91 | + id: meta |
| 92 | + uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0 |
| 93 | + with: |
| 94 | + images: | |
| 95 | + ghcr.io/socketdev/${{ inputs.name }} |
| 96 | + ${{ secrets.DOCKERHUB_USERNAME }}/${{ inputs.name }} |
| 97 | + tags: | |
| 98 | + # Tag push (v2.0.0) β immutable Docker tags 2.0.0 and 2.0 only. |
| 99 | + # :latest and floating major tags (v2) are intentionally omitted β |
| 100 | + # this is a security tool and mutable tags set the wrong example. |
| 101 | + # Users should pin to a specific version or digest; Dependabot manages upgrades. |
| 102 | + type=semver,pattern={{version}} |
| 103 | + type=semver,pattern={{major}}.{{minor}} |
| 104 | + # workflow_dispatch re-publish β use the version input directly |
| 105 | + type=raw,value=${{ inputs.version }},enable=${{ !inputs.tag_push }} |
| 106 | + labels: | |
| 107 | + org.opencontainers.image.title=${{ inputs.name }} |
| 108 | + org.opencontainers.image.source=https://github.com/SocketDev/socket-basics |
| 109 | +
|
| 110 | + # ββ Step 1: Build ββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| 111 | + # Loads image into the local Docker daemon without pushing. |
| 112 | + # Writes all layers to the GHA cache so the push step is just an upload. |
| 113 | + - name: π¨ Build (load for testing) |
| 114 | + uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2 |
| 115 | + with: |
| 116 | + context: ${{ inputs.context }} |
| 117 | + file: ${{ inputs.dockerfile }} |
| 118 | + load: true |
| 119 | + push: false |
| 120 | + tags: ${{ inputs.name }}:pipeline-test |
| 121 | + build-args: | |
| 122 | + SOCKET_BASICS_VERSION=${{ inputs.version }} |
| 123 | + VCS_REF=${{ github.sha }} |
| 124 | + BUILD_DATE=${{ github.event.repository.updated_at }} |
| 125 | + cache-from: type=gha,scope=${{ inputs.name }} |
| 126 | + cache-to: type=gha,mode=max,scope=${{ inputs.name }} |
| 127 | + |
| 128 | + # ββ Step 2: Smoke test βββββββββββββββββββββββββββββββββββββββββββββββββ |
| 129 | + - name: π§ͺ Smoke test |
| 130 | + run: | |
| 131 | + bash ./scripts/smoke-test-docker.sh \ |
| 132 | + --skip-build \ |
| 133 | + --image-tag ${{ inputs.name }}:pipeline-test \ |
| 134 | + --check-set ${{ inputs.check_set }} |
| 135 | +
|
| 136 | + # ββ Step 3: Integration test (main image only) βββββββββββββββββββββββββ |
| 137 | + - name: π¬ Integration test |
| 138 | + if: inputs.name == 'socket-basics' |
| 139 | + run: | |
| 140 | + bash ./scripts/integration-test-docker.sh \ |
| 141 | + --image-tag ${{ inputs.name }}:pipeline-test |
| 142 | +
|
| 143 | + # ββ Step 4: Push to registries (publish mode only) βββββββββββββββββββββ |
| 144 | + # All layers are in the GHA cache from step 1 β this is just an upload. |
| 145 | + - name: π Push to registries |
| 146 | + if: inputs.push |
| 147 | + uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2 |
| 148 | + with: |
| 149 | + context: ${{ inputs.context }} |
| 150 | + file: ${{ inputs.dockerfile }} |
| 151 | + load: false |
| 152 | + push: true |
| 153 | + tags: ${{ steps.meta.outputs.tags }} |
| 154 | + labels: ${{ steps.meta.outputs.labels }} |
| 155 | + build-args: | |
| 156 | + SOCKET_BASICS_VERSION=${{ inputs.version }} |
| 157 | + VCS_REF=${{ github.sha }} |
| 158 | + BUILD_DATE=${{ github.event.repository.updated_at }} |
| 159 | + cache-from: type=gha,scope=${{ inputs.name }} |
| 160 | + provenance: true |
| 161 | + sbom: true |
| 162 | + |
| 163 | + # Floating major version tags (v2 β latest v2.x.y) have been intentionally |
| 164 | + # removed. Mutable tags are structurally equivalent to :latest and are |
| 165 | + # inappropriate for a security tool. Users should pin to an immutable |
| 166 | + # version tag or digest and use Dependabot to manage upgrades. |
0 commit comments