Is there a way to get the project folder name only on a Github action?

Viewed 18368

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

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

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}
Related