Repository Name as a GitHub Action environment variable?

Viewed 12390

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.

4 Answers

Try github.event.repository.name

- run: echo "REPO_NAME=${{ github.event.repository.name }}" >> $GITHUB_ENV

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.

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

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