How to refactor deprecated set-env in github action

605 Views Asked by At

I have a github action that runs set-env, now as it is deprecated as mentioned here, how can refactor the following code?

 run: echo "::set-env name=BRANCH_NAME::$(echo ${GITHUB_HEAD_REF} | tr -cd '[:alnum:]\n' | cut -c -30 | tr '[:upper:]' '[:lower:]')"
1

There are 1 best solutions below

3
Marcin Kłopotek On BEST ANSWER

According to the documentation:

 run: echo "BRANCH_NAME=$(echo ${GITHUB_HEAD_REF} | tr -cd '[:alnum:]\n' | cut -c -30 | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV

Additionally, instead of messing up with tr and cut you can get the branch name in a more convenient way using the GITHUB_REF built-in variable. It will simplify the script to

 run: echo "BRANCH_NAME=${GITHUB_REF##*/}" >> $GITHUB_ENV