feat(slug): expose GITHUB_REPOSITORY slugs

This commit is contained in:
rlespinasse
2020-04-26 01:47:01 +02:00
parent e95fe45d8b
commit ca9a67fa1f
4 changed files with 152 additions and 18 deletions

View File

@@ -24,14 +24,16 @@ Others `Slug`-ish commands are available:
- name: Print slug/short variables
run: |
echo "Slug reference variables"
echo "Slug 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_REPOSITORY_SLUG }}"
echo "Slug URL variables"
echo " - ${{ env.GITHUB_REF_SLUG_URL }}"
echo " - ${{ env.GITHUB_HEAD_REF_SLUG_URL }}"
echo " - ${{ env.GITHUB_BASE_REF_SLUG_URL }}"
echo " - ${{ env.GITHUB_REPOSITORY_SLUG_URL }}"
echo "Short SHA variables"
echo " - ${{ env.GITHUB_SHA_SHORT }}"
```
@@ -53,30 +55,27 @@ _If neither a branch or tag is available for the event type, the variable will n
| 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 |
> **NOTE :**
> GITHUB_REF_SLUG_URL is design to be used as subdomain in an URL.
_Additional variables (only set for forked repositories) :_
- `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
### GITHUB_REPOSITORY_SLUG
In an URL, use `GITHUB_REF_SLUG_URL` instead of **GITHUB_REF_SLUG** as subdomain to be compliant.
Slug the environment variable **GITHUB_REPOSITORY**
The owner and repository name.
| GITHUB_REPOSITORY | GITHUB_REPOSITORY_SLUG | GITHUB_REPOSITORY_SLUG_URL |
|----------------------------|----------------------------|----------------------------|
| octocat/Hello-World | octocat-hello-world | octocat-hello-world |
| rlespinasse/Hello-World.go | rlespinasse-hello-world.go | rlespinasse-hello-world-go |
> **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_REF_SLUG_URL is design to be used as subdomain in an URL.
### GITHUB_SHA_SHORT
@@ -87,3 +86,23 @@ The commit SHA that triggered the workflow
| GITHUB_SHA | GITHUB_SHA_SHORT |
|------------------------------------------|------------------|
| ffac537e6cbbf934b08745a378932722df287a53 | ffac537e |
### Use slug variable in an URL
In an URL, use `<GITHUB_VARIABLE>_SLUG_URL` instead of **<GITHUB_VARIABLE>_SLUG** as subdomain to be compliant.
> **NOTE :**
> <GITHUB_VARIABLE>_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_VARIABLE>_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_VARIABLE>_SLUG }}"
```

View File

@@ -1,5 +1,19 @@
#!/bin/sh -l
slug() {
echo "$1" |
tr "[:upper:]" "[:lower:]" |
sed -r 's/[~\^]+//g;s/[^a-zA-Z0-9.]+/-/g;s/^-+\|-+$//g;s/^-*//;s/-*$//' |
cut -c1-63
}
slug_url() {
echo "$1" |
tr "[:upper:]" "[:lower:]" |
sed -r 's/[~\^]+//g;s/[^a-zA-Z0-9]+/-/g;s/^-+\|-+$//g;s/^-*//;s/-*$//' |
cut -c1-63
}
slug_ref() {
echo "$1" |
tr "[:upper:]" "[:lower:]" |
@@ -19,6 +33,9 @@ short_sha() {
cut -c1-8
}
echo ::set-env name=GITHUB_REPOSITORY_SLUG::"$(slug "$GITHUB_REPOSITORY")"
echo ::set-env name=GITHUB_REPOSITORY_SLUG_URL::"$(slug_url "$GITHUB_REPOSITORY")"
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")"

49
tests/slug.bats Normal file
View File

@@ -0,0 +1,49 @@
#!/usr/bin/env bats
@test "slug: a word" {
test_slug \
"word" \
"word"
}
@test "slug: a string" {
test_slug \
"basic-string" \
"basic-string"
}
@test "slug: a string in camel case" {
test_slug \
"camelCase" \
"camelcase"
}
@test "slug: a path" {
test_slug \
"path/to/something" \
"path-to-something"
}
@test "slug: a number" {
test_slug \
"4.2" \
"4.2"
}
@test "slug: a very long string" {
test_slug \
"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() {
given="${1}"
expected="${2}"
actual="$(slug \"${given}\")"
echo "expected : [${expected}], actual : [${actual}]"
[ "${actual}" == "${expected}" ]
}

49
tests/slug_url.bats Normal file
View File

@@ -0,0 +1,49 @@
#!/usr/bin/env bats
@test "slug_url: a word" {
test_slug_url \
"word" \
"word"
}
@test "slug_url: a string" {
test_slug_url \
"basic-string" \
"basic-string"
}
@test "slug_url: a string in camel case" {
test_slug_url \
"camelCase" \
"camelcase"
}
@test "slug_url: a path" {
test_slug_url \
"path/to/something" \
"path-to-something"
}
@test "slug_url: a number" {
test_slug_url \
"4.2" \
"4-2"
}
@test "slug_url: a very long string" {
test_slug_url \
"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() {
given="${1}"
expected="${2}"
actual="$(slug_url \"${given}\")"
echo "expected : [${expected}], actual : [${actual}]"
[ "${actual}" == "${expected}" ]
}