'Repository Name as a GitHub Action environment variable?

How would you get the repository name (not the user or organization) as an environment variable in GitHub Actions? I found github.repository but that contains the owner as the first part like so: owner/repo.



Solution 1:[1]

Try github.event.repository.name

- run: echo ::set-env name=REPO_NAME::${{ github.event.repository.name }}

Documentation aside, i'd really recommend dumping contexts (maybe in some test repo) just to get familiar with them, as there is lot of data which might or might not be useful when writing non-trivial workflows.

- name: Dump github context
  run:   echo "$GITHUB_CONTEXT"
  shell: bash
  env:
   GITHUB_CONTEXT: ${{ toJson(github) }}

Be aware that parts of github context changes depends which event triggered workflow, so it might be a good idea to double check if data you want to use is available for all events used in workflow.

Solution 2:[2]

I think the syntax you're looking for is actually github.event.repository.name

@Samira's toJson(github) tip was super useful. It took me a send look to notice the repository property was indented a bit further in, under `event.

You can use the value directly, or assign it at the top level with:

env:
  REPO_NAME: ${{ github.event.repository.name }}

Solution 3:[3]

You can extract it from github.repository:

name: Print repo name

on:
    workflow_dispatch:

jobs:
    print-name:
        runs-on: ubuntu-latest
        steps:
            - name: get-name
              run: |
                  echo "REPO_NAME=$(basename ${{ github.repository }})" >> $GITHUB_ENV
            - name: print-name
              run: |
                  echo "${{ env.REPO_NAME }}"

Solution 4:[4]

example: https://github.com/maguowei/awesome-stars/blob/master/.github/workflows/schedules.yml#L21

- name: get epository name
  run: echo "REPOSITORY_NAME=${GITHUB_REPOSITORY#*/}" >> $GITHUB_ENV

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Austin Gil
Solution 3 wynxel
Solution 4 Ma Guowei