Rename environment variables to follow the naming conventions from GitHub.

This commit is contained in:
Franz Diebold 2020-12-01 12:41:42 +01:00
parent ca2b126210
commit 5bb07c02c1
4 changed files with 238 additions and 168 deletions

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);
}