Merge pull request #7 from FranzDiebold/fix/fix-github-ref

Fix GitHub ref for branches with slash.
This commit is contained in:
Franz Diebold 2020-07-14 10:28:14 +02:00 committed by GitHub
commit a1deed7ade
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 104 additions and 52 deletions

View file

@ -7,7 +7,7 @@ jobs:
name: Linux Demo
runs-on: ubuntu-latest
steps:
- uses: franzdiebold/github-env-vars-action@v1.1.0
- uses: franzdiebold/github-env-vars-action@v1.1.1
- name: Print environment variables
run: |
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"
@ -19,7 +19,7 @@ jobs:
name: Windows Demo
runs-on: windows-latest
steps:
- uses: franzdiebold/github-env-vars-action@v1.1.0
- uses: franzdiebold/github-env-vars-action@v1.1.1
- name: Print environment variables
run: |
echo "GITHUB_REPOSITORY_OWNER=$Env:GITHUB_REPOSITORY_OWNER"
@ -31,7 +31,7 @@ jobs:
name: macOS Demo
runs-on: macos-latest
steps:
- uses: franzdiebold/github-env-vars-action@v1.1.0
- uses: franzdiebold/github-env-vars-action@v1.1.1
- name: Print environment variables
run: |
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"

View file

@ -9,30 +9,30 @@ A [GitHub Action](https://github.com/features/actions) to expose useful environm
### Environment Variables exposed by **this Action**
| Environment Variable Name | Description | Example value |
|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------|---------------------------|
| `GITHUB_REPOSITORY_OWNER` | The owner of the repository. | `FranzDiebold` |
| `GITHUB_REPOSITORY_NAME` | The name of the repository. | `github-env-vars-action` |
| `GITHUB_REF_NAME` | The branch name that triggered the workflow. If neither a branch or tag is available for the event type, the variable will not exist. | `feature-branch-1` |
| `GITHUB_SHA_SHORT` | The shortened commit SHA (8 characters) that triggered the workflow. | `ffac537e` |
| Environment Variable Name | Description | Example value |
|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------|--------------------------|
| `GITHUB_REPOSITORY_OWNER` | The owner of the repository. | `FranzDiebold` |
| `GITHUB_REPOSITORY_NAME` | The name of the repository. | `github-env-vars-action` |
| `GITHUB_REF_NAME` | The branch name that triggered the workflow. If neither a branch or tag is available for the event type, the variable will not exist. | `feat/feature-branch-1` |
| `GITHUB_SHA_SHORT` | The shortened commit SHA (8 characters) that triggered the workflow. | `ffac537e` |
### Default Environment Variables exposed by GitHub
For a full list of default environment variables exposed by GitHub see [https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables](https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables).
| Environment Variable Name | Description | Example value |
|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|
| `GITHUB_ACTOR` | The name of the person or app that initiated the workflow. | `octocat` |
| `GITHUB_REPOSITORY` | The owner and repository name. | `FranzDiebold/github-env-vars-action` |
| `GITHUB_SHA` | The commit SHA that triggered the workflow. | `ffac537e6cbbf934b08745a378932722df287a53` |
| `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. | `refs/heads/feature-branch-1` |
| Environment Variable Name | Description | Example value |
|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------|
| `GITHUB_ACTOR` | The name of the person or app that initiated the workflow. | `octocat` |
| `GITHUB_REPOSITORY` | The owner and repository name. | `FranzDiebold/github-env-vars-action` |
| `GITHUB_SHA` | The commit SHA that triggered the workflow. | `ffac537e6cbbf934b08745a378932722df287a53` |
| `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. | `refs/heads/feat/feature-branch-1` |
## Example usage
```yaml
steps:
- uses: franzdiebold/github-env-vars-action@v1.1.0
- uses: franzdiebold/github-env-vars-action@v1.1.1
- name: Print environment variables
run: |
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"

60
dist/index.js vendored
View file

@ -163,31 +163,57 @@ module.exports = require("path");
const core = __webpack_require__(827);
function slugify(inputString) {
return inputString.replace(/^\s+|\s+$/g, '') // trim
.toLowerCase()
.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
function getShaShort(fullSha) {
return fullSha ? fullSha.substring(0, 8) : null;
}
function getRepositoryOwner(repository) {
return repository ? repository.split('/')[0] : null;
}
function getRepositoryName(repository) {
return repository ? repository.split('/')[1] : null;
}
function getRefName(ref) {
return ref ? ref.split('/').slice(2).join('/') : null;
}
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables
try {
splitted_github_repository = process.env.GITHUB_REPOSITORY.split('/'); // FranzDiebold/github-env-vars-action
repository = process.env.GITHUB_REPOSITORY; // FranzDiebold/github-env-vars-action
core.exportVariable('GITHUB_REPOSITORY_OWNER', slugify(splitted_github_repository[0]));
core.info(`Set GITHUB_REPOSITORY_OWNER=${process.env.GITHUB_REPOSITORY_OWNER}`);
repositoryOwner = getRepositoryOwner(repository);
if (repositoryOwner) {
core.exportVariable('GITHUB_REPOSITORY_OWNER', repositoryOwner);
core.info(`Set GITHUB_REPOSITORY_OWNER=${process.env.GITHUB_REPOSITORY_OWNER}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_OWNER".');
}
core.exportVariable('GITHUB_REPOSITORY_NAME', slugify(splitted_github_repository[1]));
core.info(`Set GITHUB_REPOSITORY_NAME=${process.env.GITHUB_REPOSITORY_NAME}`);
repositoryName = getRepositoryName(repository);
if (repositoryName) {
core.exportVariable('GITHUB_REPOSITORY_NAME', repositoryName);
core.info(`Set GITHUB_REPOSITORY_NAME=${process.env.GITHUB_REPOSITORY_NAME}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_NAME".');
}
splitted_github_ref = process.env.GITHUB_REF.split('/'); // refs/heads/feature-branch-1
core.exportVariable('GITHUB_REF_NAME', slugify(splitted_github_ref[2]));
core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`);
refName = getRefName(process.env.GITHUB_REF); // refs/heads/feat/feature-branch-1
if (refName) {
core.exportVariable('GITHUB_REF_NAME', refName);
core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`);
} else {
core.warning('Environment variable "GITHUB_REF" not set. Cannot set "GITHUB_REF_NAME".');
}
github_sha = process.env.GITHUB_SHA; // ffac537e6cbbf934b08745a378932722df287a53
core.exportVariable('GITHUB_SHA_SHORT', github_sha.substring(0, 8));
core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`);
shaShort = getShaShort(process.env.GITHUB_SHA); // ffac537e6cbbf934b08745a378932722df287a53
if (shaShort) {
core.exportVariable('GITHUB_SHA_SHORT', shaShort);
core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`);
} else {
core.warning('Environment variable "GITHUB_SHA" not set. Cannot set "GITHUB_SHA_SHORT".');
}
} catch (error) {
core.setFailed(error.message);
}

View file

@ -2,31 +2,57 @@
const core = require('@actions/core');
function slugify(inputString) {
return inputString.replace(/^\s+|\s+$/g, '') // trim
.toLowerCase()
.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
function getShaShort(fullSha) {
return fullSha ? fullSha.substring(0, 8) : null;
}
function getRepositoryOwner(repository) {
return repository ? repository.split('/')[0] : null;
}
function getRepositoryName(repository) {
return repository ? repository.split('/')[1] : null;
}
function getRefName(ref) {
return ref ? ref.split('/').slice(2).join('/') : null;
}
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables
try {
splitted_github_repository = process.env.GITHUB_REPOSITORY.split('/'); // FranzDiebold/github-env-vars-action
repository = process.env.GITHUB_REPOSITORY; // FranzDiebold/github-env-vars-action
core.exportVariable('GITHUB_REPOSITORY_OWNER', slugify(splitted_github_repository[0]));
core.info(`Set GITHUB_REPOSITORY_OWNER=${process.env.GITHUB_REPOSITORY_OWNER}`);
repositoryOwner = getRepositoryOwner(repository);
if (repositoryOwner) {
core.exportVariable('GITHUB_REPOSITORY_OWNER', repositoryOwner);
core.info(`Set GITHUB_REPOSITORY_OWNER=${process.env.GITHUB_REPOSITORY_OWNER}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_OWNER".');
}
core.exportVariable('GITHUB_REPOSITORY_NAME', slugify(splitted_github_repository[1]));
core.info(`Set GITHUB_REPOSITORY_NAME=${process.env.GITHUB_REPOSITORY_NAME}`);
repositoryName = getRepositoryName(repository);
if (repositoryName) {
core.exportVariable('GITHUB_REPOSITORY_NAME', repositoryName);
core.info(`Set GITHUB_REPOSITORY_NAME=${process.env.GITHUB_REPOSITORY_NAME}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_NAME".');
}
splitted_github_ref = process.env.GITHUB_REF.split('/'); // refs/heads/feature-branch-1
core.exportVariable('GITHUB_REF_NAME', slugify(splitted_github_ref[2]));
core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`);
refName = getRefName(process.env.GITHUB_REF); // refs/heads/feat/feature-branch-1
if (refName) {
core.exportVariable('GITHUB_REF_NAME', refName);
core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`);
} else {
core.warning('Environment variable "GITHUB_REF" not set. Cannot set "GITHUB_REF_NAME".');
}
github_sha = process.env.GITHUB_SHA; // ffac537e6cbbf934b08745a378932722df287a53
core.exportVariable('GITHUB_SHA_SHORT', github_sha.substring(0, 8));
core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`);
shaShort = getShaShort(process.env.GITHUB_SHA); // ffac537e6cbbf934b08745a378932722df287a53
if (shaShort) {
core.exportVariable('GITHUB_SHA_SHORT', shaShort);
core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`);
} else {
core.warning('Environment variable "GITHUB_SHA" not set. Cannot set "GITHUB_SHA_SHORT".');
}
} catch (error) {
core.setFailed(error.message);
}

2
package-lock.json generated
View file

@ -1,6 +1,6 @@
{
"name": "github-env-vars-action",
"version": "1.1.0",
"version": "1.1.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View file

@ -1,6 +1,6 @@
{
"name": "github-env-vars-action",
"version": "1.1.0",
"version": "1.1.1",
"description": "A GitHub Action to expose useful environment variables.",
"main": "index.js",
"scripts": {