0

Within the action, how to get the ref (branch or version) of that action which has been called by a workflow?

In case I call my-action (v1) using:

steps:
  - uses: my-action@v1

I need to retrieve the value v1 in my-action. But ${{ github.action_ref }} only provides the ref of the caller (workflow), not the ref of the called action (my-action).

A solution would be to use in the workflow:

steps:
  - uses: my-action@v1
    with:
      ref: ${{ github.action_ref }}

But I would like to avoid asking the caller to specify this parameter and would prefer to retrieve it directly in the action itself. Is that possible?

I need to checkout the repository which contains the action into the caller repository:

runs: 
  using: composite 
  steps: 
  - name: Checkout current repository 
    uses: actions/checkout@v3 
    with: 
      path: main 
  - name: Checkout validation repository 
    uses: actions/checkout@v3 
    with: 
      repository: [ORG]/validation 
      ref: v1.5.3 
      path: validation 
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • Interesting question, I wonder why you'd want that ref. – jessehouwing May 03 '23 at 08:58
  • Because I need to checkout the repository which contains the action into the caller repository: ```` runs: using: composite steps: - name: Checkout current repository uses: actions/checkout@v3 with: path: main - name: Checkout validation repository uses: actions/checkout@v3 with: repository: [ORG]/validation ref: v1.5.3 path: validation ```` – Stephan Prat May 03 '23 at 09:05
  • 1
    The runner has already checked out the action's repo at that ref for you in its working folder. – jessehouwing May 03 '23 at 09:10
  • So I can use directly `${{ github.action_path }}`, thanks! – Stephan Prat May 03 '23 at 09:54
  • 1
    There are a few issues with `${{ github.action_path }}`, accessing the environment variable `${GITHUB_ACTION_PATH}` in bash or `$env:GITHUB_ACTION_PATH` in powershell is a lot safer: https://github.com/actions/runner/issues/716 – jessehouwing May 03 '23 at 12:00
  • I am in an action with bash shell but `${GITHUB_ACTION_PATH}` is not replaced. – Stephan Prat May 04 '23 at 13:14
  • Either $GITHUB_ACTION_PATH does not work with command: `working-directory: $GITHUB_ACTION_PATH` – Stephan Prat May 04 '23 at 13:20
  • `${GITHUB_ACTION_PATH}` should work in a bash shell *in a composite action*, just a `run:` in a normal workflow won't set this path. Can you share more of what your workflow looks like to allow us to help you better? You can't use the bash environment variable syntax to assign to an input of a step, but you should be able to `cd $GITHUB_ACTION_PATH` inside the run action without issues. – jessehouwing May 04 '23 at 13:35

1 Answers1

1

For a typescript/javascript based action, you should be able to grab the __dirname variable from node to get to the path where the action's git repo got checked out.

In that folder you should be able to run a git command to grab the current sha that was checked out.

git rev-parse HEAD

For Composite Actions an environment variable exists that should return the path where the repo was checked out:

GITHUB_ACTION_PATH The path where an action is located. This property is only supported in composite actions. You can use this path to access files located in the same repository as the action. For example, /home/runner/work/_actions/repo-owner/name-of-action-repo/v1

jessehouwing
  • 106,458
  • 22
  • 256
  • 341