Merge pull request #26 from FranzDiebold/feat/rename-env-vars

Feat/rename env vars
This commit is contained in:
Franz Diebold 2020-12-01 14:50:09 +01:00 committed by GitHub
commit 406dcfc60a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 252 additions and 177 deletions

View file

@ -7,22 +7,28 @@ jobs:
name: Linux Demo
runs-on: ubuntu-latest
steps:
- uses: FranzDiebold/github-env-vars-action@v1.3.0
- uses: FranzDiebold/github-env-vars-action@v2
- name: Print environment variables exposed by this action
run: |
echo "GITHUB_REPOSITORY_SLUG=$GITHUB_REPOSITORY_SLUG"
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"
echo "GITHUB_REPOSITORY_OWNER_SLUG=$GITHUB_REPOSITORY_OWNER_SLUG"
echo "GITHUB_REPOSITORY_NAME=$GITHUB_REPOSITORY_NAME"
echo "GITHUB_REPOSITORY_NAME_SLUG=$GITHUB_REPOSITORY_NAME_SLUG"
echo "GITHUB_REF_SLUG=$GITHUB_REF_SLUG"
echo "GITHUB_BRANCH_NAME=$GITHUB_BRANCH_NAME"
echo "GITHUB_BRANCH_NAME_SLUG=$GITHUB_BRANCH_NAME_SLUG"
echo "GITHUB_REF_NAME=$GITHUB_REF_NAME"
echo "GITHUB_REF_NAME_SLUG=$GITHUB_REF_NAME_SLUG"
echo "GITHUB_HEAD_REF_SLUG=$GITHUB_HEAD_REF_SLUG"
echo "GITHUB_BASE_REF_SLUG=$GITHUB_BASE_REF_SLUG"
echo "GITHUB_SHA_SHORT=$GITHUB_SHA_SHORT"
echo "CI_REPOSITORY_SLUG=$CI_REPOSITORY_SLUG"
echo "CI_REPOSITORY_OWNER=$CI_REPOSITORY_OWNER"
echo "CI_REPOSITORY_OWNER_SLUG=$CI_REPOSITORY_OWNER_SLUG"
echo "CI_REPOSITORY_NAME=$CI_REPOSITORY_NAME"
echo "CI_REPOSITORY_NAME_SLUG=$CI_REPOSITORY_NAME_SLUG"
echo "CI_REPOSITORY=$CI_REPOSITORY"
echo "CI_REF_SLUG=$CI_REF_SLUG"
echo "CI_ACTION_REF_NAME=$CI_ACTION_REF_NAME"
echo "CI_ACTION_REF_NAME_SLUG=$CI_ACTION_REF_NAME_SLUG"
echo "CI_REF_NAME=$CI_REF_NAME"
echo "CI_REF_NAME_SLUG=$CI_REF_NAME_SLUG"
echo "CI_REF=$CI_REF"
echo "CI_HEAD_REF_SLUG=$CI_HEAD_REF_SLUG"
echo "CI_HEAD_REF=$CI_HEAD_REF"
echo "CI_BASE_REF_SLUG=$CI_BASE_REF_SLUG"
echo "CI_BASE_REF=$CI_BASE_REF"
echo "CI_SHA_SHORT=$CI_SHA_SHORT"
echo "CI_SHA=$CI_SHA"
echo "CI_ACTOR=$CI_ACTOR"
- name: Print environment variables exposed by GitHub
run: |
echo "GITHUB_ACTOR=$GITHUB_ACTOR"
@ -41,22 +47,28 @@ jobs:
name: Windows Demo
runs-on: windows-latest
steps:
- uses: FranzDiebold/github-env-vars-action@v1.3.0
- uses: FranzDiebold/github-env-vars-action@v2
- name: Print environment variables exposed by this action
run: |
echo "GITHUB_REPOSITORY_SLUG=$Env:GITHUB_REPOSITORY_SLUG"
echo "GITHUB_REPOSITORY_OWNER=$Env:GITHUB_REPOSITORY_OWNER"
echo "GITHUB_REPOSITORY_OWNER_SLUG=$Env:GITHUB_REPOSITORY_OWNER_SLUG"
echo "GITHUB_REPOSITORY_NAME=$Env:GITHUB_REPOSITORY_NAME"
echo "GITHUB_REPOSITORY_NAME_SLUG=$Env:GITHUB_REPOSITORY_NAME_SLUG"
echo "GITHUB_REF_SLUG=$Env:GITHUB_REF_SLUG"
echo "GITHUB_BRANCH_NAME=$Env:GITHUB_BRANCH_NAME"
echo "GITHUB_BRANCH_NAME_SLUG=$Env:GITHUB_BRANCH_NAME_SLUG"
echo "GITHUB_REF_NAME=$Env:GITHUB_REF_NAME"
echo "GITHUB_REF_NAME_SLUG=$Env:GITHUB_REF_NAME_SLUG"
echo "GITHUB_HEAD_REF_SLUG=$Env:GITHUB_HEAD_REF_SLUG"
echo "GITHUB_BASE_REF_SLUG=$Env:GITHUB_BASE_REF_SLUG"
echo "GITHUB_SHA_SHORT=$Env:GITHUB_SHA_SHORT"
echo "CI_REPOSITORY_SLUG=$Env:CI_REPOSITORY_SLUG"
echo "CI_REPOSITORY_OWNER=$Env:CI_REPOSITORY_OWNER"
echo "CI_REPOSITORY_OWNER_SLUG=$Env:CI_REPOSITORY_OWNER_SLUG"
echo "CI_REPOSITORY_NAME=$Env:CI_REPOSITORY_NAME"
echo "CI_REPOSITORY_NAME_SLUG=$Env:CI_REPOSITORY_NAME_SLUG"
echo "CI_REPOSITORY=$Env:CI_REPOSITORY"
echo "CI_REF_SLUG=$Env:CI_REF_SLUG"
echo "CI_ACTION_REF_NAME=$Env:CI_ACTION_REF_NAME"
echo "CI_ACTION_REF_NAME_SLUG=$Env:CI_ACTION_REF_NAME_SLUG"
echo "CI_REF_NAME=$Env:CI_REF_NAME"
echo "CI_REF_NAME_SLUG=$Env:CI_REF_NAME_SLUG"
echo "CI_REF=$Env:CI_REF"
echo "CI_HEAD_REF_SLUG=$Env:CI_HEAD_REF_SLUG"
echo "CI_HEAD_REF=$Env:CI_HEAD_REF"
echo "CI_BASE_REF_SLUG=$Env:CI_BASE_REF_SLUG"
echo "CI_BASE_REF=$Env:CI_BASE_REF"
echo "CI_SHA_SHORT=$Env:CI_SHA_SHORT"
echo "CI_SHA=$Env:CI_SHA"
echo "CI_ACTOR=$Env:CI_ACTOR"
- name: Print environment variables exposed by GitHub
run: |
echo "GITHUB_ACTOR=$Env:GITHUB_ACTOR"
@ -75,22 +87,28 @@ jobs:
name: macOS Demo
runs-on: macos-latest
steps:
- uses: FranzDiebold/github-env-vars-action@v1.3.0
- uses: FranzDiebold/github-env-vars-action@v2
- name: Print environment variables exposed by this action
run: |
echo "GITHUB_REPOSITORY_SLUG=$GITHUB_REPOSITORY_SLUG"
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"
echo "GITHUB_REPOSITORY_OWNER_SLUG=$GITHUB_REPOSITORY_OWNER_SLUG"
echo "GITHUB_REPOSITORY_NAME=$GITHUB_REPOSITORY_NAME"
echo "GITHUB_REPOSITORY_NAME_SLUG=$GITHUB_REPOSITORY_NAME_SLUG"
echo "GITHUB_REF_SLUG=$GITHUB_REF_SLUG"
echo "GITHUB_BRANCH_NAME=$GITHUB_BRANCH_NAME"
echo "GITHUB_BRANCH_NAME_SLUG=$GITHUB_BRANCH_NAME_SLUG"
echo "GITHUB_REF_NAME=$GITHUB_REF_NAME"
echo "GITHUB_REF_NAME_SLUG=$GITHUB_REF_NAME_SLUG"
echo "GITHUB_HEAD_REF_SLUG=$GITHUB_HEAD_REF_SLUG"
echo "GITHUB_BASE_REF_SLUG=$GITHUB_BASE_REF_SLUG"
echo "GITHUB_SHA_SHORT=$GITHUB_SHA_SHORT"
echo "CI_REPOSITORY_SLUG=$CI_REPOSITORY_SLUG"
echo "CI_REPOSITORY_OWNER=$CI_REPOSITORY_OWNER"
echo "CI_REPOSITORY_OWNER_SLUG=$CI_REPOSITORY_OWNER_SLUG"
echo "CI_REPOSITORY_NAME=$CI_REPOSITORY_NAME"
echo "CI_REPOSITORY_NAME_SLUG=$CI_REPOSITORY_NAME_SLUG"
echo "CI_REPOSITORY=$CI_REPOSITORY"
echo "CI_REF_SLUG=$CI_REF_SLUG"
echo "CI_ACTION_REF_NAME=$CI_ACTION_REF_NAME"
echo "CI_ACTION_REF_NAME_SLUG=$CI_ACTION_REF_NAME_SLUG"
echo "CI_REF_NAME=$CI_REF_NAME"
echo "CI_REF_NAME_SLUG=$CI_REF_NAME_SLUG"
echo "CI_REF=$CI_REF"
echo "CI_HEAD_REF_SLUG=$CI_HEAD_REF_SLUG"
echo "CI_HEAD_REF=$CI_HEAD_REF"
echo "CI_BASE_REF_SLUG=$CI_BASE_REF_SLUG"
echo "CI_BASE_REF=$CI_BASE_REF"
echo "CI_SHA_SHORT=$CI_SHA_SHORT"
echo "CI_SHA=$CI_SHA"
echo "CI_ACTOR=$CI_ACTOR"
- name: Print environment variables exposed by GitHub
run: |
echo "GITHUB_ACTOR=$GITHUB_ACTOR"

