|
| 1 | +name: Publish Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + version: |
| 9 | + description: "Version to publish (e.g. 0.0.9). Must match pyproject.toml." |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + branch: |
| 13 | + description: "Branch to build from" |
| 14 | + required: true |
| 15 | + type: string |
| 16 | + default: "main" |
| 17 | + publish_dockerhub: |
| 18 | + description: "Also publish to Docker Hub" |
| 19 | + required: true |
| 20 | + type: boolean |
| 21 | + default: false |
| 22 | + dockerhub_image: |
| 23 | + description: "Docker Hub image, e.g. aitechnav/sentinelguard-gateway" |
| 24 | + required: false |
| 25 | + type: string |
| 26 | + default: "" |
| 27 | + |
| 28 | +permissions: |
| 29 | + contents: read |
| 30 | + id-token: write |
| 31 | + packages: write |
| 32 | + |
| 33 | +jobs: |
| 34 | + docker: |
| 35 | + runs-on: ubuntu-latest |
| 36 | + |
| 37 | + steps: |
| 38 | + - name: Resolve release inputs |
| 39 | + id: release |
| 40 | + env: |
| 41 | + DISPATCH_VERSION: ${{ inputs.version }} |
| 42 | + DISPATCH_BRANCH: ${{ inputs.branch }} |
| 43 | + RELEASE_TAG: ${{ github.event.release.tag_name }} |
| 44 | + run: | |
| 45 | + if [ "${{ github.event_name }}" = "release" ]; then |
| 46 | + VERSION="${RELEASE_TAG#v}" |
| 47 | + REF="${RELEASE_TAG}" |
| 48 | + else |
| 49 | + VERSION="${DISPATCH_VERSION}" |
| 50 | + REF="${DISPATCH_BRANCH}" |
| 51 | + fi |
| 52 | + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" |
| 53 | + echo "ref=${REF}" >> "$GITHUB_OUTPUT" |
| 54 | +
|
| 55 | + - uses: actions/checkout@v4 |
| 56 | + with: |
| 57 | + ref: ${{ steps.release.outputs.ref }} |
| 58 | + |
| 59 | + - name: Verify version matches |
| 60 | + run: | |
| 61 | + VERSION=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])") |
| 62 | + if [ "$VERSION" != "${{ steps.release.outputs.version }}" ]; then |
| 63 | + echo "ERROR: pyproject.toml version ($VERSION) does not match ${{ steps.release.outputs.version }}" |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | + echo "Version verified: $VERSION" |
| 67 | +
|
| 68 | + - name: Prepare image tags |
| 69 | + id: images |
| 70 | + env: |
| 71 | + VERSION: ${{ steps.release.outputs.version }} |
| 72 | + DOCKERHUB_IMAGE_INPUT: ${{ inputs.dockerhub_image }} |
| 73 | + DOCKERHUB_IMAGE_VAR: ${{ vars.DOCKERHUB_IMAGE }} |
| 74 | + PUBLISH_DOCKERHUB_INPUT: ${{ inputs.publish_dockerhub }} |
| 75 | + DOCKERHUB_USERNAME_SET: ${{ secrets.DOCKERHUB_USERNAME != '' }} |
| 76 | + DOCKERHUB_TOKEN_SET: ${{ secrets.DOCKERHUB_TOKEN != '' }} |
| 77 | + run: | |
| 78 | + OWNER=$(echo "$GITHUB_REPOSITORY_OWNER" | tr '[:upper:]' '[:lower:]') |
| 79 | + GHCR_IMAGE="ghcr.io/${OWNER}/sentinelguard-gateway" |
| 80 | + DOCKERHUB_IMAGE=$(echo "${DOCKERHUB_IMAGE_INPUT:-$DOCKERHUB_IMAGE_VAR}" | tr '[:upper:]' '[:lower:]') |
| 81 | + PUBLISH_DOCKERHUB="${PUBLISH_DOCKERHUB_INPUT:-false}" |
| 82 | +
|
| 83 | + if [ "${{ github.event_name }}" = "release" ] && [ -n "$DOCKERHUB_IMAGE" ]; then |
| 84 | + PUBLISH_DOCKERHUB=true |
| 85 | + fi |
| 86 | +
|
| 87 | + if [ "$PUBLISH_DOCKERHUB" = "true" ] && [ -z "$DOCKERHUB_IMAGE" ]; then |
| 88 | + echo "ERROR: Docker Hub publishing was requested, but no Docker Hub image was configured." |
| 89 | + echo "Set the DOCKERHUB_IMAGE repo variable or workflow input, for example aitechnav/sentinelguard-gateway." |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | +
|
| 93 | + if [ "$PUBLISH_DOCKERHUB" = "true" ] && { [ "$DOCKERHUB_USERNAME_SET" != "true" ] || [ "$DOCKERHUB_TOKEN_SET" != "true" ]; }; then |
| 94 | + echo "ERROR: Docker Hub publishing was requested, but DOCKERHUB_USERNAME or DOCKERHUB_TOKEN is missing." |
| 95 | + exit 1 |
| 96 | + fi |
| 97 | +
|
| 98 | + { |
| 99 | + echo "tags<<EOF" |
| 100 | + echo "${GHCR_IMAGE}:${VERSION}" |
| 101 | + echo "${GHCR_IMAGE}:latest" |
| 102 | + if [ "$PUBLISH_DOCKERHUB" = "true" ] && [ -n "$DOCKERHUB_IMAGE" ]; then |
| 103 | + echo "${DOCKERHUB_IMAGE}:${VERSION}" |
| 104 | + echo "${DOCKERHUB_IMAGE}:latest" |
| 105 | + fi |
| 106 | + echo "EOF" |
| 107 | + echo "ghcr_image=${GHCR_IMAGE}" |
| 108 | + echo "dockerhub_image=${DOCKERHUB_IMAGE}" |
| 109 | + echo "publish_dockerhub=${PUBLISH_DOCKERHUB}" |
| 110 | + } >> "$GITHUB_OUTPUT" |
| 111 | +
|
| 112 | + - name: Set up Docker Buildx |
| 113 | + uses: docker/setup-buildx-action@v3 |
| 114 | + |
| 115 | + - name: Login to GHCR |
| 116 | + uses: docker/login-action@v3 |
| 117 | + with: |
| 118 | + registry: ghcr.io |
| 119 | + username: ${{ github.actor }} |
| 120 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 121 | + |
| 122 | + - name: Login to Docker Hub |
| 123 | + if: steps.images.outputs.publish_dockerhub == 'true' |
| 124 | + uses: docker/login-action@v3 |
| 125 | + with: |
| 126 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 127 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 128 | + |
| 129 | + - name: Build and push Docker image |
| 130 | + id: build |
| 131 | + uses: docker/build-push-action@v6 |
| 132 | + with: |
| 133 | + context: . |
| 134 | + file: ./Dockerfile |
| 135 | + platforms: linux/amd64,linux/arm64 |
| 136 | + push: true |
| 137 | + tags: ${{ steps.images.outputs.tags }} |
| 138 | + build-args: | |
| 139 | + SENTINELGUARD_EXTRAS=gateway,monitoring |
| 140 | + labels: | |
| 141 | + org.opencontainers.image.title=SentinelGuard Gateway |
| 142 | + org.opencontainers.image.description=SentinelGuard OpenAI-compatible LLM gateway with security scanning |
| 143 | + org.opencontainers.image.version=${{ steps.release.outputs.version }} |
| 144 | + org.opencontainers.image.source=https://github.com/${{ github.repository }} |
| 145 | + org.opencontainers.image.revision=${{ github.sha }} |
| 146 | + provenance: true |
| 147 | + sbom: true |
| 148 | + |
| 149 | + - name: Install cosign |
| 150 | + uses: sigstore/cosign-installer@v3 |
| 151 | + |
| 152 | + - name: Sign published image tags |
| 153 | + env: |
| 154 | + TAGS: ${{ steps.images.outputs.tags }} |
| 155 | + DIGEST: ${{ steps.build.outputs.digest }} |
| 156 | + run: | |
| 157 | + while IFS= read -r tag; do |
| 158 | + if [ -n "$tag" ]; then |
| 159 | + cosign sign --yes "${tag}@${DIGEST}" |
| 160 | + fi |
| 161 | + done <<EOF |
| 162 | + ${TAGS} |
| 163 | + EOF |
| 164 | +
|
| 165 | + - name: Summary |
| 166 | + run: | |
| 167 | + echo "## Docker Publish Successful" >> "$GITHUB_STEP_SUMMARY" |
| 168 | + echo "- Version: ${{ steps.release.outputs.version }}" >> "$GITHUB_STEP_SUMMARY" |
| 169 | + echo "- Tags:" >> "$GITHUB_STEP_SUMMARY" |
| 170 | + while IFS= read -r tag; do |
| 171 | + if [ -n "$tag" ]; then |
| 172 | + echo " - \`${tag}\`" >> "$GITHUB_STEP_SUMMARY" |
| 173 | + fi |
| 174 | + done <<EOF |
| 175 | + ${{ steps.images.outputs.tags }} |
| 176 | + EOF |
| 177 | + echo "" >> "$GITHUB_STEP_SUMMARY" |
| 178 | + echo "SBOM/provenance attestations were generated by Docker Buildx." >> "$GITHUB_STEP_SUMMARY" |
| 179 | + echo "Image tags were signed with cosign keyless signing." >> "$GITHUB_STEP_SUMMARY" |
0 commit comments