pathyamlgithub-actionslocalcheckout

GitHub Actions: Use local action after checkout into different folder


I want my workflow to use a local action. Normally, one would do that like so:

jobs:
  my_first_job:
    steps:
      - name: Check out repository
        uses: actions/checkout@v3
      - name: Use local my-action
        uses: ./.github/actions/my-action

However, I want to checkout my repository in a different path:

- name: Check out repository
  uses: actions/checkout@v3
  path: ${{ env.WORK_DIR }}/local_checkout

This leads to the following error:

Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under '<mypath>/.github/actions/my-action'. Did you forget to run actions/checkout before running your local action?

I should note that my-action is not in a release or the main branch yet, only my current branch in which I am testing my pipeline. Unfortunately, something like uses: ./${{ env.WORK_DIR }}/local_checkout/.github/actions/my-action does not work, because env is unavailable in the context of uses.

So my question is: How do I use a local action if I checkout my repository in a different path than the default one?

jobs:
  my_first_job:
    steps:
      - name: Check out repository
        uses: actions/checkout@v3
        path: ${{ env.WORK_DIR }}/local_checkout
      - name: Use local my-action
        uses: ./${{ env.WORK_DIR }}/local_checkout/.github/actions/my-action

Solution

  • You need to add an extra step after "checkout" to copy the .github folder to right location:

         - name: Copy files
           working-directory: ${{ env.WORK_DIR }}/local_checkout
           run: cp -r .github "$GITHUB_WORKSPACE/"                                                                                                                    
    

    (if you have self-hosted runners which do not clear up their workspace, you might need to change this cp to rsync in delete mode to make sure no leftovers are there)