Skip to content

About

πŸš€ GitHub Action to expose safe slugs, parsed version components, and additional build metadata inside your workflow

Topics

Resources

Stars

8 stars

Watchers

1 watching

Forks

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Latest commit

Β 

History

137 Commits

Folders and files

Repository files navigation

Logo

GitHub slug action

Release version Build Status License

GitHub Action to expose safe slugs, parsed version components, and additional build metadata inside your workflow.

Why

No built-in slug variables in GitHub Actions. Even GitLab CI exposes predefined slug variables out of the box (such as CI_COMMIT_REF_SLUG and CI_PROJECT_PATH_SLUG), whereas GitHub Actions has neither native slug variables nor a slug() expression function (community discussion #127106, and tons of similar questions on different platforms). Raw refs like feature/Fix_Something or v1.2.3+build.1 break Docker tags, preview URLs, DNS labels, release asset names, cloud resource identifiers and other stuff.

And no native version parsing. GitHub Actions does not split tags or branches into major, minor, patch, or normalized semver strings. Instead of maintaining ad-hoc bash/sed/awk scripts in every workflow, this action parses common version formats and doesn't require configuration to work.

How it works

  • "Slugification" - replaces ., _, /, (, ), #, +, and unsupported characters with -, transliterates symbols like $ (dollar) and % (percent), lowercases to a-z/0-9/-, and strips leading, trailing, and repeated -.
  • Ref detection (resolves branch vs tag).
  • Version extraction - strips an optional v/ver/version prefix (with optional ., _, space, or -), extracts major, minor, patch (0 by default) and a slugged tail (extra numeric segments such as 1.2.3.4 become part of the tail: 1.2.3-4) - as much close to semver as possible.

Usage

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: gacts/github-slug@v1
        id: slug # important to set an ID to reference outputs
        with:
          to-slug: Hello ${{ github.actor }}! How are you? # optional
      - run: |
          echo "Slug: ${{ steps.slug.outputs.slug }}"
          echo "Branch name: ${{ steps.slug.outputs.branch-name }}"
          echo "Version: ${{ steps.slug.outputs.version }}"
          echo "Version semantic: ${{ steps.slug.outputs.version-semantic }}"
          # and others

Tip

Use Dependabot to keep this action updated in your repository.

Outputs

Output Type When set Description Example (on branch fix/Foo_bar) Example (on tag v1.2-rc1_Lorem)
slug string to-slug input is provided Slugged value of to-slug hello-octocat-how-are-you hello-octocat-how-are-you
is-branch bool Always Whether the workflow was triggered on a branch true false
is-tag bool Always Whether the workflow was triggered on a tag false true
branch-name string Branch runs Current branch name fix/Foo_bar (unset)
branch-name-slug string Branch runs Slugged branch-name fix-foo-bar (unset)
tag-name string Tag runs Current tag name (unset) v1.2-rc1_Lorem
tag-name-slug string Tag runs Slugged tag-name (unset) v1-2-rc1-lorem
commit-hash string Always Full commit SHA hash ffac537...287a53 (40 chars) ffac537...287a53 (40 chars)
commit-hash-short string Always Short (7-char) commit SHA hash ffac537 ffac537
version string Always Cleaned and slugged version ([X[.Y[.Z]]][-tail], prefixless) fix-foo-bar 1.2-rc1-lorem
version-major number Always Major version number (0 by default) 0 1
version-minor number Always Minor version number (0 by default) 0 2
version-patch number Always Patch version number (0 by default) 0 0
version-semantic string Always Semantic version string (X.Y.Z[-tail], prefixless) 0.0.0-fix-foo-bar 1.2.0-rc1-lorem

Note

All step outputs are strings at runtime - "false" and "0" are non-empty strings and therefore truthy in if:. Always compare booleans explicitly (if: ${{ steps.slug.outputs.is-tag == 'true' }}) and wrap numbers in fromJSON(...) for numeric comparisons (fromJSON(steps.slug.outputs.version-major) > 0).

Examples

Branch build (Go binary version)

on:
  push: {branches: [master, main]}
  pull_request: {}

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-go@v7
        with: {go-version: '1.27'}
      - uses: gacts/github-slug@v1
        id: slug
      - run: go build -ldflags "-s -w -X main.version=${{ steps.slug.outputs.commit-hash-short }}" -o ./app ./cmd/app

Tag / release build (Docker image)

on:
  release:
    types: [published]

jobs:
  docker:
    runs-on: ubuntu-latest
    permissions: {contents: read, packages: write}
    steps:
      - uses: actions/checkout@v7
      - uses: gacts/github-slug@v1
        id: slug
      - uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/build-push-action@v7
        with:
          context: .
          push: true
          tags: |
            ghcr.io/${{ github.repository }}:${{ steps.slug.outputs.version-semantic }}
            ghcr.io/${{ github.repository }}:latest

Releasing

To release a new version:

  • Build the action distribution (npm run build).
  • Commit and push changes (including dist directory changes - this is important) to the master|main branch.
  • Publish the new release using the repo releases page (the git tag should follow the vX.Y.Z format).

Major and minor git tags (v1 and v1.2 if you publish a v1.2.Z release) will be updated automatically.

Support

Issues Pull Requests

If you find any errors in the action, please create an issue in this repository.

License

This is open-source software licensed under the MIT License.

About

πŸš€ GitHub Action to expose safe slugs, parsed version components, and additional build metadata inside your workflow

Topics

Resources

Stars

8 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages