mirror of
https://github.com/FranzDiebold/github-env-vars-action.git
synced 2024-11-22 16:51:03 -05:00
Fix GitHub ref for branches with slash.
This commit is contained in:
parent
499429c001
commit
38d00ef958
6 changed files with 104 additions and 52 deletions
6
.github/workflows/demo.yml
vendored
6
.github/workflows/demo.yml
vendored
|
@ -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"
|
||||
|
|
10
README.md
10
README.md
|
@ -10,10 +10,10 @@ 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_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` |
|
||||
|
||||
|
||||
|
@ -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).
|
||||
|
||||
| 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` |
|
||||
| `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"
|
||||
|
|
52
dist/index.js
vendored
52
dist/index.js
vendored
|
@ -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]));
|
||||
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]));
|
||||
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]));
|
||||
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));
|
||||
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);
|
||||
}
|
||||
|
|
52
index.js
52
index.js
|
@ -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]));
|
||||
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]));
|
||||
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]));
|
||||
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));
|
||||
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
2
package-lock.json
generated
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "github-env-vars-action",
|
||||
"version": "1.1.0",
|
||||
"version": "1.1.1",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
|
|
@ -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": {
|
||||
|
|
Loading…
Reference in a new issue