Search code examples
github-actions

Obtaining the action reference from inside a composite/Node custom action


Are there any ways to obtain the action reference within a composite or Node action? If a user calls my action by using using: dloez/repo@v2 I would like to be able to obtain the v2.

At first I was trying to build a Node action, but the GitHub context exposed via @action/github does not seem to expose the action reference. Am I wrong in this? Is this possible?

Then I tried to use a composite action and then access github.action_ref but as stated in the documentation it can not be used inside composite actions directly.

I know I could pass the reference as an input for the action or by referencing github.action_ref on the env section when I call the action, but I would like to avoid users to specify the reference they are using.

Specifically I am building an application in Rust that uses semantic versioning, conventional commits and git tags to automate the version bumps of an application. Each time I push to the main branch of the application repository, a new tag and release with the different binaries of that version is created. Now I want to create a custom action that wraps my app so it can be easily executed on workflows. The action would:

  1. Download the binary of the same release as the action called. This means that if a user calls my action like dloez/repo@example I would need to download the binary for the runner uploaded to the example release. I would like to avoid hardcoding the version each action reference needs to download as the main purpose of this project is to avoid errors related to forgetting to upgrade the version somewhere.
  2. Execute the downloaded binary passing action inputs as arguments.

The method needs to work on Linux, macOS and Windows runners, so must be a JavaScript or composite action per the docs.


Solution

  • Well, it seems like I did not properly understand the documentation.

    As explained in this issue, to use github.action_ref you can use the env context of the steps inside a composite action. Examples:

    - name: Checkout Tag Track
      if: ${{ inputs.compile == 'true' }}
      uses: actions/checkout@v4
        with:
          repository: dloez/tag-track
          ref: ${{ env.ACTION_REF }}
          path: tag-track-repo
        env:
          ACTION_REF: ${{ github.action_ref }}
    
    - name: Generate download cache key
      if: ${{ inputs.compile == 'false' && inputs.use-cache == 'true' }}
      id: gen-download-cache-key
      shell: bash
      run: echo "download-cache-key=tag-track_download_${RUNNER_OS}_${RUNNER_ARCH}_${ACTION_REF}" >> $GITHUB_OUTPUT
      env:
        ACTION_REF: ${{ github.action_ref }}
    

    I originally thought that the unique way was to use the env context when calling the composite action:

    - uses: dloez/tag-track@github-action-research
      with:
        compile: true
        use-cache: true
        create-tag: true
      env:
        ACTION_REF: ${{ github.action_ref }}
    

    By the way, the following example does not work neither:

    - uses: dloez/tag-track@github-action-research
      with:
        compile: true
        use-cache: true
        create-tag: true
        action-ref: ${{ github.action_ref }}
    

    In the above example the input named action-ref will be empty.