docker / build-push-action

GitHub Action to build and push Docker images with Buildx
https://github.com/marketplace/actions/build-and-push-docker-images
Apache License 2.0
4.29k stars 551 forks source link

Action doesn't perform git checkout #528

Closed BeyondEvil closed 2 years ago

BeyondEvil commented 2 years ago

Behaviour

Expected behaviour

According to README:

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

Actual behaviour

Checkout doesn't happen.

Configuration

This

      - name: Build and push container
        uses: docker/build-push-action@v2
        with:
          context: ./app
          push: true
          tags: ${{ env.ECR_REGISTRY }}/proxyco/madmax-api:stable

fails with error:

/usr/local/bin/docker buildx build --tag ***.dkr.ecr.us-west-2.amazonaws.com/proxyco/madmax-api:stable --iidfile /tmp/docker-build-push-s6cfCu/iidfile --metadata-file /tmp/docker-build-push-s6cfCu/metadata-file --push ./app
error: unable to prepare context: path "./app" not found
Error: buildx failed with: error: unable to prepare context: path "./app" not found

I have to explicitly do a checkout first, for the build to be successful.

      - uses: actions/checkout@v2

      - name: Build and push container
        uses: docker/build-push-action@v2
        with:
          context: ./app
          push: true
          tags: ${{ env.ECR_REGISTRY }}/proxyco/madmax-api:stable

It's a private repo.

This is run on a self-hosted runner using ubuntu-latest.

What am I doing wrong?

actualben commented 2 years ago

You're specifying a path context string (./app) which overrides the described automagic.

It looks like you want to specify a subdir on your build. You can still have most of the automagic by specifying the context as a full git repo URL in the way described by the docker build docs' description of Git Repo URLs. You'd use one with a trailing :app. Something like:

context: https://github.com/proxyco/madmax-api.git#${{ env.GITHUB_REF }}:app

To make it a bit more reusable you can do something like:


jobs:
  buildo:
    runs-on: ubuntu-latest
    env:
      CONTEXT_SUBDIR: app
    steps:
      - name: Login to ECR
        uses: docker/login-action@v1
        with:
          # your ecr details here
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      - name: Build and push container
        uses: docker/build-push-action@v2
        with:
          context: "${{ github.server_url }}/${{ github.repository }}.git#${{ github.ref }}:${{ env.CONTEXT_SUBDIR }}"
          push: true
          tags: ${{ env.ECR_REGISTRY }}/proxyco/madmax-api:stable
actualben commented 2 years ago

There is a related feature request in: https://github.com/docker/build-push-action/issues/460

BeyondEvil commented 2 years ago

Thanks for the response @actualben !

Also, thanks for the suggested workaround, although not pretty, haha. :)

I might take a stab at that feature request, if you'll allow me?

BeyondEvil commented 2 years ago

There is a related feature request in: #460

I went ahead and added a PR. Hope you approve! 😊

actualben commented 2 years ago

👍 I was working on a PR myself but you were much quicker! I've submitted mine as a friendly alternative.