78

I can get repoowner/repo-name with ${{ github.repository }} but I like to get just the repo-name.

While I am at it.

GitLab has a straight forward variable for this.

How do I get reference access env variables from the .yaml not inside scripts? Does ${{ $ENV_VAR }} work? Probably not.

Is there a way to remove or replace the slashes directly in the yaml?

  build:
    needs: test
    name: Build release Zip
    runs-on: ubuntu-latest
    env:
      DEPLOY_REF: ${{ github.sha }}
      DEPLOY_REF_SHORT: ${{ github.sha }}
      DEPLOY_ZIPFILE: ${{ github.workspace }}/build/zip/${{ github.repository }}-${{ github.sha }}.zip
    steps:

2 Answers 2

118

github context also contains event data, from where you can easily read some more detailed info. In this case, ${{ github.event.repository.name }} will give you only repo-name part.

It might be a good idea to create testing workflow/job to print all context data until you're familiar with them, see Example printing context information to the log file.


As for replacing text, you can always dedicate one of first steps to prepare everything you need later with either ::set-env or ::set-output. You just need to print name/value in specific format, so you can use any shell you're familiar with to manipulate text while doing that:

# All steps after this one will have env variable `GHA_BRANCH` passed to them.
- run:   echo ::set-env name=GHA_BRANCH::$(echo $GITHUB_REF | awk -F / '{print $3}')
  shell: bash
8
  • Can I use - run on a workflow scope? b4 all jobs start? Probably not so I would have to redo it for each job? ::set-env is good to know. Commented Oct 20, 2019 at 17:15
  • @redanimalwar If N jobs needs same environment tuning, you can keep it all in script file. Just add - run: ./setup-env.sh (which will call echo ::set-env) as first step of each job - later, any env tuning done in .sh will affect whole workflow.
    – Samira
    Commented Oct 21, 2019 at 2:57
  • Gitlab has a simple $CI_COMMIT_SLUG for something like that. Not sure how they get that, maybe I could get something from git itself. What I like is the tag when its a tag build and the branch when its the branch and not a ugly commit hash that says nothing. Seems annoying to have to dig so deep into that object when I not even know if its always there. I think I am going with that github.repository but replacing the / with - because this will not play nice with filenames. I may actually only need it in one job for now. Commented Oct 21, 2019 at 13:33
  • 3
    ${{ github.event.repository.name }} did not work for me, at least not under env; it was the empty string/not defined. ${{ github.repository_owner }} worked though (but doesn't solve the problem, of course). Commented Jul 10, 2020 at 15:53
  • 1
    This will not work for actions scheduled via cron. The event payload is empty. Commented Jun 8, 2022 at 19:20
26

In addition to @Samira response, you could also use ${GITHUB_REPOSITORY#*/} to get the repository name, since it will cut all before the / prefix.

For e.g., at my workflow I use:

- run: echo ::set-env name=TF_VAR_instance_label::${GITHUB_REPOSITORY#*/}-${GITHUB_SHA:0:7}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.