diff --git a/bin/helpers/helper.js b/bin/helpers/helper.js index d8853606..93933022 100644 --- a/bin/helpers/helper.js +++ b/bin/helpers/helper.js @@ -102,7 +102,7 @@ exports.getGitMetaData = () => { if(!info.commonGitDir) { logger.debug(`Unable to find a Git directory`); exports.debug(`Unable to find a Git directory`); - return resolve({}); + resolve({}); } if(!info.author && exports.findGitConfig(process.cwd())) { /* commit objects are packed */ @@ -196,16 +196,6 @@ exports.getHostInfo = () => { exports.getCiInfo = () => { var env = process.env; - // GitHub Actions — checked before the presence-only providers (Jenkins, - // Bitbucket) so leftover env vars on self-hosted runners can't shadow it - if (env.GITHUB_ACTIONS === "true" || (env.CI === "true" && env.GITHUB_RUN_ID)) { - return { - name: "GitHub Actions", - build_url: `${env.GITHUB_SERVER_URL || 'https://github.com'}/${env.GITHUB_REPOSITORY}/actions/runs/${env.GITHUB_RUN_ID}`, - job_name: env.GITHUB_WORKFLOW || env.GITHUB_JOB, - build_number: env.GITHUB_RUN_ID - }; - } // Jenkins if ((typeof env.JENKINS_URL === "string" && env.JENKINS_URL.length > 0) || (typeof env.JENKINS_HOME === "string" && env.JENKINS_HOME.length > 0)) { return { @@ -278,6 +268,15 @@ exports.getCiInfo = () => { build_number: env.CI_JOB_ID }; } + // GitHub Actions + if (env.GITHUB_ACTIONS === "true" || env.CI === "true" && env.GITHUB_RUN_ID) { + return { + name: "GitHub Actions", + build_url: `${env.GITHUB_SERVER_URL || 'https://github.com'}/${env.GITHUB_REPOSITORY}/actions/runs/${env.GITHUB_RUN_ID}`, + job_name: env.GITHUB_WORKFLOW || env.GITHUB_JOB, + build_number: env.GITHUB_RUN_ID + }; + } // Buildkite if (env.CI === "true" && env.BUILDKITE === "true") { return { diff --git a/test/unit/bin/helpers/helper.js b/test/unit/bin/helpers/helper.js index c86c4729..3a3c1de8 100644 --- a/test/unit/bin/helpers/helper.js +++ b/test/unit/bin/helpers/helper.js @@ -38,63 +38,4 @@ describe('helper', () => { expect(helper.getSizeOfJsonObjectInBytes(truncatedVCSInfo)).to.be.lessThanOrEqual(64 * 1024); }); }); - - describe('getCiInfo', () => { - const CI_VARS = ['CI', 'GITHUB_ACTIONS', 'GITHUB_SERVER_URL', 'GITHUB_REPOSITORY', 'GITHUB_RUN_ID', 'GITHUB_RUN_NUMBER', 'GITHUB_WORKFLOW', 'GITHUB_JOB', 'JENKINS_URL', 'JENKINS_HOME', 'BUILD_URL', 'JOB_NAME', 'BUILD_NUMBER']; - let savedEnv = {}; - - beforeEach(() => { - CI_VARS.forEach((k) => { - savedEnv[k] = process.env[k]; - delete process.env[k]; - }); - }); - - afterEach(() => { - CI_VARS.forEach((k) => { - if (savedEnv[k] === undefined) delete process.env[k]; - else process.env[k] = savedEnv[k]; - }); - savedEnv = {}; - }); - - const setGithubActionsEnv = () => { - process.env.CI = 'true'; - process.env.GITHUB_ACTIONS = 'true'; - process.env.GITHUB_SERVER_URL = 'https://github.com'; - process.env.GITHUB_REPOSITORY = 'org/repo'; - process.env.GITHUB_RUN_ID = '27412345678'; - process.env.GITHUB_WORKFLOW = 'CI Workflow'; - }; - - it('detects GitHub Actions with build_url and run-id build_number', () => { - setGithubActionsEnv(); - const ciInfo = helper.getCiInfo(); - expect(ciInfo.name).to.eq('GitHub Actions'); - expect(ciInfo.build_url).to.eq('https://github.com/org/repo/actions/runs/27412345678'); - expect(ciInfo.build_number).to.eq('27412345678'); - }); - - it('prefers GitHub Actions over leaked Jenkins env vars on self-hosted runners', () => { - setGithubActionsEnv(); - process.env.JENKINS_HOME = '/var/lib/jenkins'; - const ciInfo = helper.getCiInfo(); - expect(ciInfo.name).to.eq('GitHub Actions'); - expect(ciInfo.build_url).to.eq('https://github.com/org/repo/actions/runs/27412345678'); - }); - - it('detects Jenkins when no explicit CI marker is set', () => { - process.env.JENKINS_URL = 'https://ci.internal/job/x'; - process.env.BUILD_URL = 'https://ci.internal/job/x/42/'; - process.env.JOB_NAME = 'x'; - process.env.BUILD_NUMBER = '42'; - const ciInfo = helper.getCiInfo(); - expect(ciInfo.name).to.eq('Jenkins'); - expect(ciInfo.build_url).to.eq('https://ci.internal/job/x/42/'); - }); - - it('returns null when no CI is detected', () => { - expect(helper.getCiInfo()).to.be.null; - }); - }); });