cd into directory prior to Docker step

Coming from GitLab CI/CD, I’m used to having a “before_script” step that CDs into my project’s “WebApplication1Core” folder (the Dockerfile is simply set up that way and I’d prefer not changing it/was having issues when I changed it).

How can this be achieved in GitHub Actions?

Simply adding a “run” step that did this didn’t work. The progress wasn’t retained and the Docker step was still run from the repository’s root directory.

steps:
    - uses: actions/checkout@v1
    - name: Sign into Docker registry
      uses: actions/docker/login@master
      env:
        DOCKER_USERNAME: ${{ secrets.DOCKER_USER }}
        DOCKER_PASSWORD: ${{ secrets.DOCKER_PW }}
        DOCKER_REGISTRY_URL: docker.pkg.github.com
    - name: Access Source directory
      run: cd WebApplication1Core
    - name: Ensure we are in the correct directory context
      run: pwd
    - name: Build Docker image
      uses: actions/docker/cli@master
      with:
        args: 'build -t docker.pkg.github.com/happy-ferret/ci-test-mr/webapplication1core:latest -f WebApplication1Core/Dockerfile .'
    - name: Push Docker image
      uses: actions/docker/cli@master
      env:
        DOCKER_USERNAME: ${{ secrets.DOCKER_USER }}
        DOCKER_PASSWORD: ${{ secrets.DOCKER_PW }}
        DOCKER_REGISTRY_URL: docker.pkg.github.com
      with:
        args: 'push docker.pkg.github.com/happy-ferret/ci-test-mr/webapplication1core:latest'

By the time I run into the “pwd” step, the directory is already reset to the root folder. The Docker build step fails because it cannot find the Dockerfile. If I add the full path to the Docker file instead, something else breaks further down the line.

1 Like

Please note that the actions/docker runner has been deprecated in favor of the run command. Since Docker is pre-installed in the virtual environment, you should be able to simply do something like:

steps:
- name: Build Docker image
    run: |
      cd WebApplication1Core
      docker build -t docker.pkg.github.com/happy-ferret/ci-test-mr/webapplication1core:latest .
2 Likes

That did indeed do the trick. Thank you!