name: Publish Docker image on: workflow_dispatch: push: branches: - main release: types: - published concurrency: group: publish-docker-image-${{ github.event.release.tag_name || github.ref }} cancel-in-progress: true permissions: contents: read packages: write jobs: publish: runs-on: ubuntu-latest env: BUILD_ARGS: | NEXT_PUBLIC_APP_PLATFORM=web steps: - uses: actions/checkout@v4 with: submodules: recursive - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to GHCR uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Detect Docker Hub credentials id: dockerhub run: | if [ -n "${{ secrets.DOCKERHUB_USERNAME }}" ] && [ -n "${{ secrets.DOCKERHUB_TOKEN }}" ]; then echo "enabled=true" >> "$GITHUB_OUTPUT" else echo "enabled=false" >> "$GITHUB_OUTPUT" fi - name: Log in to Docker Hub if: steps.dockerhub.outputs.enabled == 'true' uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Extract image metadata (GHCR only) id: meta-ghcr if: steps.dockerhub.outputs.enabled != 'true' uses: docker/metadata-action@v5 with: images: ghcr.io/${{ github.repository_owner }}/readest tags: | type=raw,value=main,enable={{is_default_branch}} type=raw,value=latest,enable=${{ github.event_name == 'release' }} type=raw,value=${{ github.event.release.tag_name }},enable=${{ github.event_name == 'release' }} type=sha,prefix=sha- - name: Extract image metadata (GHCR + Docker Hub) id: meta-all if: steps.dockerhub.outputs.enabled == 'true' uses: docker/metadata-action@v5 with: images: | ghcr.io/${{ github.repository_owner }}/readest docker.io/${{ secrets.DOCKERHUB_USERNAME }}/readest tags: | type=raw,value=main,enable={{is_default_branch}} type=raw,value=latest,enable=${{ github.event_name == 'release' }} type=raw,value=${{ github.event.release.tag_name }},enable=${{ github.event_name == 'release' }} type=sha,prefix=sha- - name: Build and push (GHCR only) id: build-ghcr if: steps.dockerhub.outputs.enabled != 'true' uses: docker/build-push-action@v6 with: context: . file: ./Dockerfile target: production-stage platforms: linux/amd64,linux/arm64 push: true tags: ${{ steps.meta-ghcr.outputs.tags }} labels: ${{ steps.meta-ghcr.outputs.labels }} build-args: ${{ env.BUILD_ARGS }} - name: Published image summary (GHCR only) if: steps.dockerhub.outputs.enabled != 'true' run: | echo "## Published Images" >> "$GITHUB_STEP_SUMMARY" echo "" >> "$GITHUB_STEP_SUMMARY" echo "Registry: GHCR" >> "$GITHUB_STEP_SUMMARY" echo "" >> "$GITHUB_STEP_SUMMARY" echo "Tags:" >> "$GITHUB_STEP_SUMMARY" echo '```' >> "$GITHUB_STEP_SUMMARY" echo "${{ steps.meta-ghcr.outputs.tags }}" >> "$GITHUB_STEP_SUMMARY" echo '```' >> "$GITHUB_STEP_SUMMARY" echo "Digest: \`${{ steps.build-ghcr.outputs.digest }}\`" >> "$GITHUB_STEP_SUMMARY" - name: Build and push (GHCR + Docker Hub) id: build-all if: steps.dockerhub.outputs.enabled == 'true' uses: docker/build-push-action@v6 with: context: . file: ./Dockerfile target: production-stage platforms: linux/amd64,linux/arm64 push: true tags: ${{ steps.meta-all.outputs.tags }} labels: ${{ steps.meta-all.outputs.labels }} build-args: ${{ env.BUILD_ARGS }} - name: Published image summary (GHCR + Docker Hub) if: steps.dockerhub.outputs.enabled == 'true' run: | echo "## Published Images" >> "$GITHUB_STEP_SUMMARY" echo "" >> "$GITHUB_STEP_SUMMARY" echo "Registries: GHCR, Docker Hub" >> "$GITHUB_STEP_SUMMARY" echo "" >> "$GITHUB_STEP_SUMMARY" echo "Tags:" >> "$GITHUB_STEP_SUMMARY" echo '```' >> "$GITHUB_STEP_SUMMARY" echo "${{ steps.meta-all.outputs.tags }}" >> "$GITHUB_STEP_SUMMARY" echo '```' >> "$GITHUB_STEP_SUMMARY" echo "Digest: \`${{ steps.build-all.outputs.digest }}\`" >> "$GITHUB_STEP_SUMMARY"