Search code examples
dockergithubdockerfilegithub-actionsdocker-registry

Github actions specify package name


TL;DR: I want to know how to publish specific package names to ghcr instead of the repostiry name. How can I do that?

For example:

  • docker-php-stack/php74:latest
  • docker-php-stack/php80:latest
  • docker-php-stack/php81:latest

Instead of what it is now:

  • docker-php-stack:sha256-a417bb4fbb9e606fb39012ff6e71219597c5eee8947eef5d99395f880f05368d.sig

Long version:

So I am running different actions for different directories with the following action: (source: https://github.com/bruvv/docker-php-stack)

name: Docker-PHP80

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

on:
  schedule:
    - cron: "20 11 * * *"
  push:
    branches: [main]
    paths:
      - "php80/Dockerfile"
    # Publish semver tags as releases.
    tags: ["v*.*.*"]

env:
  # Use docker.io for Docker Hub if empty
  REGISTRY: ghcr.io
  # github.repository as <account>/<repo>
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      # This is used to complete the identity challenge
      # with sigstore/fulcio when running outside of PRs.
      id-token: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      # Install the cosign tool except on PR
      # https://github.com/sigstore/cosign-installer
      - name: Install cosign
        if: github.event_name != 'pull_request'
        uses: sigstore/cosign-installer@9f7b96f21107c0c5f4baf9536853b2554b4179f7
        with:
          cosign-release: "v1.4.0"

      # Workaround: https://github.com/docker/build-push-action/issues/461
      - name: Setup Docker buildx
        uses: docker/setup-buildx-action@2a6fbda6d8bd6fe40f8b36c71fca20ffc286129e

      # Login against a Docker registry except on PR
      # https://github.com/docker/login-action
      - name: Log into registry ${{ env.REGISTRY }}
        if: github.event_name != 'pull_request'
        uses: docker/login-action@7c79b598eaa33458e78e8d0d71e0a9c217dd92af
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      # Extract metadata (tags, labels) for Docker
      # https://github.com/docker/metadata-action
      - name: Extract Docker metadata
        id: meta
        uses: docker/metadata-action@1237c3e3d6926ae8bfad16f64e08d259b9943637
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

      # Build and push Docker image with Buildx (don't push on PR)
      # https://github.com/docker/build-push-action
      - name: Build and push Docker image
        id: build-and-push
        uses: docker/build-push-action@f7a2a67b4c1059633be6daa491784d8ac6a7ed6d
        with:
          context: ./php80
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

      # Sign the resulting Docker image digest except on PRs.
      # This will only write to the public Rekor transparency log when the Docker
      # repository is public to avoid leaking data.  If you would like to publish
      # transparency data even for private images, pass --force to cosign below.
      # https://github.com/sigstore/cosign
      - name: Sign the published Docker image
        if: ${{ github.event_name != 'pull_request' }}
        env:
          COSIGN_EXPERIMENTAL: "true"
        # This step uses the identity token to provision an ephemeral certificate
        # against the sigstore community Fulcio instance.
        run: cosign sign ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build-and-push.outputs.digest }}

What I want is:

  • docker-php-stack/php74:latest
  • docker-php-stack/php80:latest
  • docker-php-stack/php81:latest

instead of what it is now: https://github.com/bruvv/docker-php-stack/pkgs/container/docker-php-stack

I tried changing the following:

          tags: ${{ steps.meta.outputs.tags }}

But I am getting:

error: failed to solve: unexpected status: 400 Bad Request
Error: buildx failed with: error: failed to solve: unexpected status: 400 Bad Request

That is all I was able to find online. So far I am out of ideas.


Solution

  • I found it when the following to docker/metadata-action

              tags: |
                type=raw,value=somethingyouwant-latest
    
    name: Docker-PHP74
    
    # This workflow uses actions that are not certified by GitHub.
    # They are provided by a third-party and are governed by
    # separate terms of service, privacy policy, and support
    # documentation.
    
    on:
    #   schedule:
    #     - cron: "20 11 * * *"
      push:
        branches: [main]
        paths:
          - "php74/Dockerfile"
        # Publish semver tags as releases.
        tags: ["v*.*.*"]
    
    env:
      # Use docker.io for Docker Hub if empty
      REGISTRY: ghcr.io
      # github.repository as <account>/<repo>
      IMAGE_NAME: ${{ github.repository }}
    
    jobs:
      build:
        runs-on: ubuntu-latest
        permissions:
          contents: read
          packages: write
          # This is used to complete the identity challenge
          # with sigstore/fulcio when running outside of PRs.
          id-token: write
    
        steps:
          - name: Checkout repository
            uses: actions/checkout@v3
    
          # Install the cosign tool except on PR
          # https://github.com/sigstore/cosign-installer
          - name: Install cosign
            if: github.event_name != 'pull_request'
            uses: sigstore/[email protected]
    
          # Workaround: https://github.com/docker/build-push-action/issues/461
          - name: Setup Docker buildx
            uses: docker/[email protected]
    
          # Login against a Docker registry except on PR
          # https://github.com/docker/login-action
          - name: Log into registry ${{ env.REGISTRY }}
            if: github.event_name != 'pull_request'
            uses: docker/[email protected]
            with:
              registry: ${{ env.REGISTRY }}
              username: ${{ github.actor }}
              password: ${{ secrets.GITHUB_TOKEN }}
    
          # Extract metadata (tags, labels) for Docker
          # https://github.com/docker/metadata-action
          - name: Extract Docker metadata
            id: meta
            uses: docker/[email protected]
            with:
              images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
              #flavor: prefix=php74 #this becomes php74main
              tags: |
                type=raw,value=php74-latest
              
    
          # Build and push Docker image with Buildx (don't push on PR)
          # https://github.com/docker/build-push-action
          - name: Build and push Docker image
            id: build-and-push
            uses: docker/[email protected]
            with:
              context: ./php74
              push: ${{ github.event_name != 'pull_request' }}
              tags: ${{ steps.meta.outputs.tags }}
              labels: ${{ steps.meta.outputs.labels }}
    
          # Sign the resulting Docker image digest except on PRs.
          # This will only write to the public Rekor transparency log when the Docker
          # repository is public to avoid leaking data.  If you would like to publish
          # transparency data even for private images, pass --force to cosign below.
          # https://github.com/sigstore/cosign
          - name: Sign the published Docker image
            if: ${{ github.event_name != 'pull_request' }}
            env:
              COSIGN_EXPERIMENTAL: "true"
            # This step uses the identity token to provision an ephemeral certificate
            # against the sigstore community Fulcio instance.
            run: cosign sign ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build-and-push.outputs.digest }}