How to get current branch within github actions

≯℡__Kan透↙ 提交于 2019-12-29 05:21:55

问题


I'm building docker images with Github Actions and want to tags images with branch name, I found only GITHUB_REF variable, but it results in refs/heads/feature-branch-1 and I need only feature-branch-1.


回答1:


I added a separate step for extracting branch name from $GITHUB_REF and set it to the step output

- name: Extract branch name
      shell: bash
      run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
      id: extract_branch

after that, I can use it in the next steps with

- name: Push to ECR
      id: ecr
      uses: jwalton/gh-ecr-push@master
      with:
        access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        region: us-west-2
        image: eng:${{ steps.extract_branch.outputs.branch }}



回答2:


I believe GITHUB_REF is the only environment variable that includes the branch name.

You can extract just the branch name from the rest of that string like this:

${GITHUB_REF##*/}

Example:

$ GITHUB_REF=refs/heads/feature-branch-1
$ echo ${GITHUB_REF##*/}
feature-branch-1

Update: Added a complete workflow example.

Workflow

name: CI
on: push
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Git checkout
        uses: actions/checkout@v1
      - name: Branch name
        run: echo running on branch ${GITHUB_REF##*/}
      - name: Build
        run: docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .

Source: https://github.com/tedmiston/x/blob/master/.github/workflows/workflow.yml

Sample output - master branch

Run docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .
  docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .
  shell: /bin/bash -e {0}
Sending build context to Docker daemon  146.9kB

Step 1/1 : FROM alpine
latest: Pulling from library/alpine
9d48c3bd43c5: Pulling fs layer
9d48c3bd43c5: Verifying Checksum
9d48c3bd43c5: Download complete
9d48c3bd43c5: Pull complete
Digest: sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb
Status: Downloaded newer image for alpine:latest
 ---> 961769676411
Successfully built 961769676411
Successfully tagged tedmiston/tag-example:master

Log: https://github.com/tedmiston/x/commit/cdcc58a908e41d3d90c39ab3bf6fef1ad2c4238a/checks#step:4:16

Sample output - non-master branch

Run docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .
  docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .
  shell: /bin/bash -e {0}
Sending build context to Docker daemon  144.9kB

Step 1/1 : FROM alpine
latest: Pulling from library/alpine
9d48c3bd43c5: Pulling fs layer
9d48c3bd43c5: Verifying Checksum
9d48c3bd43c5: Download complete
9d48c3bd43c5: Pull complete
Digest: sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb
Status: Downloaded newer image for alpine:latest
 ---> 961769676411
Successfully built 961769676411
Successfully tagged tedmiston/tag-example:branch-name-test

Log: https://github.com/tedmiston/x/commit/4e8d31259f861aaa2c30375756fc081c3659bddf/checks#step:4:16


See this answer for more on parameter expansion syntax.

For reference the page Virtual environments for GitHub Actions lists all of the environment variables available in the execution environment.




回答3:


I just made a simple test within GitHub Actions using a bash script:

#!/bin/bash

echo Reserved for REPO_NAME=${GITHUB_REPOSITORY##*/}
echo GITHUB_REF=${GITHUB_REF}
echo EXTRACT_GITHUB_REF=${GITHUB_REF##*/}
echo EXTRACT_GITHUB_REF_HEADS=$(echo ${GITHUB_REF#refs/heads/})

cd $REPO_NAME
git checkout ${GITHUB_REF##*/}
git checkout $(echo ${GITHUB_REF#refs/heads/})

Here is screenshot of the output:

So both ${GITHUB_REF##*/} and $(echo ${GITHUB_REF#refs/heads/}) are correct




回答4:


You could use https://github.com/rlespinasse/github-slug-action

- uses: rlespinasse/github-slug-action@master
- name: Print slug variables
  run: |
    echo ${{ env.GITHUB_REF_SLUG }}
    echo ${{ env.GITHUB_HEAD_REF_SLUG }}
    echo ${{ env.GITHUB_BASE_REF_SLUG }}

It will extract github_ref from environments variables.




回答5:


to set it as an en variable, I use this syntax:

- name: Extract branch name
  shell: bash
  run: echo "::set-env name=BRANCH_NAME::$(echo ${GITHUB_REF#refs/heads/} | sed 's/\//_/g')"
- name: Test
  run: echo "${BRANCH_NAME}"

I found this syntax here: Github actions - starter worflows#How to define env variable? #68

Rmq: the sed 's/\//_/g' is to replace \ by _ in the branch name



来源:https://stackoverflow.com/questions/58033366/how-to-get-current-branch-within-github-actions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!