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
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.
-
3Thank 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
-
13Might 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.– grenadeCommented Aug 18, 2021 at 11:30
-
1
github.event.repository.name
is now available when the workflow is triggered on schedule.– ShawnCommented Jul 6, 2023 at 22:36
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 }}
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
-
This seems to be the most reliable way as
github.event...
doesn't work for scheduled triggers etc. This will always work Commented Jun 2, 2022 at 7:03 -
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 }}"
-
1As 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 BotCommented Apr 9, 2022 at 0:06
-
1This answer has the advantage of working across all workflow events. Commented Nov 28, 2022 at 18:09
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 }}
-
For me,
${{ github.repository }}
gave memyusername/my-repo-name
which was perfect! Commented Dec 20, 2023 at 17:04