This tutorial walks you through adding the GitHub Slug Action to a workflow and using slug variables for the first time.
- A GitHub repository with GitHub Actions enabled
Create or edit a workflow file (e.g., .github/workflows/deploy.yml) and add the action after checking out your code:
name: Deploy
on: push
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Inject enhanced GitHub environment variables
uses: rlespinasse/github-slug-action@v5Tip
The actions/checkout step is recommended so that Git can determine the optimal short SHA length for your repository. It is not strictly required for slug variables.
After the action runs, all slug variables are available as environment variables. Add a step to use them:
- name: Print slug variables
run: |
echo "Repository slug: $GITHUB_REPOSITORY_SLUG"
echo "Branch slug: $GITHUB_REF_SLUG"
echo "Short SHA: $GITHUB_SHA_SHORT"
shell: bashFor a branch named feat/new_feature on repository octocat/Hello-World, this outputs:
Repository slug: octocat-hello-world
Branch slug: feat-new-feature
Short SHA: ffac537e
A common use case is naming deployment previews with a URL-safe branch identifier:
- name: Deploy preview
run: |
echo "Deploying to https://${GITHUB_REF_SLUG_URL}.preview.example.com"
shell: bashThe SLUG_URL variant replaces dots and underscores too, making it safe for subdomains.
Here is the full workflow file:
name: Deploy
on: push
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Inject enhanced GitHub environment variables
uses: rlespinasse/github-slug-action@v5
- name: Deploy preview
run: |
echo "Deploying to https://${GITHUB_REF_SLUG_URL}.preview.example.com"
echo "Commit: ${GITHUB_SHA_SHORT}"
shell: bash- SLUG variables convert values to lowercase and replace special characters with
- - SLUG_URL variables also replace
.and_, making values safe for URLs and subdomains - SHORT variables shorten SHA commit hashes
- All variables are exposed as environment variables for use in subsequent steps
- How-to guides for specific configuration tasks (prefixes, custom lengths, URL usage)
- Variable reference for the complete list of available variables
- Slug transformation rules to understand how transformations work