GithubHelp home page GithubHelp logo

docker / build-push-action Goto Github PK

View Code? Open in Web Editor NEW
4.0K 37.0 511.0 13.81 MB

GitHub Action to build and push Docker images with Buildx

Home Page: https://github.com/marketplace/actions/build-and-push-docker-images

License: Apache License 2.0

Dockerfile 7.99% TypeScript 89.76% HCL 1.82% Go 0.42%
buildx github-actions github-actions-docker docker dockerhub

build-push-action's Introduction

GitHub release GitHub marketplace CI workflow Test workflow Codecov

About

GitHub Action to build and push Docker images with Buildx with full support of the features provided by Moby BuildKit builder toolkit. This includes multi-platform build, secrets, remote cache, etc. and different builder deployment/namespacing options.

Screenshot


Usage

In the examples below we are also using 3 other actions:

  • setup-buildx action will create and boot a builder using by default the docker-container driver. This is not required but recommended using it to be able to build multi-platform images, export cache, etc.
  • setup-qemu action can be useful if you want to add emulation support with QEMU to be able to build against more platforms.
  • login action will take care to log in against a Docker registry.

Git context

By default, this action uses the Git context, so you don't need to use the actions/checkout action to check out the repository as this will be done directly by BuildKit.

The git reference will be based on the event that triggered your workflow and will result in the following context: https://github.com/<owner>/<repo>.git#<ref>.

name: ci

on:
  push:
    branches:
      - 'main'

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      -
        name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push
        uses: docker/build-push-action@v5
        with:
          push: true
          tags: user/app:latest

Be careful because any file mutation in the steps that precede the build step will be ignored, including processing of the .dockerignore file since the context is based on the Git reference. However, you can use the Path context using the context input alongside the actions/checkout action to remove this restriction.

Default Git context can also be provided using the Handlebars template expression {{defaultContext}}. Here we can use it to provide a subdirectory to the default Git context:

      -
        # Setting up Docker Buildx with docker-container driver is required
        # at the moment to be able to use a subdirectory with Git context
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      -
        name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: "{{defaultContext}}:mysubdir"
          push: true
          tags: user/app:latest

Building from the current repository automatically uses the GitHub Token, so it does not need to be passed. If you want to authenticate against another private repository, you have to use a secret named GIT_AUTH_TOKEN to be able to authenticate against it with Buildx:

      -
        name: Build and push
        uses: docker/build-push-action@v5
        with:
          push: true
          tags: user/app:latest
          secrets: |
            GIT_AUTH_TOKEN=${{ secrets.MYTOKEN }}

Path context

name: ci

on:
  push:
    branches:
      - 'main'

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v4
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      -
        name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: user/app:latest

Examples

Customizing

inputs

Following inputs can be used as step.with keys

List type is a newline-delimited string

cache-from: |
  user/app:cache
  type=local,src=path/to/dir

CSV type is a comma-delimited string

tags: name/app:latest,name/app:1.0.0
Name Type Description
add-hosts List/CSV List of customs host-to-IP mapping (e.g., docker:10.180.0.1)
allow List/CSV List of extra privileged entitlement (e.g., network.host,security.insecure)
annotations List List of annotation to set to the image
attests List List of attestation parameters (e.g., type=sbom,generator=image)
builder String Builder instance (see setup-buildx action)
build-args List List of build-time variables
build-contexts List List of additional build contexts (e.g., name=path)
cache-from List List of external cache sources (e.g., type=local,src=path/to/dir)
cache-to List List of cache export destinations (e.g., type=local,dest=path/to/dir)
cgroup-parent String Optional parent cgroup for the container used in the build
context String Build's context is the set of files located in the specified PATH or URL (default Git context)
file String Path to the Dockerfile. (default {context}/Dockerfile)
labels List List of metadata for an image
load Bool Load is a shorthand for --output=type=docker (default false)
network String Set the networking mode for the RUN instructions during build
no-cache Bool Do not use cache when building the image (default false)
no-cache-filters List/CSV Do not cache specified stages
outputs List List of output destinations (format: type=local,dest=path)
platforms List/CSV List of target platforms for build
provenance Bool/String Generate provenance attestation for the build (shorthand for --attest=type=provenance)
pull Bool Always attempt to pull all referenced images (default false)
push Bool Push is a shorthand for --output=type=registry (default false)
sbom Bool/String Generate SBOM attestation for the build (shorthand for --attest=type=sbom)
secrets List List of secrets to expose to the build (e.g., key=string, GIT_AUTH_TOKEN=mytoken)
secret-envs List/CSV List of secret env vars to expose to the build (e.g., key=envname, MY_SECRET=MY_ENV_VAR)
secret-files List List of secret files to expose to the build (e.g., key=filename, MY_SECRET=./secret.txt)
shm-size String Size of /dev/shm (e.g., 2g)
ssh List List of SSH agent socket or keys to expose to the build
tags List/CSV List of tags
target String Sets the target stage to build
ulimit List Ulimit options (e.g., nofile=1024:1024)
github-token String GitHub Token used to authenticate against a repository for Git context (default ${{ github.token }})

