question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create a new env var for the branch name

See original GitHub issue

Describe the enhancement We already have access to the branch/PR/tag ref name which is good but having an easy way to get the actual branch name is a really important feature.

Gitlab has two very good variables for that: (by the way, they very handy for me)

  • CI_COMMIT_REF_NAME for the branch/tag name
  • CI_COMMIT_REF_SLUG a slug for the branch/tag name

Ref: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html#variables-reference

One of my use cases is to tag a Docker image based on the branch name.

Additional information Here is a list of questions on the web about it and how this can lead to different implementations prone to error:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jwarbycommented, Sep 23, 2021

@tony been a while since I looked at this but I ended up with something along these lines:

jobs:
      define-vars:
          name: Define variables
          runs-on: ubuntu-latest
          outputs:
              docker_tag: ${{ steps.create-docker-tag.outputs.docker_tag }}
          steps:
          - uses: actions/checkout@v2
          - id: create-docker-tag
            run: echo "::set-output name=docker_tag::$(git rev-parse --abbrev-ref ${{ github.head_ref || github.ref || 'HEAD' }} | sed 's/[^a-z0-9_-]/__/gi')"

    build:
        # ..snip...
        steps:
           # Build app steps here
           - name: Push to GitHub Packages
            uses: docker/build-push-action@v1.1.0
            with:
                username: ${{ github.actor }}
                password: ${{ secrets.CR_PAT }}
                registry: ghcr.io
                repository: <repository-name>/<app-name>
                path: <path/to/app>
                tags: ${{ needs.define-vars.outputs.docker_tag }}

Hope that helps!

0reactions
nikola-jokiccommented, Feb 8, 2022

Hi everyone!

While reading comments, it seems like it has been resolved. I am closing this issue now but feel free to re-open it or file another issue if this matter is not resolved 😊.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Choose an environment variable based on the branch ...
Based on the current branch name, I have to pick the correct environment variable, and store its value (mail ID) to another environment...
Read more >
Dynamically set environment var based on branch name
I need some way to set an ENV var that can be used in my config.yml file to represent either “staging” or “production”...
Read more >
Environment variables - AWS Amplify Hosting
Amplify environment variablesSet environment variablesAccess environment variables at build ... The branch Amazon Resource Name (ARN) of the current build.
Read more >
Introduction to environment variables
Use environment variables to set up various configuration options, ... run: # print the name of the branch we're on name: "What branch...
Read more >
Environment Variables
A common way to customize the build process is to define environment variables, which can be accessed from any stage in your build...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found