View file

@ -2,7 +2,7 @@
[![GitHub Action: View on Marketplace](https://img.shields.io/badge/GitHub%20Action-View_on_Marketplace-28a745?logo=github)](https://github.com/marketplace/actions/github-environment-variables-action)
[![Demo: available](https://img.shields.io/badge/Demo-available-orange)](.github/workflows/demo.yml)
[![Version: v1.3.0](https://img.shields.io/badge/Version-v1.3.0-brightgreen)](https://github.com/FranzDiebold/github-env-vars-action/releases/tag/v1.3.0)
[![Version: v2.0.0](https://img.shields.io/badge/Version-v2.0.0-brightgreen)](https://github.com/FranzDiebold/github-env-vars-action/releases/tag/v2.0.0)
[![Lint and Test](https://github.com/FranzDiebold/github-env-vars-action/workflows/Lint%20and%20Test/badge.svg)](https://github.com/FranzDiebold/github-env-vars-action/actions?query=workflow%3A%22Lint+and+Test%22)
[![license: MIT](https://img.shields.io/badge/license-MIT-brightgreen.svg)](./LICENSE)
@ -10,21 +10,27 @@ 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_SLUG` | The slug of the owner and repository name (i.e. slug of `FranzDiebold/github-env-vars-action`). | `franzdiebold-github-env-vars-action` |
| `GITHUB_REPOSITORY_OWNER` | The owner of the repository. | `FranzDiebold` |
| `GITHUB_REPOSITORY_OWNER_SLUG` | The slug of the owner of the repository. | `franzdiebold` |
| `GITHUB_REPOSITORY_NAME` | The name of the repository. | `github-env-vars-action` |
| `GITHUB_REPOSITORY_NAME_SLUG` | The slug of the name of the repository. | `github-env-vars-action` |
| `GITHUB_REF_SLUG` | The slug of the branch or tag *ref* that triggered the workflow (i.e. slug of `refs/heads/feat/feature-branch-1`). <br>If neither a branch or tag is available for the event type, the variable will not exist. | `refs-heads-feat-feature-branch-1` |
| `GITHUB_BRANCH_NAME` | The branch *name* that triggered the workflow. For pull requests it is the *head* branch name. | `feat/feature-branch-1` |
| `GITHUB_BRANCH_NAME_SLUG` | The slug of the branch *name* that triggered the workflow. For pull requests it is the slug of the *head* branch name. | `feat-feature-branch-1` |
| `GITHUB_REF_NAME` | The branch or tag *name* that triggered the workflow. <br>If neither a branch or tag is available for the event type, the variable will not exist. | `feat/feature-branch-1` |
| `GITHUB_REF_NAME_SLUG` | The slug of the branch or tag *name* that triggered the workflow. <br>If neither a branch or tag is available for the event type, the variable will not exist. | `feat-feature-branch-1` |
| `GITHUB_HEAD_REF_SLUG` | The slug of the head branch *name*. <br>Only set for event type *pull request* or forked repositories. | `feat-feature-branch-1` |
| `GITHUB_BASE_REF_SLUG` | The slug of the base branch *name*. <br>Only set for event type *pull request* or forked repositories. | `main` |
| `GITHUB_SHA_SHORT` | The shortened commit SHA (8 characters) that triggered the workflow. | `ffac537e` |
| Environment Variable Name | Description | Example value |
|--------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------|
| `CI_REPOSITORY_SLUG` | The slug of the owner and repository name (i.e. slug of `FranzDiebold/github-env-vars-action`). | `franzdiebold-github-env-vars-action` |
| `CI_REPOSITORY_OWNER` | The owner of the repository. | `FranzDiebold` |
| `CI_REPOSITORY_OWNER_SLUG` | The slug of the owner of the repository. | `franzdiebold` |
| `CI_REPOSITORY_NAME` | The name of the repository. | `github-env-vars-action` |
| `CI_REPOSITORY_NAME_SLUG` | The slug of the name of the repository. | `github-env-vars-action` |
| `CI_REPOSITORY` | The owner and repository name. Copy of `GITHUB_REPOSITORY` - for reasons of completeness. | `FranzDiebold/github-env-vars-action` |
| `CI_REF_SLUG` | The slug of the branch or tag *ref* that triggered the workflow (i.e. slug of `refs/heads/feat/feature-branch-1`). <br>If neither a branch or tag is available for the event type, the variable will not exist. | `refs-heads-feat-feature-branch-1` |
| `CI_ACTION_REF_NAME` | The branch or tag *name* that triggered the workflow. For pull requests it is the *head* branch name. | `feat/feature-branch-1` |
| `CI_ACTION_REF_NAME_SLUG` | The slug of the branch or tag *name* that triggered the workflow. For pull requests it is the slug of the *head* branch name. | `feat-feature-branch-1` |
| `CI_REF_NAME` | The branch or tag *name* that triggered the workflow. <br>If neither a branch or tag is available for the event type, the variable will not exist. | `feat/feature-branch-1` |
| `CI_REF_NAME_SLUG` | The slug of the branch or tag *name* that triggered the workflow. <br>If neither a branch or tag is available for the event type, the variable will not exist. | `feat-feature-branch-1` |
| `CI_REF` | The branch or tag ref that triggered the workflow. <br>If neither a branch or tag is available for the event type, the variable will not exist. Copy of `GITHUB_REF` - for reasons of completeness. | `refs/heads/feat/feature-branch-1` |
| `CI_HEAD_REF_SLUG` | The slug of the head branch *name*. <br>Only set for event type *pull request* or forked repositories. | `feat-feature-branch-1` |
| `CI_HEAD_REF` | Only set for forked repositories / pull request. The branch of the head repository / the head branch name. Copy of `GITHUB_HEAD_REF` - for reasons of completeness. | `feat/feature-branch-1` |
| `CI_BASE_REF_SLUG` | The slug of the base branch *name*. <br>Only set for event type *pull request* or forked repositories. | `main` |
| `CI_BASE_REF` | Only set for forked repositories / pull request. The branch of the base repository / the base branch name. Copy of `GITHUB_BASE_REF` - for reasons of completeness. | `main` |
| `CI_SHA_SHORT` | The shortened commit SHA (8 characters) that triggered the workflow. | `ffac537e` |
| `CI_SHA` | The commit SHA that triggered the workflow. Copy of `GITHUB_SHA` - for reasons of completeness. | `ffac537e6cbbf934b08745a378932722df287a53` |
| `CI_ACTOR` | The name of the person or app that initiated the workflow. Copy of `GITHUB_ACTOR` - for reasons of completeness. | `octocat` |
> The [slugified](https://en.wikipedia.org/wiki/Clean_URL#Slug) values are designed to be used in a URL.
@ -45,22 +51,28 @@ For a full list of default environment variables exposed by GitHub see [https://
```yaml
steps:
- uses: FranzDiebold/github-env-vars-action@v1.3.0
- uses: FranzDiebold/github-env-vars-action@v2
- name: Print environment variables
run: |
echo "GITHUB_REPOSITORY_SLUG=$GITHUB_REPOSITORY_SLUG"
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"
echo "GITHUB_REPOSITORY_OWNER_SLUG=$GITHUB_REPOSITORY_OWNER_SLUG"
echo "GITHUB_REPOSITORY_NAME=$GITHUB_REPOSITORY_NAME"
echo "GITHUB_REPOSITORY_NAME_SLUG=$GITHUB_REPOSITORY_NAME_SLUG"
echo "GITHUB_REF_SLUG=$GITHUB_REF_SLUG"
echo "GITHUB_BRANCH_NAME=$GITHUB_BRANCH_NAME"
echo "GITHUB_BRANCH_NAME_SLUG=$GITHUB_BRANCH_NAME_SLUG"
echo "GITHUB_REF_NAME=$GITHUB_REF_NAME"
echo "GITHUB_REF_NAME_SLUG=$GITHUB_REF_NAME_SLUG"
echo "GITHUB_HEAD_REF_SLUG=$GITHUB_HEAD_REF_SLUG"
echo "GITHUB_BASE_REF_SLUG=$GITHUB_BASE_REF_SLUG"
echo "GITHUB_SHA_SHORT=$GITHUB_SHA_SHORT"
echo "CI_REPOSITORY_SLUG=$CI_REPOSITORY_SLUG"
echo "CI_REPOSITORY_OWNER=$CI_REPOSITORY_OWNER"
echo "CI_REPOSITORY_OWNER_SLUG=$CI_REPOSITORY_OWNER_SLUG"
echo "CI_REPOSITORY_NAME=$CI_REPOSITORY_NAME"
echo "CI_REPOSITORY_NAME_SLUG=$CI_REPOSITORY_NAME_SLUG"
echo "CI_REPOSITORY=$CI_REPOSITORY"
echo "CI_REF_SLUG=$CI_REF_SLUG"
echo "CI_ACTION_REF_NAME=$CI_ACTION_REF_NAME"
echo "CI_ACTION_REF_NAME_SLUG=$CI_ACTION_REF_NAME_SLUG"
echo "CI_REF_NAME=$CI_REF_NAME"
echo "CI_REF_NAME_SLUG=$CI_REF_NAME_SLUG"
echo "CI_REF=$CI_REF"
echo "CI_HEAD_REF_SLUG=$CI_HEAD_REF_SLUG"
echo "CI_HEAD_REF=$CI_HEAD_REF"
echo "CI_BASE_REF_SLUG=$CI_BASE_REF_SLUG"
echo "CI_BASE_REF=$CI_BASE_REF"
echo "CI_SHA_SHORT=$CI_SHA_SHORT"
echo "CI_SHA=$CI_SHA"
echo "CI_ACTOR=$CI_ACTOR"
```
### Demo

116
dist/index.js vendored
View file

@ -59,119 +59,139 @@ function getShaShort(fullSha) {
return fullSha ? fullSha.substring(0, 8) : null;
}
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables
// https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables#default-environment-variables
try {
// i.e. FranzDiebold/github-env-vars-action
repository = process.env.GITHUB_REPOSITORY;
if (repository) {
core.exportVariable('GITHUB_REPOSITORY_SLUG', slugify(repository));
core.info(`Set GITHUB_REPOSITORY_SLUG=` +
`${process.env.GITHUB_REPOSITORY_SLUG}`);
core.exportVariable('CI_REPOSITORY_SLUG', slugify(repository));
core.info(`Set CI_REPOSITORY_SLUG=` +
`${process.env.CI_REPOSITORY_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_SLUG".');
'Cannot set "CI_REPOSITORY_SLUG".');
}
repositoryOwner = getRepositoryOwner(repository);
if (repositoryOwner) {
core.exportVariable('GITHUB_REPOSITORY_OWNER', repositoryOwner);
core.info(`Set GITHUB_REPOSITORY_OWNER=` +
`${process.env.GITHUB_REPOSITORY_OWNER}`);
core.exportVariable('CI_REPOSITORY_OWNER', repositoryOwner);
core.info(`Set CI_REPOSITORY_OWNER=` +
`${process.env.CI_REPOSITORY_OWNER}`);
core.exportVariable('GITHUB_REPOSITORY_OWNER_SLUG',
core.exportVariable('CI_REPOSITORY_OWNER_SLUG',
slugify(repositoryOwner));
core.info(`Set GITHUB_REPOSITORY_OWNER_SLUG=` +
`${process.env.GITHUB_REPOSITORY_OWNER_SLUG}`);
core.info(`Set CI_REPOSITORY_OWNER_SLUG=` +
`${process.env.CI_REPOSITORY_OWNER_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_OWNER" and ' +
'"GITHUB_REPOSITORY_OWNER_SLUG".');
'Cannot set "CI_REPOSITORY_OWNER" and ' +
'"CI_REPOSITORY_OWNER_SLUG".');
}
repositoryName = getRepositoryName(repository);
if (repositoryName) {
core.exportVariable('GITHUB_REPOSITORY_NAME', repositoryName);
core.info(`Set GITHUB_REPOSITORY_NAME=` +
`${process.env.GITHUB_REPOSITORY_NAME}`);
core.exportVariable('CI_REPOSITORY_NAME', repositoryName);
core.info(`Set CI_REPOSITORY_NAME=` +
`${process.env.CI_REPOSITORY_NAME}`);
core.exportVariable('GITHUB_REPOSITORY_NAME_SLUG',
core.exportVariable('CI_REPOSITORY_NAME_SLUG',
slugify(repositoryName));
core.info(`Set GITHUB_REPOSITORY_NAME_SLUG=` +
`${process.env.GITHUB_REPOSITORY_NAME_SLUG}`);
core.info(`Set CI_REPOSITORY_NAME_SLUG=` +
`${process.env.CI_REPOSITORY_NAME_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_NAME" and ' +
'"GITHUB_REPOSITORY_NAME_SLUG".');
'Cannot set "CI_REPOSITORY_NAME" and ' +
'"CI_REPOSITORY_NAME_SLUG".');
}
core.exportVariable('CI_REPOSITORY', repository);
core.info(`Set CI_REPOSITORY=${process.env.CI_REPOSITORY}`);
// i.e. refs/heads/feat/feature-branch-1
ref = process.env.GITHUB_REF;
if (ref) {
core.exportVariable('GITHUB_REF_SLUG', slugify(ref));
core.info(`Set GITHUB_REF_SLUG=${process.env.GITHUB_REF_SLUG}`);
core.exportVariable('CI_REF_SLUG', slugify(ref));
core.info(`Set CI_REF_SLUG=${process.env.CI_REF_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REF" not set. ' +
'Cannot set "GITHUB_REF_SLUG".');
'Cannot set "CI_REF_SLUG".');
}
refName = getRefName(ref);
if (refName) {
core.exportVariable('GITHUB_REF_NAME', refName);
core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`);
core.exportVariable('CI_REF_NAME', refName);
core.info(`Set CI_REF_NAME=${process.env.CI_REF_NAME}`);
core.exportVariable('GITHUB_REF_NAME_SLUG', slugify(refName));
core.info(`Set GITHUB_REF_NAME_SLUG=${process.env.GITHUB_REF_NAME_SLUG}`);
core.exportVariable('CI_REF_NAME_SLUG', slugify(refName));
core.info(`Set CI_REF_NAME_SLUG=${process.env.CI_REF_NAME_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REF" not set. ' +
'Cannot set "GITHUB_REF_NAME" and ' +
'"GITHUB_REF_NAME_SLUG".');
'Cannot set "CI_REF_NAME" and ' +
'"CI_REF_NAME_SLUG".');
}
core.exportVariable('CI_REF', ref);
core.info(`Set CI_REF=${process.env.CI_REF}`);
headRef = process.env.GITHUB_HEAD_REF;
branchName = headRef || refName;
if (branchName) {
core.exportVariable('GITHUB_BRANCH_NAME', branchName);
core.info(`Set GITHUB_BRANCH_NAME=${process.env.GITHUB_BRANCH_NAME}`);
core.exportVariable('CI_ACTION_REF_NAME', branchName);
core.info(`Set CI_ACTION_REF_NAME=${process.env.CI_ACTION_REF_NAME}`);
core.exportVariable('GITHUB_BRANCH_NAME_SLUG', slugify(branchName));
core.info('Set GITHUB_BRANCH_NAME_SLUG=' +
`${process.env.GITHUB_BRANCH_NAME_SLUG}`);
core.exportVariable('CI_ACTION_REF_NAME_SLUG', slugify(branchName));
core.info('Set CI_ACTION_REF_NAME_SLUG=' +
`${process.env.CI_ACTION_REF_NAME_SLUG}`);
} else {
core.warning('Environment variables "GITHUB_REF" and ' +
'"GITHUB_HEAD_REF" not set. ' +
'Cannot set "GITHUB_BRANCH_NAME" and ' +
'"GITHUB_BRANCH_NAME_SLUG".');
'Cannot set "CI_ACTION_REF_NAME" and ' +
'"CI_ACTION_REF_NAME_SLUG".');
}
if (headRef) {
core.exportVariable('GITHUB_HEAD_REF_SLUG', slugify(headRef));
core.info(`Set GITHUB_HEAD_REF_SLUG=${process.env.GITHUB_HEAD_REF_SLUG}`);
core.exportVariable('CI_HEAD_REF_SLUG', slugify(headRef));
core.info(`Set CI_HEAD_REF_SLUG=${process.env.CI_HEAD_REF_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_HEAD_REF" not set. ' +
'Cannot set "GITHUB_HEAD_REF_SLUG".');
'Cannot set "CI_HEAD_REF_SLUG".');
}
core.exportVariable('CI_HEAD_REF', headRef);
core.info(`Set CI_HEAD_REF=${process.env.CI_HEAD_REF}`);
baseRef = process.env.GITHUB_BASE_REF;
if (baseRef) {
core.exportVariable('GITHUB_BASE_REF_SLUG', slugify(baseRef));
core.info(`Set GITHUB_BASE_REF_SLUG=${process.env.GITHUB_BASE_REF_SLUG}`);
core.exportVariable('CI_BASE_REF_SLUG', slugify(baseRef));
core.info(`Set CI_BASE_REF_SLUG=${process.env.CI_BASE_REF_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_BASE_REF" not set. ' +
'Cannot set "GITHUB_BASE_REF_SLUG".');
'Cannot set "CI_BASE_REF_SLUG".');
}
core.exportVariable('CI_BASE_REF', baseRef);
core.info(`Set CI_BASE_REF=${process.env.CI_BASE_REF}`);
// i.e. ffac537e6cbbf934b08745a378932722df287a53
shaShort = getShaShort(process.env.GITHUB_SHA);
if (shaShort) {
core.exportVariable('GITHUB_SHA_SHORT', shaShort);
core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`);
sha = process.env.GITHUB_SHA;
if (sha) {
core.exportVariable('CI_SHA_SHORT', getShaShort(sha));
core.info(`Set CI_SHA_SHORT=${process.env.CI_SHA_SHORT}`);
} else {
core.warning('Environment variable "GITHUB_SHA" not set. ' +
'Cannot set "GITHUB_SHA_SHORT".');
'Cannot set "CI_SHA_SHORT".');
}
core.exportVariable('CI_SHA', sha);
core.info(`Set CI_SHA=${process.env.CI_SHA}`);
actor = process.env.GITHUB_ACTOR;
core.exportVariable('CI_ACTOR', actor);
core.info(`Set CI_ACTOR=${process.env.CI_ACTOR}`);
} catch (error) {
core.setFailed(error.message);
}

116
index.js
View file

@ -52,119 +52,139 @@ function getShaShort(fullSha) {
return fullSha ? fullSha.substring(0, 8) : null;
}
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables
// https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables#default-environment-variables
try {
// i.e. FranzDiebold/github-env-vars-action
repository = process.env.GITHUB_REPOSITORY;
if (repository) {
core.exportVariable('GITHUB_REPOSITORY_SLUG', slugify(repository));
core.info(`Set GITHUB_REPOSITORY_SLUG=` +
`${process.env.GITHUB_REPOSITORY_SLUG}`);
core.exportVariable('CI_REPOSITORY_SLUG', slugify(repository));
core.info(`Set CI_REPOSITORY_SLUG=` +
`${process.env.CI_REPOSITORY_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_SLUG".');
'Cannot set "CI_REPOSITORY_SLUG".');
}
repositoryOwner = getRepositoryOwner(repository);
if (repositoryOwner) {
core.exportVariable('GITHUB_REPOSITORY_OWNER', repositoryOwner);
core.info(`Set GITHUB_REPOSITORY_OWNER=` +
`${process.env.GITHUB_REPOSITORY_OWNER}`);
core.exportVariable('CI_REPOSITORY_OWNER', repositoryOwner);
core.info(`Set CI_REPOSITORY_OWNER=` +
`${process.env.CI_REPOSITORY_OWNER}`);
core.exportVariable('GITHUB_REPOSITORY_OWNER_SLUG',
core.exportVariable('CI_REPOSITORY_OWNER_SLUG',
slugify(repositoryOwner));
core.info(`Set GITHUB_REPOSITORY_OWNER_SLUG=` +
`${process.env.GITHUB_REPOSITORY_OWNER_SLUG}`);
core.info(`Set CI_REPOSITORY_OWNER_SLUG=` +
`${process.env.CI_REPOSITORY_OWNER_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_OWNER" and ' +
'"GITHUB_REPOSITORY_OWNER_SLUG".');
'Cannot set "CI_REPOSITORY_OWNER" and ' +
'"CI_REPOSITORY_OWNER_SLUG".');
}
repositoryName = getRepositoryName(repository);
if (repositoryName) {
core.exportVariable('GITHUB_REPOSITORY_NAME', repositoryName);
core.info(`Set GITHUB_REPOSITORY_NAME=` +
`${process.env.GITHUB_REPOSITORY_NAME}`);
core.exportVariable('CI_REPOSITORY_NAME', repositoryName);
core.info(`Set CI_REPOSITORY_NAME=` +
`${process.env.CI_REPOSITORY_NAME}`);
core.exportVariable('GITHUB_REPOSITORY_NAME_SLUG',
core.exportVariable('CI_REPOSITORY_NAME_SLUG',
slugify(repositoryName));
core.info(`Set GITHUB_REPOSITORY_NAME_SLUG=` +
`${process.env.GITHUB_REPOSITORY_NAME_SLUG}`);
core.info(`Set CI_REPOSITORY_NAME_SLUG=` +
`${process.env.CI_REPOSITORY_NAME_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_NAME" and ' +
'"GITHUB_REPOSITORY_NAME_SLUG".');
'Cannot set "CI_REPOSITORY_NAME" and ' +
'"CI_REPOSITORY_NAME_SLUG".');
}
core.exportVariable('CI_REPOSITORY', repository);
core.info(`Set CI_REPOSITORY=${process.env.CI_REPOSITORY}`);
// i.e. refs/heads/feat/feature-branch-1
ref = process.env.GITHUB_REF;
if (ref) {
core.exportVariable('GITHUB_REF_SLUG', slugify(ref));
core.info(`Set GITHUB_REF_SLUG=${process.env.GITHUB_REF_SLUG}`);
core.exportVariable('CI_REF_SLUG', slugify(ref));
core.info(`Set CI_REF_SLUG=${process.env.CI_REF_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REF" not set. ' +
'Cannot set "GITHUB_REF_SLUG".');
'Cannot set "CI_REF_SLUG".');
}
refName = getRefName(ref);
if (refName) {
core.exportVariable('GITHUB_REF_NAME', refName);
core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`);
core.exportVariable('CI_REF_NAME', refName);
core.info(`Set CI_REF_NAME=${process.env.CI_REF_NAME}`);
core.exportVariable('GITHUB_REF_NAME_SLUG', slugify(refName));
core.info(`Set GITHUB_REF_NAME_SLUG=${process.env.GITHUB_REF_NAME_SLUG}`);
core.exportVariable('CI_REF_NAME_SLUG', slugify(refName));
core.info(`Set CI_REF_NAME_SLUG=${process.env.CI_REF_NAME_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REF" not set. ' +
'Cannot set "GITHUB_REF_NAME" and ' +
'"GITHUB_REF_NAME_SLUG".');
'Cannot set "CI_REF_NAME" and ' +
'"CI_REF_NAME_SLUG".');
}
core.exportVariable('CI_REF', ref);
core.info(`Set CI_REF=${process.env.CI_REF}`);
headRef = process.env.GITHUB_HEAD_REF;
branchName = headRef || refName;
if (branchName) {
core.exportVariable('GITHUB_BRANCH_NAME', branchName);
core.info(`Set GITHUB_BRANCH_NAME=${process.env.GITHUB_BRANCH_NAME}`);
core.exportVariable('CI_ACTION_REF_NAME', branchName);
core.info(`Set CI_ACTION_REF_NAME=${process.env.CI_ACTION_REF_NAME}`);
core.exportVariable('GITHUB_BRANCH_NAME_SLUG', slugify(branchName));
core.info('Set GITHUB_BRANCH_NAME_SLUG=' +
`${process.env.GITHUB_BRANCH_NAME_SLUG}`);
core.exportVariable('CI_ACTION_REF_NAME_SLUG', slugify(branchName));
core.info('Set CI_ACTION_REF_NAME_SLUG=' +
`${process.env.CI_ACTION_REF_NAME_SLUG}`);
} else {
core.warning('Environment variables "GITHUB_REF" and ' +
'"GITHUB_HEAD_REF" not set. ' +
'Cannot set "GITHUB_BRANCH_NAME" and ' +
'"GITHUB_BRANCH_NAME_SLUG".');
'Cannot set "CI_ACTION_REF_NAME" and ' +
'"CI_ACTION_REF_NAME_SLUG".');
}
if (headRef) {
core.exportVariable('GITHUB_HEAD_REF_SLUG', slugify(headRef));
core.info(`Set GITHUB_HEAD_REF_SLUG=${process.env.GITHUB_HEAD_REF_SLUG}`);
core.exportVariable('CI_HEAD_REF_SLUG', slugify(headRef));
core.info(`Set CI_HEAD_REF_SLUG=${process.env.CI_HEAD_REF_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_HEAD_REF" not set. ' +
'Cannot set "GITHUB_HEAD_REF_SLUG".');
'Cannot set "CI_HEAD_REF_SLUG".');
}
core.exportVariable('CI_HEAD_REF', headRef);
core.info(`Set CI_HEAD_REF=${process.env.CI_HEAD_REF}`);
baseRef = process.env.GITHUB_BASE_REF;
if (baseRef) {
core.exportVariable('GITHUB_BASE_REF_SLUG', slugify(baseRef));
core.info(`Set GITHUB_BASE_REF_SLUG=${process.env.GITHUB_BASE_REF_SLUG}`);
core.exportVariable('CI_BASE_REF_SLUG', slugify(baseRef));
core.info(`Set CI_BASE_REF_SLUG=${process.env.CI_BASE_REF_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_BASE_REF" not set. ' +
'Cannot set "GITHUB_BASE_REF_SLUG".');
'Cannot set "CI_BASE_REF_SLUG".');
}
core.exportVariable('CI_BASE_REF', baseRef);
core.info(`Set CI_BASE_REF=${process.env.CI_BASE_REF}`);
// i.e. ffac537e6cbbf934b08745a378932722df287a53
shaShort = getShaShort(process.env.GITHUB_SHA);
if (shaShort) {
core.exportVariable('GITHUB_SHA_SHORT', shaShort);
core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`);
sha = process.env.GITHUB_SHA;
if (sha) {
core.exportVariable('CI_SHA_SHORT', getShaShort(sha));
core.info(`Set CI_SHA_SHORT=${process.env.CI_SHA_SHORT}`);
} else {
core.warning('Environment variable "GITHUB_SHA" not set. ' +
'Cannot set "GITHUB_SHA_SHORT".');
'Cannot set "CI_SHA_SHORT".');
}
core.exportVariable('CI_SHA', sha);
core.info(`Set CI_SHA=${process.env.CI_SHA}`);
actor = process.env.GITHUB_ACTOR;
core.exportVariable('CI_ACTOR', actor);
core.info(`Set CI_ACTOR=${process.env.CI_ACTOR}`);
} catch (error) {
core.setFailed(error.message);
}

View file

@ -19,30 +19,35 @@ test('gets repository owner', () => {
.toEqual('FranzDiebold');
});
test('gets repository owner for empty GITHUB_REPOSITORY', () => {
test('gets repository owner for empty repository', () => {
expect(getRepositoryOwner(undefined)).toBeFalsy();
});
test('gets repository name', () => {
test('gets repository name from repository', () => {
expect(getRepositoryName('FranzDiebold/github-env-vars-action'))
.toEqual('github-env-vars-action');
});
test('gets repository name for empty GITHUB_REPOSITORY', () => {
test('gets repository name for empty repository', () => {
expect(getRepositoryName(undefined)).toBeFalsy();
});
test('gets ref from simple ref name', () => {
test('gets ref name from simple ref', () => {
expect(getRefName('refs/heads/feature-branch-1'))
.toEqual('feature-branch-1');
});
test('gets ref from complex ref name', () => {
test('gets ref name from tag', () => {
expect(getRefName('refs/tags/v1.3.7'))
.toEqual('v1.3.7');
});
test('gets ref name from complex ref', () => {
expect(getRefName('refs/heads/feat/feature-branch-1'))
.toEqual('feat/feature-branch-1');
});
test('gets repository name for empty GITHUB_REF_NAME', () => {
test('gets ref name for empty ref', () => {
expect(getRefName(undefined)).toBeFalsy();
});
@ -51,6 +56,6 @@ test('gets short SHA', () => {
.toEqual('ffac537e');
});
test('gets short SHA for empty GITHUB_SHA', () => {
test('gets short SHA for empty SHA', () => {
expect(getShaShort(undefined)).toBeFalsy();
});

2
package-lock.json generated
View file

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

View file

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