Fetching Current Repository Name in a GitHub Actions Workflow

Fetching Current Repository Name in a GitHub Actions Workflow

GitHub Actions allows us to automate software workflows directly from GitHub repositories. Frequently, during the execution of these workflows, we need to refer to the current repository’s name. This could be useful for tagging Docker images, creating artifacts, or even generating notifications. In this blog post, we’ll guide you on how to fetch the current repository name in a GitHub Actions workflow.

GitHub Actions and the github Context

GitHub Actions provides a github context which can be used to access metadata about the workflow run, including the repository name. This context is a read-only object generated for each run, and it includes useful information like the event that triggered the workflow run and the workflow run itself.

Extracting Repository Name

In your workflow, you can access the github context and fetch the repository name using the following syntax:

In this example, the github.repository expression fetches the current repository’s name. This value is in the form owner/repo.

If you need only the repository’s name without the owner, you can use a small shell script to parse it. Here’s an example of how to do this using bash:

In this example, the awk command splits the full repository name owner/repo into two parts based on the slash (/) separator and prints the second part ({print $2}), which is the repository name.

Conclusion

Being able to fetch the current repository’s name within a GitHub Actions workflow is a handy tool. Whether you are managing Docker images, deploying to different environments, or simply debugging your workflows, it’s a trick that can simplify your workflow.

Remember, the github context offers a lot more than just the repository name. It provides access to various other run-level details, so it’s worth exploring to see how else it might aid your workflows.