|
| 1 | +name: Publish Docker image |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + tag: |
| 9 | + description: Docker tag to publish |
| 10 | + required: true |
| 11 | + type: string |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: read |
| 15 | + packages: write |
| 16 | + |
| 17 | +jobs: |
| 18 | + push-to-registry: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Check out the repo |
| 23 | + uses: actions/checkout@v4 |
| 24 | + |
| 25 | + - name: Validate manual tag |
| 26 | + if: github.event_name == 'workflow_dispatch' |
| 27 | + env: |
| 28 | + REQUESTED_TAG: ${{ inputs.tag }} |
| 29 | + run: | |
| 30 | + normalized_tag=$(printf '%s' "$REQUESTED_TAG" | tr '[:upper:]' '[:lower:]') |
| 31 | +
|
| 32 | + if [ "$normalized_tag" = "latest" ]; then |
| 33 | + echo "Error: the tag 'latest' is reserved for stable releases and cannot be set manually." |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | +
|
| 37 | + if ! printf '%s' "$REQUESTED_TAG" | grep -Eq '^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$'; then |
| 38 | + echo "Error: invalid Docker tag. Use 1-128 characters: letters, numbers, underscores, periods, or hyphens; the first character must be a letter, number, or underscore." |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | +
|
| 42 | + - name: Log in to GitHub Container Registry |
| 43 | + uses: docker/login-action@v3 |
| 44 | + with: |
| 45 | + registry: ghcr.io |
| 46 | + username: ${{ github.actor }} |
| 47 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 48 | + |
| 49 | + - name: Extract metadata for Docker |
| 50 | + id: meta |
| 51 | + uses: docker/metadata-action@v5 |
| 52 | + with: |
| 53 | + images: ghcr.io/${{ github.repository }} |
| 54 | + tags: | |
| 55 | + type=raw,value=${{ github.event.release.tag_name }},enable=${{ github.event_name == 'release' }} |
| 56 | + type=raw,value=latest,enable=${{ github.event_name == 'release' && !github.event.release.prerelease }} |
| 57 | + type=raw,value=${{ inputs.tag }},enable=${{ github.event_name == 'workflow_dispatch' }} |
| 58 | +
|
| 59 | + - name: Build and push Docker image |
| 60 | + uses: docker/build-push-action@v6 |
| 61 | + with: |
| 62 | + context: . |
| 63 | + push: true |
| 64 | + tags: ${{ steps.meta.outputs.tags }} |
| 65 | + labels: ${{ steps.meta.outputs.labels }} |
0 commit comments