54

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.

5 Answers 5

70

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.

7
  • 3
    Thank you! For context, I needed this information for pull request events, I found the variable to be github.event.pull_request.base.repo.name. I am going to mark your post as the answer since its very good debugging advice and it lead me to the solution. Commented Jul 9, 2020 at 13:21
  • 7
    github.event.repository.name worked for me.
    – msdundar
    Commented Jan 4, 2021 at 4:28
  • 13
    Might be useful to mention that github.event.repository.name is unavailable when your workflow is triggered on schedule. Sorting through that right now! :) Commented Jun 29, 2021 at 21:25
  • github.repository.name is not valid and does not contain the repository name. the rest of this answer is informative but the first example is simply invalid.
    – grenade
    Commented Aug 18, 2021 at 11:30
  • 1
    github.event.repository.name is now available when the workflow is triggered on schedule.
    – Shawn
    Commented Jul 6, 2023 at 22:36
27

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

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
2
  • This seems to be the most reliable way as github.event... doesn't work for scheduled triggers etc. This will always work
    – Kristofer
    Commented Jun 2, 2022 at 7:03
  • This works regardless of the event Commented Sep 5, 2022 at 16:40
4

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 }}"
2
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Apr 9, 2022 at 0:06
  • 1
    This answer has the advantage of working across all workflow events. Commented Nov 28, 2022 at 18:09
4

Nowadays ${{ github.repository }} more than enough.

If you need just the basename excluding repo owner or and other computed value, see example below.


Please note!

If you want to calculate some value, consider using setting an output as it:

  • more safe;
  • more flexible when you'll switch to reusable actions, eg Composite actions.

Here's the complete and ready to go example for Vue app deploy to Github Pages:

on:
  push:

jobs:
  pages:
    runs-on: ubuntu-latest
    name: Pages Deploy
    steps:
    - uses: actions/checkout@v2

    # Output setting example
    - id: repo-basename
      run: |
        echo "value=`basename ${{ github.repository }}`" >> $GITHUB_OUTPUT
      shell: bash

    - uses: xRealNeon/[email protected]
      with:
        token: ${{ secrets.GITHUB_TOKEN }} # Leave this line unchanged
        username: ${{ github.repository_owner }}

        # Using of computed variable example
        reponame: ${{ steps.repo-basename.outputs.value }}
1
  • For me, ${{ github.repository }} gave me myusername/my-repo-name which was perfect!
    – takanuva15
    Commented Dec 20, 2023 at 17:04

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.