Compare commits

..

3 Commits

Author SHA1 Message Date
rlespinasse
ca9a67fa1f feat(slug): expose GITHUB_REPOSITORY slugs 2020-04-26 02:12:41 +02:00
rlespinasse
e95fe45d8b 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>
2020-04-26 02:12:28 +02:00
rlespinasse
250b75dc58 ci: introduce version 2.x serie 2020-04-25 23:17:37 +02:00
13 changed files with 303 additions and 100 deletions

View File

@@ -1,3 +0,0 @@
FROM bats/bats:latest
CMD ["./tests"]

View File

@@ -5,31 +5,18 @@ 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:
semantic_version: 17.0.7
branches: |
[
'v1.1.x',
'v2.x'
]
extra_plugins: |
@semantic-release/changelog@5.0.1
@semantic-release/git@9.0.0
@semantic-release/git
@semantic-release/changelog
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./
- name: Output
run: |
echo "Slug variables"
echo " ref : ${{ env.GITHUB_REF_SLUG }}"
echo " head ref : ${{ env.GITHUB_HEAD_REF_SLUG }}"
echo " base ref : ${{ env.GITHUB_BASE_REF_SLUG }}"
echo "Short SHA variables"
echo " sha : ${{ env.GITHUB_SHA_SHORT }}"

View File

@@ -1,17 +1,3 @@
# [1.2.0](http://github.com/rlespinasse/github-slug-action/compare/1.1.1...1.2.0) (2021-04-05)
### Features
* add deprecation warning ([c9eec68](http://github.com/rlespinasse/github-slug-action/commit/c9eec68e1a02f91b77e9b978ad23527085244aaa))
## [1.1.1](http://github.com/rlespinasse/github-slug-action/compare/1.1.0...1.1.1) (2020-10-07)
### Bug Fixes
* remove set-env due to CVE fix ([6c69465](http://github.com/rlespinasse/github-slug-action/commit/6c69465e62a6430b5742c5bc0fce732f55630ee7))
# [1.1.0](http://github.com/rlespinasse/github-slug-action/compare/1.0.2...1.1.0) (2019-11-11)

View File

@@ -6,48 +6,76 @@ 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@v1.1.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 variables"
echo " - ${{ env.GITHUB_REF_SLUG }}"
echo " - ${{ env.GITHUB_HEAD_REF_SLUG }}"
echo " - ${{ env.GITHUB_BASE_REF_SLUG }}"
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 }}"
```
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 |
> **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` : 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**
### GITHUB_REPOSITORY_SLUG
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_URL is design to be used as subdomain in an URL.
### GITHUB_SHA_SHORT
@@ -55,6 +83,26 @@ 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 |
### 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,11 +1,14 @@
# Security Policy
## Supported Versions
## Supported Versions and Branches
| Version | Supported |
|---------|--------------------|
| 1.1.x | :white_check_mark: |
| < 1.0.x | :x: |
| Version | Branch | Supported |
|---------|--------|--------------------|
| 2.x | v2.x | :white_check_mark: |
| 1.1.x | v1.1.x | :white_check_mark: |
| < 1.0.x | master | :x: |
A GitHub repository can used one of the available branches as action inside its workflows.
## Reporting a Vulnerability

View File

@@ -1,6 +1,27 @@
#!/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:]" |
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/-*$//' |
@@ -12,11 +33,15 @@ short_sha() {
cut -c1-8
}
{
echo "GITHUB_REF_SLUG=$(slug_ref "$GITHUB_REF")"
echo "GITHUB_HEAD_REF_SLUG=$(slug_ref "$GITHUB_HEAD_REF")"
echo "GITHUB_BASE_REF_SLUG=$(slug_ref "$GITHUB_BASE_REF")"
echo "GITHUB_SHA_SHORT=$(short_sha "$GITHUB_SHA")"
} >>"$GITHUB_ENV"
echo ::set-env name=GITHUB_REPOSITORY_SLUG::"$(slug "$GITHUB_REPOSITORY")"
echo ::set-env name=GITHUB_REPOSITORY_SLUG_URL::"$(slug_url "$GITHUB_REPOSITORY")"
echo "::warning ::This version is deprecated, please use 'rlespinasse/github-slug-action@v3.x' instead"
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
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{
"name": "github-slug-action",
"version": "0.0.0",
"version": "1.0.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@@ -1,9 +1,9 @@
{
"name": "github-slug-action",
"version": "0.0.0",
"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": {
@@ -11,9 +11,9 @@
"url": "http://github.com/rlespinasse/github-slug-action.git"
},
"devDependencies": {
"@semantic-release/changelog": "5.0.1",
"@semantic-release/git": "9.0.0",
"semantic-release": "17.0.7"
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"semantic-release": "^17.0.7"
},
"release": {
"tagFormat": "${version}",
@@ -21,12 +21,16 @@
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
"@semantic-release/git",
[
"@semantic-release/git",
{
"assets": [
"package.json"
],
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
}
],
"@semantic-release/github"
],
"branches": [
"v1.1.x",
"v2.x"
]
}
}

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bats
@test "Short long hash" {
@test "short_sha: long hash" {
test_short_sha \
"a35a1a486a260cfd99c5b6f8c6034a2929ba9b3f" \
"a35a1a48"

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}" ]
}

View File

@@ -1,43 +1,43 @@
#!/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"
}
@@ -45,7 +45,7 @@
# Load sluf_ref function
source entrypoint.sh > /dev/null 2>&1
test_sluf_ref() {
test_slug_ref() {
given="${1}"
expected="${2}"

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}" ]
}

55
tests/slug_url_ref.bats Normal file
View 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}" ]
}