|
| 1 | +name: Docker Publish |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + tag: |
| 7 | + description: "The tag for the Docker image, e.g., 'latest' or 'main'" |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + platforms: |
| 11 | + description: "The platforms for Docker image build, e.g., 'linux/amd64,linux/arm64'" |
| 12 | + required: true |
| 13 | + type: string |
| 14 | + secrets: |
| 15 | + DOCKERHUB_USERNAME: |
| 16 | + required: true |
| 17 | + DOCKERHUB_TOKEN: |
| 18 | + required: true |
| 19 | + |
| 20 | +jobs: |
| 21 | + publish: |
| 22 | + environment: release-docker |
| 23 | + runs-on: ubuntu-latest |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout code |
| 27 | + uses: actions/checkout@main |
| 28 | + with: |
| 29 | + show-progress: false |
| 30 | + submodules: recursive |
| 31 | + |
| 32 | + - name: Set up Docker Buildx |
| 33 | + uses: docker/setup-buildx-action@master |
| 34 | + |
| 35 | + - name: Login to ghcr.io |
| 36 | + uses: docker/login-action@master |
| 37 | + with: |
| 38 | + registry: ghcr.io |
| 39 | + username: ${{ github.repository_owner }} |
| 40 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 41 | + |
| 42 | + - name: Login to DockerHub |
| 43 | + uses: docker/login-action@master |
| 44 | + with: |
| 45 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 46 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 47 | + |
| 48 | + - name: Prepare environment outputs |
| 49 | + shell: sh |
| 50 | + run: | |
| 51 | + set -eu |
| 52 | +
|
| 53 | + echo "DATE_ISO8601=$(date --iso-8601=seconds --utc)" >> "$GITHUB_ENV" |
| 54 | + echo "GHCR_REPOSITORY=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_ENV" |
| 55 | + echo "DH_REPOSITORY=$(echo ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }} | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_ENV" |
| 56 | + if [ "${{ inputs.tag }}" != "main" ]; then |
| 57 | + echo "FIXED_TAG=$(echo ${{ github.ref }} | cut -d '/' -f 3)" >> "$GITHUB_ENV" |
| 58 | + fi |
| 59 | +
|
| 60 | + - name: Build and publish Docker image from Dockerfile |
| 61 | + uses: docker/build-push-action@master |
| 62 | + with: |
| 63 | + context: . |
| 64 | + platforms: ${{ inputs.platforms }} |
| 65 | + provenance: true |
| 66 | + sbom: true |
| 67 | + push: true |
| 68 | + labels: | |
| 69 | + org.opencontainers.image.created=${{ env.DATE_ISO8601 }} |
| 70 | + org.opencontainers.image.version=${{ inputs.tag == 'main' && github.sha || env.FIXED_TAG }} |
| 71 | + org.opencontainers.image.revision=${{ github.sha }} |
| 72 | + tags: | |
| 73 | + ghcr.io/${{ env.GHCR_REPOSITORY }}:${{ inputs.tag }} |
| 74 | + ${{ env.DH_REPOSITORY }}:${{ inputs.tag }} |
| 75 | + ${{ inputs.tag != 'main' && format('ghcr.io/{0}:{1}', env.GHCR_REPOSITORY, env.FIXED_TAG) || '' }} |
| 76 | + ${{ inputs.tag != 'main' && format('{0}:{1}', env.DH_REPOSITORY, env.FIXED_TAG) || '' }} |
| 77 | +
|
| 78 | + - name: Update DockerHub repository description |
| 79 | + if: ${{ inputs.tag == 'main' }} |
| 80 | + uses: peter-evans/dockerhub-description@main |
| 81 | + with: |
| 82 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 83 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 84 | + repository: ${{ env.DH_REPOSITORY }} |
| 85 | + short-description: ${{ github.event.repository.description }} |
0 commit comments