|
| 1 | +# Publishes an 'edge' container image to a registry, tagging with the current date and commit hash. |
| 2 | +name: Publish Container Image |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_call: |
| 6 | + inputs: |
| 7 | + image-name: |
| 8 | + description: 'Container image name (e.g. my-app)' |
| 9 | + required: true |
| 10 | + type: string |
| 11 | + image-description: |
| 12 | + description: 'Description used in OCI annotations' |
| 13 | + required: false |
| 14 | + default: '' |
| 15 | + type: string |
| 16 | + registry: |
| 17 | + description: 'Container registry host (e.g. ghcr.io)' |
| 18 | + required: false |
| 19 | + default: 'ghcr.io' |
| 20 | + type: string |
| 21 | + repo-owner: |
| 22 | + description: 'Owner/namespace for the image (defaults to calling repo owner)' |
| 23 | + required: false |
| 24 | + default: '' |
| 25 | + type: string |
| 26 | + context: |
| 27 | + description: 'Build context passed to Docker (e.g. . or ./app)' |
| 28 | + required: false |
| 29 | + default: '.' |
| 30 | + type: string |
| 31 | + dockerfile: |
| 32 | + description: 'Relative path to Dockerfile' |
| 33 | + required: false |
| 34 | + default: 'Dockerfile' |
| 35 | + type: string |
| 36 | + platforms: |
| 37 | + description: 'Target platforms for the image (comma-separated)' |
| 38 | + required: false |
| 39 | + default: 'linux/amd64,linux/arm64' |
| 40 | + type: string |
| 41 | + |
| 42 | +env: |
| 43 | + image-name: ${{ inputs.image-name }} |
| 44 | + image-description: ${{ inputs.image-description }} |
| 45 | + repo-owner: ${{ inputs.repo-owner || github.repository_owner }} |
| 46 | + registry: ${{ inputs.registry }} |
| 47 | + context: ${{ inputs.context }} |
| 48 | + dockerfile: ${{ inputs.dockerfile }} |
| 49 | + platforms: ${{ inputs.platforms }} |
| 50 | + |
| 51 | +jobs: |
| 52 | + publish-container: |
| 53 | + runs-on: ubuntu-latest |
| 54 | + permissions: |
| 55 | + packages: write |
| 56 | + steps: |
| 57 | + - name: Check out the repo |
| 58 | + uses: actions/checkout@v4 |
| 59 | + |
| 60 | + - name: Set up QEMU |
| 61 | + uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3.6.0 |
| 62 | + |
| 63 | + - name: Set up Docker Buildx |
| 64 | + uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 |
| 65 | + |
| 66 | + - name: Docker Login |
| 67 | + uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 |
| 68 | + with: |
| 69 | + registry: ${{ env.registry }} |
| 70 | + username: ${{ github.actor }} |
| 71 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 72 | + |
| 73 | + - name: Docker Metadata action |
| 74 | + id: meta |
| 75 | + uses: docker/metadata-action@902fa8ec7d6ecbf8d84d538b9b233a880e428804 # v5.7.0 |
| 76 | + env: |
| 77 | + DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index |
| 78 | + with: |
| 79 | + images: ${{ env.registry }}/${{ env.repo-owner }}/${{ env.image-name }} |
| 80 | + # Tag notes: |
| 81 | + # - RFC3339 is not suitable for docker tags, so we squash the date |
| 82 | + # - We tag both the short (7-char prefixed) and full sha commit hashes; both are useful |
| 83 | + # - `edge` represents latest main branch commit (potentially unstable) |
| 84 | + tags: | |
| 85 | + type=sha |
| 86 | + ${{ github.sha }} |
| 87 | + type=raw,value={{date 'YYYYMMDDHHmmss[Z]'}} |
| 88 | + edge |
| 89 | + # Label notes: |
| 90 | + # - Static labels are applied in the Dockerfile |
| 91 | + # - Date format in `org.opencontainers.image.created` must be RFC3339 |
| 92 | + # - version should be considered a semver candidate only, unless revision aligns with a git tag |
| 93 | + labels: | |
| 94 | + org.opencontainers.image.revision={{sha}} |
| 95 | + org.opencontainers.image.created={{date 'YYYY-MM-DD HH:mm:ss[Z]'}} |
| 96 | + annotations: | |
| 97 | + org.opencontainers.image.description=${{ env.image-description }} |
| 98 | +
|
| 99 | + - name: Build and push Docker images |
| 100 | + uses: docker/build-push-action@471d1dc4e07e5cdedd4c2171150001c434f0b7a4 # v6.15.0 |
| 101 | + with: |
| 102 | + context: ${{ env.context }} |
| 103 | + file: ${{ env.dockerfile }} |
| 104 | + push: true |
| 105 | + platforms: ${{ env.platforms }} |
| 106 | + tags: ${{ steps.meta.outputs.tags }} |
| 107 | + labels: ${{ steps.meta.outputs.labels }} |
| 108 | + annotations: ${{ steps.meta.outputs.annotations }} |
0 commit comments