Dynamic naming of build artifacts based on commit reference

I’ve been staring at the docs for about half an hour, but honestly I’ve got no idea what I’m doing so I figure it’s worth asking for some more direct help.

My repo has a fairly simple action - When a commit is made to a branch that isn’t master, run the BigWigs packager and upload the artifacts.

The packager creates two zip files, SurroundFix-v3.6-7-g5e389b5.zip and SurroundFix-v3.6-7-g5e389b5-classic.zip, with the reference part of the file being the output of git describe --always. These are what I currently upload with the script.

The packager runs twice to create each zip, and it also creates a folder, simply called SurroundFix. What I would like to do is upload an artifact after the first run that contains this folder called SurroundFix-v3.6-7-g5e389b5, and an artifact after the second run that contains this folder called SurroundFix-v3.6-7-g5e389b5-classic, with the git reference obviously changing depending on the current commit that triggers the build.

That would leave me with two zip artifacts named as above, each containing a folder called SurroundFix.

How exactly would I go about doing that?

      - name: Upload Retail Artifact
        uses: actions/upload-artifact@v2
        with:
          name: (folder name + git reference)
          path: .release/*/

      - name: Upload Classic Artifact
        uses: actions/upload-artifact@v2
        with:
          name: (folder name + git reference + `-classic`)
          path: .release/*/

I know that’s basically what I want to do, but I have absolutely no idea how to do that.


Based on this page, I assume it’s something to do with ${{ github.head_ref }} or something similar, but I’ve got absolutely no idea how to find the name of the folder to append to the start, no idea how to get the git reference, and no idea how to combine the values into a complete name.

If anyone can give me any help with this, I’d hugely appreciate it.
Thank you!

I’d use the github.sha variable, unless you want to shorten it first. Combining it just means using ${{ github.sha }} in the pattern, like this:

      - name: Upload Retail Artifact
        uses: actions/upload-artifact@v2
        with:
          name: SurroundFix-${{ github.sha }}
          path: .release/*/

I’m not sure what you mean by finding the folder name, I thought that’s fixed? If you’re talking about the version number you can just store it in an environment variable and use the env context to access it, the same way as with the github context.

Oh, that’s fairly simple, I thought there would have to be something fancy to combine the string.

The folder name should always be the same, but I’d ideally like to try and make it dynamic as there are a couple of potential future sitautions where it might change.

    env:
      folder_name: (something that returns the name of the folder in .release/)

      - name: Upload Retail Artifact
        uses: actions/upload-artifact@v2
        with:
          name: ${{ folder_name }}-${{ github.sha }}
          path: .release/*/

Do I need to be doing something like this to get a variable with the name of the folder?
Honestly I’m totally at sea here, thank you for the answer!

Basically yes! Though I assume that the folder name is going to be a bit too dynamic to set it in the workflow or job, so it’s probably best to create it in a step and write it to the environment file so it’s available to the following steps:

      - name: Set folder name
        run: |
          echo "folder_name=$(some_command_to_find_the_folder)" >> $GITHUB_ENV
      - name: Upload Retail Artifact
        uses: actions/upload-artifact@v2
        with:
          name: ${{ env.folder_name }}-${{ github.sha }}
          path: .release/*/

The code in the run step is regular Bash. You can use other interpreters or call scripts, the important thing is that the folder_name=... assignment ends up in the file given by the GITHUB_ENV environment variable.

Awesome, I think I should be able to get it to work with this. I know for certain that there’s a way to get a folder name from bash.

Thanks!

EDIT:
As an edit for future reference, I managed to get this working absolutely perfectly.

      # add variables for the name of the folder being added as an artifact
      - name: Set Name Variables
        run: |
          echo "version=$(git describe)" >> $GITHUB_ENV
          cd .release
          echo "folder_name=$(ls -d */ | sed 's:/*$::')" >> $GITHUB_ENV

      # upload the artifact for retail
      - name: Upload Retail Artifact
        uses: actions/upload-artifact@v2
        with:
          name: ${{ env.folder_name }}-${{ env.version }}
          path: |
            .release/*/
            !.release/*.zip

I found a bit of bash that output the directory name and removed the trailing slash, then realised I could do the same thing to just put git describe into a variable too.
Now the artifact names are exactly as I wanted them, and everything is absolutely perfect.

Thanks again for the help!

1 Like