43

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.

CalculatingKraken
  • 651
  • 1
  • 9
  • 20

5 Answers5

49

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.

Samira
  • 7,795
  • 3
  • 25
  • 25
  • 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. – CalculatingKraken Jul 09 '20 at 13:21
  • 7
    `github.event.repository.name` worked for me. – msdundar Jan 04 '21 at 04: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! :) – davidrmcharles Jun 29 '21 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 Aug 18 '21 at 11:30
  • Can't upvote this enough! I would've saved so much time if I had known about `toJson(github)`... – tarikki Nov 18 '21 at 13:05
  • Thank you for helping on this. As the GH Actions grows and becomes more mature, this answer is no more actual. See https://stackoverflow.com/a/75513916/10148424 – D. Naumov Feb 20 '23 at 20:38
  • `github.event.repository.name` is [now available when the workflow is triggered on schedule](https://github.blog/changelog/2022-09-27-github-actions-additional-information-available-in-github-event-payload-for-scheduled-workflow-runs/). – Shawn Jul 06 '23 at 22:36
23

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 }}
Austin Gil
  • 601
  • 4
  • 6
14

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
Ma Guowei
  • 651
  • 6
  • 5
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 }}"
wynxel
  • 31
  • 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](/help/how-to-answer). – Community Apr 09 '22 at 00:06
  • 1
    This answer has the advantage of working across all workflow events. – JoshuaCWebDeveloper Nov 28 '22 at 18:09
0

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/VuePagesAction@1.0.1
      with:
        token: ${{ secrets.GITHUB_TOKEN }} # Leave this line unchanged
        username: ${{ github.repository_owner }}

        # Using of computed variable example
        reponame: ${{ steps.repo-basename.outputs.value }}
D. Naumov
  • 140
  • 2
  • 7