outputs

The following outputs are available:

Name Type Description
imageid String Image ID
digest String Image digest
metadata JSON Build result metadata

Troubleshooting

See TROUBLESHOOTING.md

Contributing

Want to contribute? Awesome! You can find information about contributing to this project in the CONTRIBUTING.md

build-push-action's People

Contributors

agabani avatar akhilerm avatar alextes avatar andrew-waters avatar andygrunwald avatar baibaratsky avatar bendrucker avatar beyondevil avatar chris-crone avatar cpswan avatar crazy-max avatar danlester avatar dependabot[bot] avatar dud225 avatar dvalentiate avatar dvdksn avatar elias-lundgren avatar gkkachi avatar itchyny avatar jedevc avatar jesserockz avatar jtomaszewski avatar kmconner avatar liboz avatar malkam03 avatar mathieubergeron avatar ruslandoga avatar syntaqx avatar tonistiigi avatar zappy-shu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

build-push-action's Issues

Add *complete* example workflow yaml file

Hello,
I am adding this Action via this PR (nf-core/kmermaid#56) and can't seem to get it to work (yaml below). Can you provide a complete minimal working example to guide the way for new users? Thank you!
Warmest,
Olga

name: Build Docker images
# This workflow tests building the docker image so I stop committing
# bad Dockerfiles and breaking the build
on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Build Docker images
        uses: docker/[email protected]
        with:
          repository: nfcore/kmermaid
          push: false

Feature request: Timestamp based tag

I would like to have some time stamp based tag - built in. Can be done using (date +%s) or similar.
I could try to contribute it myself, but I couldn't find out where the actual script that run :(

"tags" not required, but omitting it doesn't do what you'd expect

The tags input is currently optional...

tags:
    description: Comma-delimited list of tags. These will be added to the registry/repository to form the image's tags
    required: false

... but when it's omitted, no images are actually pushed to the registry. You'll typically see something like this in the logs:

...
Successfully built 36c976682bcd
Pushing image []

The workflow will succeed, since technically it was successful in pushing 0 images, but that's a little misleading. It would be reasonable to expect it to push an image with the :latest default tag, like the Docker CLI does when there's no tag - e.g.

$ docker build org/repo .
$ docker push org/repo 

It would seem sensible here to either make tags a required input (explaining in the comment that latest needs be specified explicitly, if so desired) or implement a default tag latest, analogous to the Docker CLI.

Can't set build_arg from env

I've tried to set a build_arg from an env I set earlier with Github Actions, but I seem to be unable to. Am I doing something wrong, or is there a real issue here?

jobs:
  deliver-docker:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Determine version
        id: version
        run: echo "::set-env name=PACK_VERSION::`echo ${{ github.event.release.tag_name }} | cut -d "v" -f2`"
        shell: bash
      - name: Build and Push Image
        uses: docker/build-push-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
          repository: dfreilich/pack
          tags: latest
          dockerfile: .github/workflows/delivery/docker/Dockerfile
          build_args: PACK_VERSION=`${{ env.PACK_VERSION }}

Example for Cache-Froms option

Can an example of how to use the cache-froms option in a github workflow be added to the documentation? I was unable to figure out a way to use it that actually took advantage of the option. Looking at the go utility code, it just adds the option to the build command, but that will do nothing when there is no local cache (ie a CB scenario like this). In my experience you need to pull the image first and the original issue asks for that as well. I did some testing and was unable to get it to work. It was indistinguishable from not using it.

- name: Docker Build
  uses: docker/build-push-action@v1
  with:
    registry: quay.io
    repository: ${{ env.quayio_repo }}
    username: ${{ env.quayio_user }}
    password: ${{ secrets.QUAY_AUTH }}
    tags: >-
      ${{ env.docker_tag }},
      ${{ env.docker_cache_tag }}
    cache_froms: ${{ env.quayio_repo }}:${{ env.docker_cache_tag }}

My original comment from the original issue.

github action pushes smaller image on docker hub than when pushing from local machine

I created a docker image for a monorepo using node:10 base docker image.
When I build it locally and push it on docker hub, I get, on docker hub, an image with size 616.11mb and when I run the service it is working fine.
Using the github action the image after pushing has size 572.49 mb and when I start the service it displays only a white page on the browser.

https://hub.docker.com/repository/docker/satapps/terriamap/tags?page=1
https://github.com/SatelliteApplicationsCatapult/TerriaMap

Any idea why ?
Thanks

Docker login to private registry prior to build

Hi,
Thanks for the GitHub action! :)
I am trying to build an image based on private image which is available in same repo we use to push.

Currently it looks like 'login' to the private registry happens before 'push' but after 'build' steps.
Is it possible to shift the 'login' step to happen 'build' step?

Thanks!

Add 'v1' tag instead of 'v1.0'

In README.md and marketplace, uses: docker/build-push-action@v1 but v1 does not exists.

Download action repository 'docker/build-push-action@v1'
##[warning]Failed to download action 'https://api.github.com/repos/docker/build-push-action/tarball/v1'. Error Response status code does not indicate success: 404 (Not Found).
##[warning]Back off 19.862 seconds before retry.
##[warning]Failed to download action 'https://api.github.com/repos/docker/build-push-action/tarball/v1'. Error Response status code does not indicate success: 404 (Not Found).
##[warning]Back off 22.87 seconds before retry.
##[error]Response status code does not indicate success: 404 (Not Found).

Currently only worksuses: docker/[email protected]

Pull docker/github-actions:v1.04s
82dd17ef2c28: Download complete
Pull down action image 'docker/github-actions:v1.0'
...

FYI: Official action's taggs: https://github.com/actions/checkout/tags

Default to using BuildKit

I'm finding myself always needing to remember to add the following to my actions:

env:
        DOCKER_BUILDKIT: 1

I wonder if this should be a first-class part of the interface, rather than needing to know the environment variable? I also wonder if this should default to using BuildKit?

[Feature] Pass tags and labels as build args as well

The action computes lots of useful values, for example it creates tags using options like tag_with_ref or tag_with_sha; and it adds many useful labels with add_git_labels

Feature Request

What's missing, however, is an option that allows passing all those values as build arguments, so we can make use of them in our dockerfile.

Since all the information is already there, the feature boils down to just adding one or two more options:

  • Maybe a simple bool
    pass_git_labels_as_build_args: true (or maybe something shorter :P)
  • Or maybe allow users to select what to pass
    computed_build_args: [ "git_ref", "git_sha", "org.opencontainers.image.created" ]

Use case

Most applications need to know their version/build time, so they can display it somewhere (in the header of their commandline output, or "about box", ...)

Repository/image name should be lowercased automatically.

Name components may contain lowercase letters, digits and separators. A separator is defined as a period, one or two underscores, or one or more dashes. A name component may not start or end with a separator.
Src: https://docs.docker.com/engine/reference/commandline/tag/#extended-description.

I have no idea if the same rule applies to username or registry, but I have a failing build due to this. Note that repository name is feeded with ${{ github.repository }}/gateway. Currently, there is no toLower function in Actions.

2020-04-26T12:49:01.8839602Z ##[group]Run docker/build-push-action@v1
2020-04-26T12:49:01.8839748Z with:
2020-04-26T12:49:01.8839835Z   username: 0xbkt
2020-04-26T12:49:01.8840486Z   password: ***
2020-04-26T12:49:01.8840585Z   repository: 0xbkt/rEdAcTeD/gateway
2020-04-26T12:49:01.8840655Z   registry: docker.pkg.github.com
2020-04-26T12:49:01.8840739Z   tags: latest
2020-04-26T12:49:01.8840819Z   tag_with_ref: false
2020-04-26T12:49:01.8840902Z   tag_with_sha: false
2020-04-26T12:49:01.8840968Z   path: .
2020-04-26T12:49:01.8841048Z   always_pull: false
2020-04-26T12:49:01.8841130Z   add_git_labels: false
2020-04-26T12:49:01.8841225Z   push: true
2020-04-26T12:49:01.8841290Z ##[endgroup]
2020-04-26T12:49:01.8867757Z ##[command]/usr/bin/docker run --name dockergithubactionsv1_68dc5e --label c27d31 --workdir /github/workspace --rm -e INPUT_USERNAME -e INPUT_PASSWORD -e INPUT_REPOSITORY -e INPUT_REGISTRY -e INPUT_TAGS -e INPUT_TAG_WITH_REF -e INPUT_TAG_WITH_SHA -e INPUT_PATH -e INPUT_DOCKERFILE -e INPUT_TARGET -e INPUT_ALWAYS_PULL -e INPUT_BUILD_ARGS -e INPUT_CACHE_FROMS -e INPUT_LABELS -e INPUT_ADD_GIT_LABELS -e INPUT_PUSH -e HOME -e GITHUB_JOB -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_REPOSITORY_OWNER -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/rEdAcTeD/rEdAcTeD":"/github/workspace" docker/github-actions:v1  "build-push"
2020-04-26T12:49:05.7322652Z Logging in to registry docker.pkg.github.com
2020-04-26T12:49:05.7555246Z WARNING! Using --password via the CLI is insecure. Use --password-stdin.
2020-04-26T12:49:05.8533235Z WARNING! Your password will be stored unencrypted in /github/home/.docker/config.json.
2020-04-26T12:49:05.8533483Z Login Succeeded
2020-04-26T12:49:05.8533831Z Configure a credential helper to remove this warning. See
2020-04-26T12:49:05.8534604Z https://docs.docker.com/engine/reference/commandline/login/#credentials-store
2020-04-26T12:49:05.8534726Z 
2020-04-26T12:49:05.8547653Z Building image [docker.pkg.github.com/0xbkt/rEdAcTeD/gateway:latest]
2020-04-26T12:49:05.8721089Z invalid argument "docker.pkg.github.com/0xbkt/rEdAcTeD/gateway:latest" for "-t, --tag" flag: invalid reference format: repository name must be lowercase
2020-04-26T12:49:05.8721637Z See 'docker build --help'.
2020-04-26T12:49:05.8734269Z Error: exit status 125
2020-04-26T12:49:05.8738409Z Usage:
2020-04-26T12:49:05.8738519Z exit status 125
2020-04-26T12:49:05.8739333Z   github-actions build-push [flags]
2020-04-26T12:49:05.8739585Z 
2020-04-26T12:49:05.8739769Z Flags:
2020-04-26T12:49:05.8740834Z   -h, --help   help for build-push
2020-04-26T12:49:05.8741420Z 

Pushing with image variants

I would like, in addition to tags, add a variant part to docker image, like:

  • metacontrollerio/metacontroller:v4.2.0-alpine
  • metacontrollerio/metacontroller:v4.2.0-debian

etc. What is the easiest way to do it ?

Logging in to two Docker registries?

I'm trying to build a docker container, from a private container on dockerhub, and push it to the github registry.

For public dockerhub packages, this works splendidly

  docker-rabbitmq:
    runs-on: ubuntu-latest
    needs: lerna
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2
        with:
          ref: ${{ github.head_ref }}
      - name: Build the container
        uses: docker/build-push-action@v1
        with:
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
          registry: docker.pkg.github.com
          repository: xxx/yyy/rabbitmq
          tag_with_ref: true
          dockerfile: ./packages/yyy/containers/rabbitmq/Dockerfile
          cache_froms: xxx/yyy/rabbitmq:latest
          path: ./packages/yyy/containers/rabbitmq/

But for a private container, this fails because we are not logged in to DockerHub.

I tried to add the following before the build-push-action, but that does not get picked up

      - uses: azure/docker-login@v1
        with:
          username: "xxx"
          password: "xxx"

Any ideas on how to do this?

COPY failed: no such file or directory

First of all: Thank you very much for your work on this GitHub actions. It looks very promising, but I could not get it to work so far.

Here is my configuration:

   - name: Build and Push Docker Image
      uses: docker/build-push-action@v1
      with:
          dockerfile: ./backend/guide/Dockerfile
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
          registry: docker.pkg.github.com
          repository: some/repo
          tags: latest

Here is the Dockerfile:

FROM openjdk:11-jre-slim
COPY ./core/build/libs/core.jar core.jar
ENTRYPOINT ["java","-jar","/core.jar"]

I keep on getting, the following error:

Step 2/3 : COPY core/build/libs/core.jar core.jar
COPY failed: stat /var/lib/docker/tmp/docker-builder622038188/core/build/libs/core.jar: no such file or directory

I've also tried the following:

FROM openjdk:11-jre-slim
COPY core/build/libs/core.jar core.jar
ENTRYPOINT ["java","-jar","/core.jar"]

But it did not work either.

When I ssh into the runner machine, I can use docker build just find, and everything works.

Can somebody spot my mistake? I'm close to losing my mind :)

Thanks.

Please consider adding input safelisting

GitHub Actions can be vulnerable to environment injection for optional inputs, see my article here:
https://francoisbest.com/posts/2020/the-security-of-github-actions

One form of defence against that for now is to add another input that lists input names that are safe to load (because explicitly defined by the user in their workflow).

Example:

uses: docker/build-push-action@v1
with:
  username: ${{ secrets.DOCKER_USERNAME }}
  password: ${{ secrets.DOCKER_PASSWORD }}
  repository: myorg/myrepository
  tags: latest
  inputsSafeList: username,password,repository,tags

In this example, if a malicious action defines and exports INPUT_REGISTRY, it would be ignored as registry is not part of the safelist. Without it, the image could be pushed to a registry controlled by the attacker.

Remove `v` from tags

I think the action should have the option to remove the v from git tags that are semver. And I think it should be the default behavior.

Git tag: v1.1.1 -> Docker tag: 1.1.1

Update Readme and description with push on Docker hub

Currently, if we use the Github action with this repo, the image is built and pushed to the Docker hub, but the Readme and description are not updated on the Docker hub webpage. It would be nice to have this in the future to use this action and altogether disable auto build.

`--cache_froms` from a gcr.io private registry: auth error during pull

My repo contains multiple images and I am trying to prevent some of them to be built if the underlying Dockerfile and related files do not change. For this I tried to use BUILDKIT_INLINE_CACHE, cache_froms and always_pull.

From the logs, login to the private registry works well as well as pushing the image(s). Caching fails because the cache image cannot be pulled from the registry. See the ERROR log:

2020-06-18T13:38:46.0362521Z Logging in to registry gcr.io
2020-06-18T13:38:46.0655216Z WARNING! Using --password via the CLI is insecure. Use --password-stdin.
2020-06-18T13:38:46.2805517Z WARNING! Your password will be stored unencrypted in /github/home/.docker/config.json.
2020-06-18T13:38:46.2806701Z Login Succeeded
2020-06-18T13:38:46.2823664Z Configure a credential helper to remove this warning. See
2020-06-18T13:38:46.2846885Z https://docs.docker.com/engine/reference/commandline/login/#credentials-store
2020-06-18T13:38:46.2848444Z 
2020-06-18T13:38:46.2902754Z Building image [gcr.io/my-project-id/my-image:latest gcr.io/my-project-id/my-image-ca3422b]
2020-06-18T13:38:46.4792304Z #1 [internal] load .dockerignore
2020-06-18T13:38:46.4792661Z #1 transferring context: 2B done
2020-06-18T13:38:46.4792982Z #1 DONE 0.0s
2020-06-18T13:38:46.4793265Z 
2020-06-18T13:38:46.4793549Z #2 [internal] load build definition from Dockerfile
2020-06-18T13:38:46.4793869Z #2 transferring dockerfile: 554B done
2020-06-18T13:38:46.4794173Z #2 DONE 0.0s
2020-06-18T13:38:46.4794317Z 
2020-06-18T13:38:46.4794611Z #3 [internal] load metadata for docker.io/library/ubuntu:20.04
2020-06-18T13:38:47.0797488Z #3 DONE 0.6s
2020-06-18T13:38:47.0798118Z 
2020-06-18T13:38:47.0854806Z #5 [1/5] FROM docker.io/library/ubuntu:20.04@sha256:52259450119427dab05c0c4...
2020-06-18T13:38:47.0861553Z #5 DONE 0.0s
2020-06-18T13:38:47.0861844Z 
2020-06-18T13:38:47.0862243Z #7 [internal] load build context
2020-06-18T13:38:47.0862666Z #7 DONE 0.0s
2020-06-18T13:38:47.0862893Z 
2020-06-18T13:38:47.0864116Z #4 importing cache manifest from my-project-id/my-image:latest
2020-06-18T13:38:47.2298524Z #4 ERROR: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed
2020-06-18T13:38:47.2299344Z 
2020-06-18T13:38:47.2299694Z #7 [internal] load build context
2020-06-18T13:38:47.2300054Z #7 transferring context: 855B done
2020-06-18T13:38:47.2300405Z #7 DONE 0.0s

The error is ERROR: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed but pushing works so I am pretty sure scope is correctly setup on the Google IAM side. Any ideas?


name: Build and Push Docker Images

env:
  PROJECT_ID: my-project-id

on:
  push:
    branches:
      - master
    tags:
      - "*"
  pull_request:

jobs:
  build-images:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Build my-image
        uses: docker/[email protected]
        env:
          DOCKER_BUILDKIT: 1
        with:
          repository: ${{ env.PROJECT_ID }}/my-image
          path: base
          build_args: BASE_IMAGE=ubuntu:20.04,BUILDKIT_INLINE_CACHE=1
          username: _json_key
          password: ${{ secrets.GCR_PUSH_KEY }}
          registry: gcr.io
          add_git_labels: true
          tag_with_ref: true
          tag_with_sha: true
          push: true
          cache_froms: ${{ env.PROJECT_ID }}/my-image:latest
          always_pull: true
ARG BASE_IMAGE="ubuntu:20.04"

FROM ${BASE_IMAGE}

ARG DEBIAN_FRONTEND=noninteractive

RUN apt update --fix-missing && \
    apt install --no-install-recommends -y \
    wget bzip2 ca-certificates libglib2.0-0 libxext6 \
    libsm6 libxrender1 git mercurial subversion && \
    apt clean

ENV CONDA_PATH "/opt/conda"
ENV PATH "$CONDA_PATH/bin:$PATH"

ADD ./bootstrap.sh /bootstrap.sh
ADD ./entrypoint.sh /entrypoint.sh
RUN bash /bootstrap.sh

SHELL ["/bin/bash", "--login", "-c"]
ENTRYPOINT ["/entrypoint.sh"]

Question: Does the docker build check the repository?

I am using this action to build an image with a lot of dependencies in it for the application I am developing. These several dependencies take a long time to build and install onto the docker image, and therefore the docker image takes a long time to build.

When I am developing the docker image locally, the docker cache stores the intermediate layers so that it only needs to rebuild the layers after and including any changes I made. I am wondering if this action has a similar feature. Does it pull down the image from dockerhub to see which layer to start from?

Thank you

Automatic creation of staggered versioned tags

I'd would be cool if there was a option for this action to automatically split the version of the git reference version up and create multiple docker tags based on the major and minor version numbers of the git reference.

For example: refs/tags/v1.3.1 would turn into

  • v1.3.1
  • v1.3
  • v1

Sorry don't know if there is a proper name for this practice πŸ˜… But this seems to be common among the more popular images.

Leverage docker cache

Would it possible to add support for using docker cache?

Especially for PR validation, it would be great if it would be possible to specify a docker image that would be used for resolving layer cache. That image name would be passed to a docker build option:

--cache-from strings      Images to consider as cache sources

Also the image should be pulled from the registry first.

Feature Request: Ability to check for existing image via manifest and retag

This GHA is a good start; I'd like to suggest for the ability to check for existing docker image on the registry without downloading or building a local image by directing using docker registry API.

This is especially useful when doing a CI/CD workflow where you promote non-master to master; so there isn't an actual need to rebuild docker image but simply re-tag existing docker image.

docs: Change use of "Name" to "Path" in dockerfile option information

Hi. Thanks very much for making this GitHub Action!

I'd like to suggest that in the dockerfile option the text

Name of the Dockerfile

be changed to

Path to the Dockerfile relative to path.

When I first read the docs I got confused by the use of "path" and "name" (instead of "context" and "path") and for a directory that structure that is structured with

$ tree
.
β”œβ”€β”€ other_files_needed_for_build
β”œβ”€β”€ docker
β”‚Β Β  └── Dockerfile

attempted to use

    - name: Build and Publish to Registry
      uses: docker/build-push-action@v1
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
        repository: org/repo-name
        path: docker
        dockerfile: Dockerfile

instead of the following (correct)

    - name: Build and Publish to Registry
      uses: docker/build-push-action@v1
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
        repository: org/repo-name
        dockerfile: docker/Dockerfile

I think the use of "path" would be more clear than "name". If you agree with me I have a branch with this change on my fork that I can open a PR here on (though telling me to do so might involve more typing).

Feature request: provide way to add --ssh argument to docker build command

There does not appear to be a way to turn on the newish --ssh forwarding capability of docker build with this action. This is needed to access private github repos from inside the Dockerfile (etc). If this is in some way available, it is not documented.

Some way to place the ssh key(s) (or inject them from a repo secret) and get them to work with RUN --mount=type=ssh would be great. If this ability is already present, then documentation would be greate!

Example of multistep workflow

How to use build artifact from previous workflow step?

I did not find examples
My script does not work (does not see the artifact collected in the previous step)

Thanks!

What does `always_pull` do?

My guess is that pulling a previously built image will facilitate layer-reusing, like using the old image as a cache. Am I right?

Possible issue when building with Tini

I have the following dockerfile

FROM python:3.8-slim

# Packages that we need
COPY requirements.txt /app/
WORKDIR /app

# build the venv and install the requirements
RUN python3 -m venv app_env
RUN pip install -r requirements.txt

# use TINI
ENV TINI_VERSION v0.19.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini
RUN chmod +x /tini
ENTRYPOINT ["/tini", "--", "myapp"]
CMD "-h"

When I run it on my computer with
docker build -t dockerfile .

I am able to run and see the help of the application with the command
docker run -t dockerfile

However, if I pull the container from github, with the command
docker run -t docker.pkg.github.com/myrepo/myapp

I have an error:
[FATAL tini (7)] exec myapp failed: No such file or directory

I got the same error, no matter what argument is passed to the container

The rule that I use in the github action is

        uses: docker/build-push-action@v1
        with:
          dockerfile: ./docker/dockerfile
          registry: docker.pkg.github.com
          repository: ${{ github.repository }}/myapp
          tags: latest
          username: ${{ github.ref }}
          password: ${{ secrets.GH_TOKEN }}

can this be used with GitHub package registry?

Hi,

I'm currently trying to figure out if this action can be used with GitHub package registry. So this was my attempt:

      with:
        username: ${{ env.GITHUB_ACTOR }}
        password: ${{ secrets.GITHUB_TOKEN }}
        registry: docker.pkg.github.com
        ...

However, this nonetheless fails with

Error: both username and password must be set to login

I suspect the username is the problem, however this is how this docker action is authenticating: https://github.com/matootie/github-docker/blob/master/index.js#L39

I do not want to add a personal access token so I could publish this with my username and my personal access token, although that would probably work.

Does anyone know how to make this work with the injected GITHUB_TOKEN?

Experiencing unknown blob error on push

I have a workflow containing multiple jobs building and pushing images to Github Packages.
This works great most of the time, but sometimes a random job is failing during docker push with an unknown blob error.

The jobs are declared like this:

  build-maintenance:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: docker/build-push-action@v1
        with:
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
          registry: docker.pkg.github.com
          repository: XXX/YYY/maintenance
          path: maintenance
          target: production
          tag_with_sha: true

Tail of job output:

Successfully built c6ac9d5faeba
Successfully tagged docker.pkg.github.com/XXX/YYY/maintenance:sha-eac817d
Pushing image [docker.pkg.github.com/XXX/YYY/maintenance:sha-eac817d]
The push refers to repository [docker.pkg.github.com/XXX/YYY/maintenance]
1e64616ff473: Preparing
02e9da9d44c5: Preparing
3810cc0c140f: Preparing
3e207b409db3: Preparing
3e207b409db3: Layer already exists
3810cc0c140f: Layer already exists
unknown blob
Error: exit status 1
exit status 1
Usage:
  github-actions build-push [flags]

Flags:
  -h, --help   help for build-push

Add build flag similar to push flag

At present it's possible to set push to false and just build an image. This is useful when you want to test the image before pushing it. But when you do that, pushing becomes either a matter of doing so manually (potentially with multiple tags), or triggering the full action again, including a build.

It would be useful to run just the push action, maybe by adding a property for build that defaults to true?

allow only build phase

we would love it if we could skip the publish part, so we can create a pipeline that only runs a test stage in a multistage dockerfile

Enable use of non-master branch as "latest"

From the tag_with_ref docs:

If {branch-name} is master then the tag will be latest.

We use a workflow that has separate master and dev branches, with strict rules around the interaction between them. Anything that requires a rebuild of the image (i.e. updating dependencies, rather than source files) must go through dev first. We have set dev as our main branch in Github, and it's locked, with controls around how devs can push to it.

We would like to have the latest tag always applied to the dev branch.

Appreciate we may be outliers here, but if it was possible to add a setting, it would really make a difference for us. I'll dig around myself and if I can see how to do it, will submit a PR.

How to create a presonal registry and allow to push by github actions.

I created a docer registry but have no https certificate, and I don't known how to push automatic by github actions.

I can push image locally if I configer in /etc/docker/daemon.json

{
    "insecure-registries": [
        "xxxx:5000"
    ]
}

But I don't know how to push it when I use the Github Actions, I tried to edit the /etc/docker/daemon.json at the workflows, but It told me Permission denied.

image

Push image directly:
image

Best practice for environment variables

Sorry for creating an issue for a question but there is no other place where I can ask a question.

How environment variables should be set? Can I simply use the env setup of the Github's workflow or do I have to use the ENV command in the Dockerfile?

Add tag_prefix input

I think an input for tag_prefix would be very useful in some usecases. If given, this value would be prefixed to all tags on on the image. With the current inputs, the tag_prefix would be prefixed to the following tag inputs. Note that I've included tags here as wel as I think that would be most consistent, but that is the one I'm the least sure about whether that would make sense.

One example, which is my usecas for this input and is something the official ubuntu image tags reflect as well.

When we have one GitHub repository with multiple Dockerfiles (see mine as example here) to build different images (Ubuntu versions in my case). Let's say we have the following images we want to build.

  • bionic
  • xenial
  • focal

WIth the tag_prefix with the values of the list above and e.g. a SHA of abc1234, these images would get the following tags.

  • bionic-sha-abc1234
  • xenial-sha-abc1234
  • focal-sha-abc1234

I hope I explained everything in here correctly, but I'd be happy to elaborate and discuss.

Edit:
I've locally tested this and it should work as described above.

input_tag_prefix.patch.txt

No examples of `path` or `dockerfile` options

I'm working on a project that has multiple Dockerfiles, each of them in a separate directory. One of those is named api, so the dockerfile is in api/Dockerfile. I have the following in my workflow:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: docker/build-push-action@v1
      with:
        path: api
        username: companyusername
        password: companypassword
        repository: company/myproject/api
        registry: companyregistry
        tags: latest

I get the following error:

unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /github/workspace/api/Dockerfile: no such file or directory

However, I see the checkout action logging the following:

Initializing the repository
  /usr/bin/git init /home/runner/work/myproject/myproject

From the documentation it is unclear that I should be using some variable to determine /home/runner/work/myproject/myproject instead of /github/workspace/. What am I doing wrong?

I also tried using dockerfile:, but also with the same results: always relative to /github/workspace.

New boolean input tag_with_latest

New boolean input tag_with_latest

It would add the ability for users to better set when to push as latest, such as evaluating an expression for example.

Example: tag as latest when push git tags

- uses: docker/build-push-action@v1
  with:
    username: ${{ secrets.DOCKER_USERNAME }}
    password: ${{ secrets.DOCKER_PASSWORD }}
    repository: felipecassiors/argbash-test
    add_git_labels: true
    # Only push as latest when push a git tag
    tag_with_latest: ${{ startsWith(github.ref, 'refs/tags/') }}
    tag_with_ref: true
    tag_with_sha: true

Usage with tag_with_ref

If tag_with_latest property is defined alongside tag_with_ref, then the strategy of push as latest when the branch is master from tag_with_ref should be ignored.

Where is the source code of this action?

Sorry, I am probablyΒ blind but I can't find the source code of this action. Where is it? For example where is code for tag_with_ref tags add? It would be nice to link to this in README.md.

Return image digest as action output

To better support image pinning, this action should output the image digest after pushing to a registry. The digest could then be used as a parameter for a future step in the workflow, like triggering a deployment.

Not worked with ECR actions

After ECR login action, can pull and push images from ECR repository on run docker command directly.
Maybe it required to support local ~/.docker/config.json
But cannot pull and push on docker/build-push-action caused by no basic auth credentials error.
My workflow is

  build_and_push_image:
    name: Build and push docker image to ECR.
    runs-on: ubuntu-latest
    steps:
      - name: Check out
        uses: actions/checkout@v2
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: **********
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1
      - name: Pull from ECR (pull test)
        run: docker pull ${{ steps.login-ecr.outputs.registry }}/${{ secrets.REGISTRY }}:latest
      - name: Debug auth (pull test)
        run: cat ~/.docker/config.json
      - name: Build & Push
        uses: docker/build-push-action@v1
        with:
          repository: ${{ steps.login-ecr.outputs.registry }}/${{ secrets.REGISTRY }}
          add_git_labels: true
          tag_with_ref: true
      - name: Logout of Amazon ECR
        if: always()
        run: docker logout ${{ steps.login-ecr.outputs.registry }}

Successfully pull on command line: Pull from ECR (pull test)

Pull from ECR (pull test)6s
***.dkr.ecr.us-east-1.amazonaws.com/***:latest

...

18ebb058d5da: Pull complete
Digest: sha256:ac4754ea1154010603db8d7cbe07bb1a33954e59b088efab46445c69d8b0fc58
Status: Downloaded newer image for ***.dkr.ecr.us-east-1.amazonaws.com/***:latest
***.dkr.ecr.us-east-1.amazonaws.com/***:latest

Logged in to ECR: Debug auth (pull test)

Run cat ~/.docker/config.json
{
	"auths": {
		"***.dkr.ecr.us-east-1.amazonaws.com": {
			"auth": "***"
		}
	},
	"HttpHeaders": {
		"User-Agent": "Docker-Client/3.0.11+azure (linux)"
	}
}

Failed to push or pull on docker/build-push-action@v1

...

Successfully built a60891a407a2
Successfully tagged ***.dkr.ecr.us-east-1.amazonaws.com/***:topic-use_original_docker_actions
Pushing image [***.dkr.ecr.us-east-1.amazonaws.com/***:topic-use_original_docker_actions]
The push refers to repository [***.dkr.ecr.us-east-1.amazonaws.com/***]
no basic auth credentials
Error: exit status 1
Usage:
  github-actions build-push [flags]

Flags:
  -h, --help   help for build-push

exit status 1

Did't push the image to github package, but workflow completed job

Hello, I am trying to integrate maven build and docker push. There are no errors after workflow script execution, but there is no image in my package.

Workflow script:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Push to GitHub Packages
uses: docker/build-push-action@v1
with:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: docker.pkg.github.com
repository: bwhyman/githubactions-examples/githubactions
tag: latest
logs
image

And the following script by using docker command works fine.
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: docker push
run: |
docker login -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }} docker.pkg.github.com
docker build . -t docker.pkg.github.com/bwhyman/githubactions-examples/githubactions
docker push docker.pkg.github.com/bwhyman/githubactions-examples/githubactions

Did I miss something??
Thanks a lot

Feature Request: skip Push on Unchanged Digest

I have a monorepo with multiple docker images and I would like to publish an image only if the digest of the image is not present in the registry, regardless of the tag. I'm using fluxcd and right now because a tag is pushed on every push to master, all the pods are being restarted.

I suggest the option skipUnchangedDigest to the build-push-action and when set to true the action will skip the push if the digest already present.

Bazel build supports this with skipUnchangedDigest flag to the container_push rule, code is here:
https://github.com/bazelbuild/rules_docker/blob/06c5419265e84baf168ba4d3982f45fe1fe312b4/container/go/cmd/pusher/pusher.go#L132
https://github.com/bazelbuild/rules_docker/blob/06c5419265e84baf168ba4d3982f45fe1fe312b4/container/go/cmd/pusher/pusher.go#L163

Potential security issue

When using this action, the following warning is displayed and it states that secrets are visible inside the container in plaintext in /github/home/.docker/config.json. I am aware that action containers are ephemeral, but isn't this file accessible to subsequent executed actions?

15 Logging in to registry 16 WARNING! Using --password via the CLI is insecure. Use --password-stdin. 17 WARNING! Your password will be stored unencrypted in /github/home/.docker/config.json. 18 Configure a credential helper to remove this warning. See 19 https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.