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

View file

@ -10,10 +10,10 @@ A [GitHub Action](https://github.com/features/actions) to expose useful environm
### Environment Variables exposed by **this Action** ### Environment Variables exposed by **this Action**
| Environment Variable Name | Description | Example value | | Environment Variable Name | Description | Example value |
|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------|---------------------------| |---------------------------|---------------------------------------------------------------------------------------------------------------------------------------|--------------------------|
| `GITHUB_REPOSITORY_OWNER` | The owner of the repository. | `FranzDiebold` | | `GITHUB_REPOSITORY_OWNER` | The owner of the repository. | `FranzDiebold` |
| `GITHUB_REPOSITORY_NAME` | The name of the repository. | `github-env-vars-action` | | `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_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` | | `GITHUB_SHA_SHORT` | The shortened commit SHA (8 characters) that triggered the workflow. | `ffac537e` |
@ -22,17 +22,17 @@ A [GitHub Action](https://github.com/features/actions) to expose useful environm
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). 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 | | Environment Variable Name | Description | Example value |
|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------| |---------------------------|---------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------|
| `GITHUB_ACTOR` | The name of the person or app that initiated the workflow. | `octocat` | | `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_REPOSITORY` | The owner and repository name. | `FranzDiebold/github-env-vars-action` |
| `GITHUB_SHA` | The commit SHA that triggered the workflow. | `ffac537e6cbbf934b08745a378932722df287a53` | | `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` | | `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 ## Example usage
```yaml ```yaml
steps: steps:
- uses: franzdiebold/github-env-vars-action@v1.1.0 - uses: franzdiebold/github-env-vars-action@v1.1.1
- name: Print environment variables - name: Print environment variables
run: | run: |
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER" echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"

52
dist/index.js vendored
View file

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

View file

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

2
package-lock.json generated
View file

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

View file

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