Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Running a build with empty build args builds and pushes an image with no name #80

Closed
br3ndonland opened this issue Jul 26, 2020 · 7 comments

Comments

@br3ndonland
Copy link

br3ndonland commented Jul 26, 2020

Description

I recently ran this action in a workflow, with my repo name nicely lowercased (#37), but had one of my build args configured incorrectly. Note the environment variable BUILD_env=test, which should have been BUILD_ENV=test. My Dockerfile has default build args set (like ARG BUILD_ENV=development), but the build arg here was probably overwriting that. My bad.

However, the action actually sets the image name to [], kicks off a Docker build, completes the build, and attempts to push an image named [].

Specific image name below replaced with org/repo/image.

Run docker/build-push-action@v1
  with:
    username: br3ndonland
    password: ***
    build_args: BUILD_ENV=
    registry: docker.pkg.github.com
    repository: org/repo/image
    tag_with_ref: false
    tag_with_sha: false
    path: .
    always_pull: false
    add_git_labels: false
    push: true
  env:
    DOCKER_REPO: org/repo
    BUILD_env: test

...

Logging in to registry docker.pkg.github.com

Login Succeeded
Building image []
Sending build context to Docker daemon  301.6kB

...

Removing intermediate container c6704dceaa3b
---> 2a565e64a9ba
Successfully built 2a565e64a9ba
Pushing image []

Suggestions

  • The action should correctly read image names.
  • If the action doesn't receive a name, it should stop and throw an error, rather than going through the entire build and push process.

Related

#37
#41

@mattwelke
Copy link

mattwelke commented Aug 9, 2020

I'm having this issue too. I started from the Cloud Run example maintained by GCP (https://github.com/GoogleCloudPlatform/github-actions/tree/master/example-workflows/cloud-run). It worked perfectly, but I figured it was a waste to use my Cloud Build minutes to build the image, since GitHub Actions was just waiting for Cloud Build to build the image. I figured I'd swap out the Cloud Build part of that workflow for a generic "build and push" step that would push to GCR. That led me to this action.

My workflow step follows the style in the README in this repo:

- name: Build and push Docker image to Google Container Registry
  uses: docker/build-push-action@v1.1.0
  with:
    username: _json_key
    password: ${{ secrets.RUN_SA_KEY }}
    registry: gcr.io
    repository: ${{ secrets.RUN_PROJECT }}/${{ env.SERVICE_NAME }}
    tags: ${{ env.GITHUB_SHA }}

(note that I added the "Cloud Storage Admin" role to the service account created when following the Cloud Run example README, so that my service account could be used to push the image to GCR too)

But, when it runs, it does the build but finishes with this in the logs:

Successfully built f8a13f5fd208
Pushing image []

And when I check GCR, I don't see a repository for the repository name I chose in this workflow.

My Dockerfile is something I grabbed when googling for a standard multi stage Go build:

# Use the official Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang
FROM golang:1.14 as builder

# Create and change to the app directory.
WORKDIR /app

# Retrieve application dependencies using go modules.
# Allows container builds to reuse downloaded dependencies.
COPY go.* ./
RUN go mod download

# Copy local code to the container image.
COPY . ./

# Build the binary.
# -mod=readonly ensures immutable go.mod and go.sum in container builds.
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server

# Use the official Alpine image for a lean production container.
# https://hub.docker.com/_/alpine
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine:3
RUN apk add --no-cache ca-certificates

# Copy the binary to the production image from the builder stage.
COPY --from=builder /app/server /server

# Run the web service on container startup.
CMD ["/server"]

I checked out #37 and my GitHub repo name has no uppercase characters, it's just eight lowercase characters with no separators, so I don't think it's related.

UPDATE:

After replacing that step in my workflow with a manual step that used the credential helper for GCR and built and pushed using Bash commands, it worked:

- name: "Build and push Docker image to Google Container Registry without action"
  run: |-
    gcloud auth configure-docker
    docker build -t gcr.io/${{ secrets.RUN_PROJECT }}/${{ env.SERVICE_NAME }}:latest .
    docker push gcr.io/${{ secrets.RUN_PROJECT }}/${{ env.SERVICE_NAME }}:latest

I ended up with the image in my project's GCR after this, and the logs display the image being pushed properly:

Successfully built 9a6e0deaeca2
Successfully tagged gcr.io/***/manydocs-gha:latest
The push refers to repository [gcr.io/***/manydocs-gha]
61b033c6261d: Preparing
241b44965f21: Preparing
50644c29ef5a: Preparing
50644c29ef5a: Layer already exists
241b44965f21: Pushed
61b033c6261d: Pushed
latest: digest: sha256:299a0abb6472a45d2e28b690a1479b0ea1dfcdfb550084b631fd2625e106b0ca size: 949

@br3ndonland
Copy link
Author

@mattwelke glad to hear you got it working. I was able to fix my builds as well, by correcting the build args in my case.

In terms of the preferred behavior for this action though, if the action doesn't receive a name, it should stop and throw an error, rather than wasting time going through the entire build and push process.

@mattwelke
Copy link

I think my issue is about a more core issue though. I don't see anything complex about my Dockerfile. I'm not using build args and as far as I can tell, I'm specifying the name of the image to be built properly. I don't understand why my push fails just because it's to GCR.

@crazy-max
Copy link
Member

Version 2 has been merged to the main branch and is therefore available via uses: docker/build-push-action@v2 (mutable tag).

As a reminder, this new version changes drastically and works with 3 new actions (login, setup-buildx and setup-qemu) that we have created. Many usage examples have been added to handle most use cases.

This new version should fix your issue. Don't hesitate if you have any questions.

@mickel8
Copy link

mickel8 commented Oct 21, 2021

Is there an example for defining Docker build-arg? I can't see anything like this.

I wanted to do something like this --build-arg=VERSION=$(git describe) but can't manage to include this in workflow

@crazy-max
Copy link
Member

@mickel8 #380 (comment)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@crazy-max @mattwelke @mickel8 @br3ndonland and others