IMPROVED:
This technique doesn't need to bring in anything from the actions marketplace.
Add this to steps: in an action:
- name: Split branch name
env:
BRANCH: ${{ github.ref_name }}
id: split
run: echo "::set-output name=fragment::${BRANCH##*/}"
It captures the output of a shell command. The only necessary actions are setting github.ref_name to an environmental variable and using parameter expansion to get the part of the branch name you want.
## greedily removes the subsequent pattern off the front
*/ matches anything followed by a /
- So,
##*/ removes everything up to and including the final forward slash.
Because id is split, and name=fragment, you then reference your split string with steps.split.outputs.fragment. For example,
# Another step
- name: Test variable
run: |
echo ${{ steps.split.outputs.fragment }}
Other parameter expansion features would be useful here, like #, %, and %% in addition to ##.
220829: Final update
Originally I spawned another process with $(echo ${BRANCH##*/}) but that is unnecessary. The variable can be directly referenced.
ORIGINAL:
I'm doing something essentially the same and using an action was the easiest temporary workaround, however I also would have preferred something built-in like a string-splitting expression. For now here is what worked.
- uses: jungwinter/split@master
id: branch
with:
msg: ${{ github.ref_name }}
separator: "/"
maxsplit: -1
From its repo:
Outputs
_0, _1, ..., _n: Each result of a split
- According to metadata syntax of outputs, it has
_ prefix
- Currently, support only 100 splits
length: Length of the splits
So, then I can reference each portion of the string with steps.branch.outputs._n, with branch being the id and n being the split index.
I was looking for the first instead of the last value in the branch name, so I could set maxsplit to 1 and just take _0. In your case you might need to do some magic with checking length and choosing the index from there.