How to get current branch within github actions

前端 未结 13 2067
-上瘾入骨i
-上瘾入骨i 2020-11-29 20:11

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/f

相关标签:
13条回答
  • 2020-11-29 21:01

    How to get the current branch within Github Actions?

    Assuming ${{ github.ref }} is something like refs/heads/mybranch, you can extract the branch name using the following method:

    steps:
      - name: Prints the current branch name
        run: echo "${GITHUB_BRANCH##*/}"
        env:
          GITHUB_BRANCH: ${{ github.ref }}
    

    If your branch includes slashes (such as feature/foo), use the following syntax:

    steps:
      - name: Prints the current branch name
        run: echo "${GITHUB_REF#refs/heads/}"
    

    Credits: @rmunn comment

    Or use the method from the accepted answer, here is much shorter version (lint friendly):

    steps:
      - name: Get the current branch name
        shell: bash
        run: echo "::set-output name=branch::${GITHUB_REF#refs/heads/}"
        id: myref
    

    Then refer in other steps as ${{ steps.myref.outputs.branch }}.

    Notes:

    • Above methods works only for Unix-based images (Linux and macOS).
    • For docs, read: Context and expression syntax for GitHub Actions.
    0 讨论(0)
  • 2020-11-29 21:02

    Be aware that if you are executing your GitHub action on pull request trigger, then GITHUB_REF variable will contain something like refs/pull/421/merge so if you will try to git push to that name it will most likely fail.

    What you can use though are references on the GitHub context in your YAML. Something like: ${{ github.head_ref }}

    https://help.github.com/en/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context

    0 讨论(0)
  • 2020-11-29 21:03

    Heres a complete workflow that works for both push and pull_request events

    name: whichBranch
    on: [pull_request, push]
    
    jobs:
      which_branch:
        runs-on: ubuntu-latest
        steps:
          # extract branch name
          - name: Extract branch name
            if: github.event_name != 'pull_request'
            shell: bash
            run: echo "::set-env name=BRANCH_NAME::$(echo ${GITHUB_REF#refs/heads/})"
            id: extract_branch
    
          # extract branch name on pull request
          - name: Print branch name
            if: github.event_name == 'pull_request'
            run: echo "::set-env name=BRANCH_NAME::$(echo ${GITHUB_HEAD_REF})"
    
          # print branch name
          - name: Get branch name
            run: echo 'The branch name is' $BRANCH_NAME
    
    0 讨论(0)
  • 2020-11-29 21:04

    Using setenv is now deprecated. It is advised to use environment files. Building on @youjin's answer, while still allowing feature/ branches (replacing all occurences of / with -), I am now using this:

    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - name: Get branch name (merge)
            if: github.event_name != 'pull_request'
            shell: bash
            run: echo "BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/} | tr / -)" >> $GITHUB_ENV
    
          - name: Get branch name (pull request)
            if: github.event_name == 'pull_request'
            shell: bash
            run: echo "BRANCH_NAME=$(echo ${GITHUB_HEAD_REF} | tr / -)" >> $GITHUB_ENV
    
          - name: Debug
            run: echo ${{ env.BRANCH_NAME }}
    
    0 讨论(0)
  • 2020-11-29 21:05

    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

    0 讨论(0)
  • 2020-11-29 21:07

    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.

    0 讨论(0)
提交回复
热议问题