Skip to content

Commit

Permalink
feat(slug): keep period in slug variable
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rlespinasse and m4rcs committed Apr 26, 2020
1 parent 250b75d commit e95fe45
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 47 deletions.
3 changes: 0 additions & 3 deletions .github/actions/bats/Dockerfile

This file was deleted.

4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -5,7 +5,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: ./.github/actions/bats
- uses: docker://ffurrer/bats:latest
with:
args: "--recursive ."
- uses: cycjimmy/semantic-release-action@v2
with:
branches: |
Expand Down
77 changes: 53 additions & 24 deletions README.md
Expand Up @@ -6,55 +6,84 @@ This action slug and expose some github variables.

`Slug` a variable will

- put the variable content in lower case,
- replace any character by `-` except `0-9` and `a-z`,
- remove leading and trailing `-` character,
- limit the string size to 63 characters.
- put the variable content in lower case
- replace any character by `-` except `0-9`, `a-z`, and `.`
- remove leading and trailing `-` character
- limit the string size to 63 characters

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

```yaml
- uses: rlespinasse/github-slug-action@v2.x
- name: Print slug variables
- name: Inject slug/short variables
uses: rlespinasse/github-slug-action@v2.x

- name: Print slug/short variables
run: |
echo ${{ env.GITHUB_REF_SLUG }}
echo ${{ env.GITHUB_HEAD_REF_SLUG }}
echo ${{ env.GITHUB_BASE_REF_SLUG }}
echo ${{ env.GITHUB_SHA_SHORT }}
echo "Slug reference variables"
echo " - ${{ env.GITHUB_REF_SLUG }}"
echo " - ${{ env.GITHUB_HEAD_REF_SLUG }}"
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.

### GITHUB_REF_SLUG
### GITHUB_REF_SLUG(_URL)

Slug the environment variable **GITHUB_REF**

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._

| Environment variable (GITHUB_REF) | Slug variable (GITHUB_REF_SLUG) |
|-----------------------------------|---------------------------------|
| refs/heads/master | master |
| refs/heads/feat/new_feature | feat-new-feature |
| refs/tags/v1.0.0 | v1-0-0 |
| refs/tags/product@1.0.0-rc.2 | product-1-0-0-rc-2 |
| refs/heads/New_Awesome_Product | new-awesome-product |
| GITHUB_REF | GITHUB_REF_SLUG | GITHUB_REF_SLUG_URL |
|--------------------------------|---------------------|---------------------|
| refs/heads/master | master | master |
| refs/heads/feat/new_feature | feat-new-feature | feat-new-feature |
| 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 | product-1-0-0-rc-2 |
| refs/heads/New_Awesome_Product | new-awesome-product | new-awesome-product |

_Additional variables (only set for forked repositories) :_

- `GITHUB_HEAD_REF_SLUG` : The branch of the head repository **GITHUB_HEAD_REF**,
- `GITHUB_BASE_REF_SLUG` : The branch of the base repository **GITHUB_BASE_REF**.
- `GITHUB_HEAD_REF_SLUG`/`GITHUB_HEAD_REF_SLUG_URL`, The branch of the head repository **GITHUB_HEAD_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

Short the environment variable **GITHUB_SHA**

The commit SHA that triggered the workflow

| Environment variable (GITHUB_SHA) | Short variable (GITHUB_SHA_SHORT) |
|------------------------------------------|-----------------------------------|
| ffac537e6cbbf934b08745a378932722df287a53 | ffac537e |
| GITHUB_SHA | GITHUB_SHA_SHORT |
|------------------------------------------|------------------|
| ffac537e6cbbf934b08745a378932722df287a53 | ffac537e |
12 changes: 12 additions & 0 deletions entrypoint.sh
@@ -1,6 +1,13 @@
#!/bin/sh -l

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" |
tr "[:upper:]" "[:lower:]" |
sed -r 's#refs/[^\/]*/##;s/[~\^]+//g;s/[^a-zA-Z0-9]+/-/g;s/^-+\|-+$//g;s/^-*//;s/-*$//' |
Expand All @@ -15,4 +22,9 @@ short_sha() {
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_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")"
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -3,7 +3,7 @@
"version": "1.1.0",
"license": "MIT",
"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"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion tests/short_sha.bats
@@ -1,6 +1,6 @@
#!/usr/bin/env bats

@test "Short long hash" {
@test "short_sha: long hash" {
test_short_sha \
"a35a1a486a260cfd99c5b6f8c6034a2929ba9b3f" \
"a35a1a48"
Expand Down
34 changes: 17 additions & 17 deletions tests/slug_ref.bats
@@ -1,51 +1,51 @@
#!/usr/bin/env bats

@test "Slug master branch" {
test_sluf_ref \
@test "slug_ref:: master branch" {
test_slug_ref \
"refs/heads/master" \
"master"
}

@test "Slug a feature branch" {
test_sluf_ref \
@test "slug_ref: a feature branch" {
test_slug_ref \
"refs/heads/feat/new_feature" \
"feat-new-feature"
}

@test "Slug a fix branch" {
test_sluf_ref \
@test "slug_ref: a fix branch" {
test_slug_ref \
"refs/heads/fix/issue_number" \
"fix-issue-number"
}

@test "Slug a simple tag" {
test_sluf_ref \
@test "slug_ref: a simple tag" {
test_slug_ref \
"refs/tags/v1.0.0" \
"v1-0-0"
"v1.0.0"
}

@test "Slug a complex tag" {
test_sluf_ref \
@test "slug_ref: a complex tag" {
test_slug_ref \
"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_sluf_ref \
@test "slug_ref: a reference with upper case letters" {
test_slug_ref \
"refs/heads/New_Awesome_Product" \
"new-awesome-product"
}

@test "Slug a very long name" {
test_sluf_ref \
@test "slug_ref: a very long name" {
test_slug_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_sluf_ref() {
test_slug_ref() {
given="${1}"
expected="${2}"

Expand Down
55 changes: 55 additions & 0 deletions tests/slug_url_ref.bats
@@ -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}" ]
}

0 comments on commit e95fe45

Please sign in to comment.