Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support additional CI variables #1274

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/git-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class GitData {
REF_NAME: "main",
SHA: "0000000000000000000000000000000000000000",
SHORT_SHA: "00000000",
TIMESTAMP: new Date().toISOString().split(".")[0] + "Z",
};

static async init (cwd: string, writeStreams: WriteStreams): Promise<GitData> {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ process.on("SIGUSR2", async () => await cleanupJobResources(jobs));
.parserConfiguration({"greedy-arrays": false})
.showHelpOnFail(false)
.version(packageJson["version"])
.wrap(yargs.terminalWidth())
.wrap(yargs.terminalWidth?.())
.command({
handler: async (argv) => {
try {
Expand Down
6 changes: 5 additions & 1 deletion src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class Job {
readonly name: string;
readonly baseName: string;
readonly dependencies: string[] | null;
readonly environment?: {name: string; url: string | null};
readonly environment?: {name: string; url: string | null; deployment_tier: string | null; action: string | null};
readonly jobId: number;
readonly rules?: {
if: string;
Expand Down Expand Up @@ -169,6 +169,9 @@ export class Job {
predefinedVariables["CI_ENVIRONMENT_NAME"] = this.environment?.name ?? "";
predefinedVariables["CI_ENVIRONMENT_SLUG"] = this.environment?.name?.replace(/[^a-z\d]+/ig, "-").replace(/^-/, "").slice(0, 23).replace(/-$/, "").toLowerCase() ?? "";
predefinedVariables["CI_ENVIRONMENT_URL"] = this.environment?.url ?? "";
predefinedVariables["CI_ENVIRONMENT_TIER"] = this.environment?.deployment_tier ?? "";
predefinedVariables["CI_ENVIRONMENT_ACTION"] = this.environment?.action ?? "";

if (opt.nodeIndex !== null) {
predefinedVariables["CI_NODE_INDEX"] = `${opt.nodeIndex}`;
}
Expand Down Expand Up @@ -428,6 +431,7 @@ export class Job {

const argv = this.argv;
this._startTime = process.hrtime();
this._variables["CI_JOB_STARTED_AT"] = new Date().toISOString().split(".")[0] + "Z";
const writeStreams = this.writeStreams;
this._dotenvVariables = await this.initProducerReportsDotenvVariables(writeStreams, Utils.expandVariables(this._variables));
const expanded = Utils.unscape$$Variables(Utils.expandVariables({...this._variables, ...this._dotenvVariables}));
Expand Down
4 changes: 4 additions & 0 deletions src/predefined-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type PredefinedVariablesOpts = {

export function init ({gitData, argv}: PredefinedVariablesOpts): {[name: string]: string} {
const predefinedVariables: {[key: string]: string} = {
CI: "true",
ANGkeith marked this conversation as resolved.
Show resolved Hide resolved
GITLAB_USER_LOGIN: gitData.user["GITLAB_USER_LOGIN"],
GITLAB_USER_EMAIL: gitData.user["GITLAB_USER_EMAIL"],
GITLAB_USER_NAME: gitData.user["GITLAB_USER_NAME"],
Expand All @@ -25,6 +26,9 @@ export function init ({gitData, argv}: PredefinedVariablesOpts): {[name: string]
CI_COMMIT_BRANCH: gitData.commit.REF_NAME, // Not available in merge request or tag pipelines
CI_COMMIT_REF_NAME: gitData.commit.REF_NAME, // Tag or branch name
CI_COMMIT_REF_SLUG: gitData.commit.REF_NAME.replace(/[^a-z\d]+/ig, "-").replace(/^-/, "").slice(0, 63).replace(/-$/, "").toLowerCase(),
CI_COMMIT_TIMESTAMP: gitData.commit.TIMESTAMP,
ANGkeith marked this conversation as resolved.
Show resolved Hide resolved
CI_PIPELINE_CREATED_AT: new Date().toISOString().split(".")[0] + "Z",
CI_JOB_STARTED_AT: new Date().toISOString().split(".")[0] + "Z",
CI_COMMIT_TITLE: "Commit Title", // First line of commit message.
CI_COMMIT_MESSAGE: "Commit Title\nMore commit text", // Full commit message
CI_COMMIT_DESCRIPTION: "More commit text",
Expand Down
4 changes: 4 additions & 0 deletions tests/test-cases/environment/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ deploy-stage-job:
- echo ${CI_ENVIRONMENT_SLUG}
- echo ${CI_ENVIRONMENT_NAME}
- echo ${CI_ENVIRONMENT_URL}
- echo ${CI_ENVIRONMENT_ACTION}
- echo ${CI_ENVIRONMENT_TIER}
environment:
name: Stage Domain
url: http://stage.domain.com
action: stop
deployment_tier: production
2 changes: 2 additions & 0 deletions tests/test-cases/environment/integration.environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ test("environment <deploy-stage-job>", async () => {
chalk`{blueBright deploy-stage-job} {greenBright >} stage-domain`,
chalk`{blueBright deploy-stage-job} {greenBright >} Stage Domain`,
chalk`{blueBright deploy-stage-job} {greenBright >} http://stage.domain.com`,
chalk`{blueBright deploy-stage-job} {greenBright >} stop`,
chalk`{blueBright deploy-stage-job} {greenBright >} production`,
chalk`{blueBright deploy-stage-job} environment: \{ name: {bold Stage Domain}, url: {bold http://stage.domain.com} \}`,
];
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
Expand Down