mirror of
https://github.com/rlespinasse/github-slug-action.git
synced 2026-06-13 01:51:09 +00:00
feat(slug): keep period in slug variable
BREAKING CHANGE: The previous slug function is rename slug_url to be able to still use itin the subdomain of an url. Co-authored-by: Marc Schiller <m4rc.schiller@gmail.com>
This commit is contained in:
3
.github/actions/bats/Dockerfile
vendored
3
.github/actions/bats/Dockerfile
vendored
@@ -1,3 +0,0 @@
|
|||||||
FROM bats/bats:latest
|
|
||||||
|
|
||||||
CMD ["./tests"]
|
|
||||||
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@@ -5,7 +5,9 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
- uses: ./.github/actions/bats
|
- uses: docker://ffurrer/bats:latest
|
||||||
|
with:
|
||||||
|
args: "--recursive ."
|
||||||
- uses: cycjimmy/semantic-release-action@v2
|
- uses: cycjimmy/semantic-release-action@v2
|
||||||
with:
|
with:
|
||||||
branches: |
|
branches: |
|
||||||
|
|||||||
77
README.md
77
README.md
@@ -6,48 +6,77 @@ This action slug and expose some github variables.
|
|||||||
|
|
||||||
`Slug` a variable will
|
`Slug` a variable will
|
||||||
|
|
||||||
- put the variable content in lower case,
|
- put the variable content in lower case
|
||||||
- replace any character by `-` except `0-9` and `a-z`,
|
- replace any character by `-` except `0-9`, `a-z`, and `.`
|
||||||
- remove leading and trailing `-` character,
|
- remove leading and trailing `-` character
|
||||||
- limit the string size to 63 characters.
|
- limit the string size to 63 characters
|
||||||
|
|
||||||
Others `Slug`-ish commands are available:
|
Others `Slug`-ish commands are available:
|
||||||
|
|
||||||
- `Short SHA` a variable will limit the string size to 8 characters.
|
- `Slug URL` a variable will be like the `slug` variable but the `.` character will also be replaced by `-`
|
||||||
|
- `Short SHA` a variable will limit the string size to 8 characters
|
||||||
|
|
||||||
## Exposed environment variables
|
## Exposed environment variables
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
- uses: rlespinasse/github-slug-action@v2.x
|
- name: Inject slug/short variables
|
||||||
- name: Print slug variables
|
uses: rlespinasse/github-slug-action@v2.x
|
||||||
|
|
||||||
|
- name: Print slug/short variables
|
||||||
run: |
|
run: |
|
||||||
echo ${{ env.GITHUB_REF_SLUG }}
|
echo "Slug reference variables"
|
||||||
echo ${{ env.GITHUB_HEAD_REF_SLUG }}
|
echo " - ${{ env.GITHUB_REF_SLUG }}"
|
||||||
echo ${{ env.GITHUB_BASE_REF_SLUG }}
|
echo " - ${{ env.GITHUB_HEAD_REF_SLUG }}"
|
||||||
echo ${{ env.GITHUB_SHA_SHORT }}
|
echo " - ${{ env.GITHUB_BASE_REF_SLUG }}"
|
||||||
|
echo "Slug URL reference variables"
|
||||||
|
echo " - ${{ env.GITHUB_REF_SLUG_URL }}"
|
||||||
|
echo " - ${{ env.GITHUB_HEAD_REF_SLUG_URL }}"
|
||||||
|
echo " - ${{ env.GITHUB_BASE_REF_SLUG_URL }}"
|
||||||
|
echo "Short SHA variables"
|
||||||
|
echo " - ${{ env.GITHUB_SHA_SHORT }}"
|
||||||
```
|
```
|
||||||
|
|
||||||
Read [default environment variables](https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables) page for more information.
|
Read [default environment variables](https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables) page for more information.
|
||||||
|
|
||||||
### GITHUB_REF_SLUG
|
### GITHUB_REF_SLUG(_URL)
|
||||||
|
|
||||||
Slug the environment variable **GITHUB_REF**
|
Slug the environment variable **GITHUB_REF**
|
||||||
|
|
||||||
The branch or tag ref that triggered the workflow.
|
The branch or tag ref that triggered the workflow.
|
||||||
_If neither a branch or tag is available for the event type, the variable will not exist._
|
_If neither a branch or tag is available for the event type, the variable will not exist._
|
||||||
|
|
||||||
| Environment variable (GITHUB_REF) | Slug variable (GITHUB_REF_SLUG) |
|
| GITHUB_REF | GITHUB_REF_SLUG | GITHUB_REF_SLUG_URL |
|
||||||
|-----------------------------------|---------------------------------|
|
|--------------------------------|---------------------|---------------------|
|
||||||
| refs/heads/master | master |
|
| refs/heads/master | master | master |
|
||||||
| refs/heads/feat/new_feature | feat-new-feature |
|
| refs/heads/feat/new_feature | feat-new-feature | feat-new-feature |
|
||||||
| refs/tags/v1.0.0 | v1-0-0 |
|
| refs/tags/v1.0.0 | v1.0.0 | v1-0-0 |
|
||||||
| refs/tags/product@1.0.0-rc.2 | product-1-0-0-rc-2 |
|
| refs/tags/product@1.0.0-rc.2 | product-1.0.0-rc.2 | product-1-0-0-rc-2 |
|
||||||
| refs/heads/New_Awesome_Product | new-awesome-product |
|
| refs/heads/New_Awesome_Product | new-awesome-product | new-awesome-product |
|
||||||
|
|
||||||
_Additional variables (only set for forked repositories) :_
|
_Additional variables (only set for forked repositories) :_
|
||||||
|
|
||||||
- `GITHUB_HEAD_REF_SLUG` : The branch of the head repository **GITHUB_HEAD_REF**,
|
- `GITHUB_HEAD_REF_SLUG`/`GITHUB_HEAD_REF_SLUG_URL`, The branch of the head repository **GITHUB_HEAD_REF**
|
||||||
- `GITHUB_BASE_REF_SLUG` : The branch of the base repository **GITHUB_BASE_REF**.
|
- `GITHUB_BASE_REF_SLUG`/`GITHUB_BASE_REF_SLUG_URL`, The branch of the base repository **GITHUB_BASE_REF**
|
||||||
|
|
||||||
|
#### Use in an URL
|
||||||
|
|
||||||
|
In an URL, use `GITHUB_REF_SLUG_URL` instead of **GITHUB_REF_SLUG** as subdomain to be compliant.
|
||||||
|
|
||||||
|
> **NOTE :**
|
||||||
|
> GITHUB_REF_SLUG can be used in an URL only as part of the _resource path_.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
- name: Inject slug/short variables
|
||||||
|
uses: rlespinasse/github-slug-action@v2.x
|
||||||
|
|
||||||
|
- name: Deploy dummy application using slug in the 'subdomain' part
|
||||||
|
run: |
|
||||||
|
./deploy-application.sh --url "https://${{ env.GITHUB_REF_SLUG_URL }}.staging.app.mycompagny.com"
|
||||||
|
|
||||||
|
- name: Deploy dummy application using slug in the 'resource path' part
|
||||||
|
run: |
|
||||||
|
./deploy-application.sh --url "https://staging.app.mycompagny.com/${{ env.GITHUB_REF_SLUG }}"
|
||||||
|
```
|
||||||
|
|
||||||
### GITHUB_SHA_SHORT
|
### GITHUB_SHA_SHORT
|
||||||
|
|
||||||
@@ -55,6 +84,6 @@ Short the environment variable **GITHUB_SHA**
|
|||||||
|
|
||||||
The commit SHA that triggered the workflow
|
The commit SHA that triggered the workflow
|
||||||
|
|
||||||
| Environment variable (GITHUB_SHA) | Short variable (GITHUB_SHA_SHORT) |
|
| GITHUB_SHA | GITHUB_SHA_SHORT |
|
||||||
|------------------------------------------|-----------------------------------|
|
|------------------------------------------|------------------|
|
||||||
| ffac537e6cbbf934b08745a378932722df287a53 | ffac537e |
|
| ffac537e6cbbf934b08745a378932722df287a53 | ffac537e |
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
#!/bin/sh -l
|
#!/bin/sh -l
|
||||||
|
|
||||||
slug_ref() {
|
slug_ref() {
|
||||||
|
echo "$1" |
|
||||||
|
tr "[:upper:]" "[:lower:]" |
|
||||||
|
sed -r 's#refs/[^\/]*/##;s/[~\^]+//g;s/[^a-zA-Z0-9.]+/-/g;s/^-+\|-+$//g;s/^-*//;s/-*$//' |
|
||||||
|
cut -c1-63
|
||||||
|
}
|
||||||
|
|
||||||
|
slug_url_ref() {
|
||||||
echo "$1" |
|
echo "$1" |
|
||||||
tr "[:upper:]" "[:lower:]" |
|
tr "[:upper:]" "[:lower:]" |
|
||||||
sed -r 's#refs/[^\/]*/##;s/[~\^]+//g;s/[^a-zA-Z0-9]+/-/g;s/^-+\|-+$//g;s/^-*//;s/-*$//' |
|
sed -r 's#refs/[^\/]*/##;s/[~\^]+//g;s/[^a-zA-Z0-9]+/-/g;s/^-+\|-+$//g;s/^-*//;s/-*$//' |
|
||||||
@@ -15,4 +22,9 @@ short_sha() {
|
|||||||
echo ::set-env name=GITHUB_REF_SLUG::"$(slug_ref "$GITHUB_REF")"
|
echo ::set-env name=GITHUB_REF_SLUG::"$(slug_ref "$GITHUB_REF")"
|
||||||
echo ::set-env name=GITHUB_HEAD_REF_SLUG::"$(slug_ref "$GITHUB_HEAD_REF")"
|
echo ::set-env name=GITHUB_HEAD_REF_SLUG::"$(slug_ref "$GITHUB_HEAD_REF")"
|
||||||
echo ::set-env name=GITHUB_BASE_REF_SLUG::"$(slug_ref "$GITHUB_BASE_REF")"
|
echo ::set-env name=GITHUB_BASE_REF_SLUG::"$(slug_ref "$GITHUB_BASE_REF")"
|
||||||
|
|
||||||
|
echo ::set-env name=GITHUB_REF_SLUG_URL::"$(slug_url_ref "$GITHUB_REF")"
|
||||||
|
echo ::set-env name=GITHUB_HEAD_REF_SLUG_URL::"$(slug_url_ref "$GITHUB_HEAD_REF")"
|
||||||
|
echo ::set-env name=GITHUB_BASE_REF_SLUG_URL::"$(slug_url_ref "$GITHUB_BASE_REF")"
|
||||||
|
|
||||||
echo ::set-env name=GITHUB_SHA_SHORT::"$(short_sha "$GITHUB_SHA")"
|
echo ::set-env name=GITHUB_SHA_SHORT::"$(short_sha "$GITHUB_SHA")"
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "docker run -w /workdir -v $(pwd):/workdir bats/bats:latest ./tests",
|
"test": "docker run -w /workdir -v $(pwd):/workdir ffurrer/bats:latest --recursive .",
|
||||||
"release": "semantic-release"
|
"release": "semantic-release"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env bats
|
#!/usr/bin/env bats
|
||||||
|
|
||||||
@test "Short long hash" {
|
@test "short_sha: long hash" {
|
||||||
test_short_sha \
|
test_short_sha \
|
||||||
"a35a1a486a260cfd99c5b6f8c6034a2929ba9b3f" \
|
"a35a1a486a260cfd99c5b6f8c6034a2929ba9b3f" \
|
||||||
"a35a1a48"
|
"a35a1a48"
|
||||||
|
|||||||
@@ -1,43 +1,43 @@
|
|||||||
#!/usr/bin/env bats
|
#!/usr/bin/env bats
|
||||||
|
|
||||||
@test "Slug master branch" {
|
@test "slug_ref:: master branch" {
|
||||||
test_sluf_ref \
|
test_slug_ref \
|
||||||
"refs/heads/master" \
|
"refs/heads/master" \
|
||||||
"master"
|
"master"
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "Slug a feature branch" {
|
@test "slug_ref: a feature branch" {
|
||||||
test_sluf_ref \
|
test_slug_ref \
|
||||||
"refs/heads/feat/new_feature" \
|
"refs/heads/feat/new_feature" \
|
||||||
"feat-new-feature"
|
"feat-new-feature"
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "Slug a fix branch" {
|
@test "slug_ref: a fix branch" {
|
||||||
test_sluf_ref \
|
test_slug_ref \
|
||||||
"refs/heads/fix/issue_number" \
|
"refs/heads/fix/issue_number" \
|
||||||
"fix-issue-number"
|
"fix-issue-number"
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "Slug a simple tag" {
|
@test "slug_ref: a simple tag" {
|
||||||
test_sluf_ref \
|
test_slug_ref \
|
||||||
"refs/tags/v1.0.0" \
|
"refs/tags/v1.0.0" \
|
||||||
"v1-0-0"
|
"v1.0.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "Slug a complex tag" {
|
@test "slug_ref: a complex tag" {
|
||||||
test_sluf_ref \
|
test_slug_ref \
|
||||||
"refs/tags/product@1.0.0-rc.2" \
|
"refs/tags/product@1.0.0-rc.2" \
|
||||||
"product-1-0-0-rc-2"
|
"product-1.0.0-rc.2"
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "Slug a reference with upper case letters" {
|
@test "slug_ref: a reference with upper case letters" {
|
||||||
test_sluf_ref \
|
test_slug_ref \
|
||||||
"refs/heads/New_Awesome_Product" \
|
"refs/heads/New_Awesome_Product" \
|
||||||
"new-awesome-product"
|
"new-awesome-product"
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "Slug a very long name" {
|
@test "slug_ref: a very long name" {
|
||||||
test_sluf_ref \
|
test_slug_ref \
|
||||||
"refs/heads/an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters" \
|
"refs/heads/an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters" \
|
||||||
"an-awesome-feature-very-very-very-very-very-very-very-long-more"
|
"an-awesome-feature-very-very-very-very-very-very-very-long-more"
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@
|
|||||||
# Load sluf_ref function
|
# Load sluf_ref function
|
||||||
source entrypoint.sh > /dev/null 2>&1
|
source entrypoint.sh > /dev/null 2>&1
|
||||||
|
|
||||||
test_sluf_ref() {
|
test_slug_ref() {
|
||||||
given="${1}"
|
given="${1}"
|
||||||
expected="${2}"
|
expected="${2}"
|
||||||
|
|
||||||
|
|||||||
55
tests/slug_url_ref.bats
Normal file
55
tests/slug_url_ref.bats
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#!/usr/bin/env bats
|
||||||
|
|
||||||
|
@test "slug_url_ref: master branch" {
|
||||||
|
test_slug_url_ref \
|
||||||
|
"refs/heads/master" \
|
||||||
|
"master"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "slug_url_ref: a feature branch" {
|
||||||
|
test_slug_url_ref \
|
||||||
|
"refs/heads/feat/new_feature" \
|
||||||
|
"feat-new-feature"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "slug_url_ref: a fix branch" {
|
||||||
|
test_slug_url_ref \
|
||||||
|
"refs/heads/fix/issue_number" \
|
||||||
|
"fix-issue-number"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "slug_url_ref: a simple tag" {
|
||||||
|
test_slug_url_ref \
|
||||||
|
"refs/tags/v1.0.0" \
|
||||||
|
"v1-0-0"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "slug_url_ref: a complex tag" {
|
||||||
|
test_slug_url_ref \
|
||||||
|
"refs/tags/product@1.0.0-rc.2" \
|
||||||
|
"product-1-0-0-rc-2"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "slug_url_ref: a reference with upper case letters" {
|
||||||
|
test_slug_url_ref \
|
||||||
|
"refs/heads/New_Awesome_Product" \
|
||||||
|
"new-awesome-product"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "slug_url_ref: a very long name" {
|
||||||
|
test_slug_url_ref \
|
||||||
|
"refs/heads/an-awesome-Feature-Very-Very-Very-Very-Very-Very-Very-Long-moreThan63Characters" \
|
||||||
|
"an-awesome-feature-very-very-very-very-very-very-very-long-more"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Load sluf_ref function
|
||||||
|
source entrypoint.sh > /dev/null 2>&1
|
||||||
|
|
||||||
|
test_slug_url_ref() {
|
||||||
|
given="${1}"
|
||||||
|
expected="${2}"
|
||||||
|
|
||||||
|
actual="$(slug_url_ref \"${given}\")"
|
||||||
|
echo "expected : [${expected}], actual : [${actual}]"
|
||||||
|
[ "${actual}" == "${expected}" ]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user