actions / runner

The Runner for GitHub Actions :rocket:
https://github.com/features/actions
MIT License
4.89k stars 959 forks source link

Docker container action does not execute from the repository root #2017

Open discentem opened 2 years ago

discentem commented 2 years ago

Describe the bug

I have a dockerfile defined in dockerfiles/compile/Dockerfile.

Github Actions executes Docker from the directory where the Dockerfile is stored:

/usr/bin/docker build -t 4cd98f:f7c0bfc3fa7c515f8b9a964cbb495763 -f "/home/runner/work/hello-world-docker-action/hello-world-docker-action/./.github/workflows/actions/compile/../../../../dockerfiles/compile/Dockerfile" "/home/runner/work/hello-world-docker-action/hello-world-docker-action/dockerfiles/compile"

This causes my go build instruction in my Dockerfile to fail in Github Actions because my source files (namely go.mod) are not found in dockerfiles/compile/Dockerfile.

See this gist for the full log: https://gist.github.com/discentem/4deff7191e527f0210e79fedfaca4026

To Reproduce

Fork https://github.com/discentem/hello-world-docker-action and push this commit to a new branch to trigger CI.

Expected behavior

I would expect Docker to be executed from the root of my repository and not specifically from the directory where the Dockerfile is sourced from. Something like this:

/usr/bin/docker build -t 4cd98f:f7c0bfc3fa7c515f8b9a964cbb495763 -f "/home/runner/work/hello-world-docker-action/hello-world-docker-action/./.github/workflows/actions/compile/../../../../dockerfiles/compile/Dockerfile" "."

Or perhaps there should be a param to tell the runner what directory to run docker in

Workaround

If I copy my Dockerfile to the root of my repository on the fly and point the action to this copy, the action succeeds. This is because the Dockerfile is now being executed in a directory where go.mod and the go source files can be found.

Runner Version and Platform

Hosted, Ubuntu 20.04

OS of the machine running the runner? Linux

What's not working?

Please include error messages and screenshots.

Job Log Output

Failed job output

Runner and Worker's Diagnostic Logs

N/A

nikola-jokic commented 2 years ago

Hi @discentem,

Thank you for submitting this issue. I will mark this as an enhancement, since the issue here is that you don't have a way to control the context passed to the docker build for example.

discentem commented 2 years ago

Hey @nikola-jokic, thanks for the reply!

I am curious though, what is the expected way to use this feature? Is the workaround I settled on common?

discentem commented 1 year ago

Hi again @nikola-jokic, any updates on providing a param for context?

I'm also wondering whether there are alternative strategies that I should consider. Is there an env variable or symlink I could utilize to access the root of the Github repo from whatever arbitrary directory I'm in?

nikola-jokic commented 1 year ago

Hey @discentem,

I can't think of one to leverage docker like this, without changing the runner and possibly the workflow.yaml definition. One idea might be to get around this issue might be to have a Makefile. It is common for go projects to have makefiles, so you can leverage that. Other than that, until we change the workings of the docker actions in the runner, the only thing I can think of is to use runner container hooks.

discentem commented 8 months ago

Hi again, this documentation actually implies the working directory should already be set to $GITHUB_WORKSPACE (the root of the code repository): https://docs.github.com/en/actions/creating-actions/dockerfile-support-for-github-actions#workdir

In my experience that was not the case. However, should I simply be able to do cd $GITHUB_WORKSPACE to solve my issue (get back to the root of my project code) instead of copying the Dockerfile around?

AndresPinerosZen commented 6 months ago

Hello, I'm also having this issue.