diff --git a/src/job.ts b/src/job.ts index 899bc707b..a5a0ad7a0 100644 --- a/src/job.ts +++ b/src/job.ts @@ -170,8 +170,8 @@ export class Job { predefinedVariables["CI_JOB_NAME_SLUG"] = `${this.name.replace(/[^a-z\d]+/ig, "-").replace(/^-/, "").slice(0, 63).replace(/-$/, "").toLowerCase()}`; predefinedVariables["CI_JOB_STAGE"] = `${this.stage}`; predefinedVariables["CI_PROJECT_DIR"] = ciProjectDir; - predefinedVariables["CI_JOB_URL"] = `https://${gitData.remote.host}/${gitData.remote.group}/${gitData.remote.project}/-/jobs/${this.jobId}`; // Changes on rerun. - predefinedVariables["CI_PIPELINE_URL"] = `https://${gitData.remote.host}/${gitData.remote.group}/${gitData.remote.project}/pipelines/${this.pipelineIid}`; + predefinedVariables["CI_JOB_URL"] = `${predefinedVariables["CI_SERVER_URL"]}/${gitData.remote.group}/${gitData.remote.project}/-/jobs/${this.jobId}`; // Changes on rerun. + predefinedVariables["CI_PIPELINE_URL"] = `${predefinedVariables["CI_SERVER_URL"]}/${gitData.remote.group}/${gitData.remote.project}/pipelines/${this.pipelineIid}`; 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 ?? ""; diff --git a/src/parser-includes.ts b/src/parser-includes.ts index f43f41579..bf818daa7 100644 --- a/src/parser-includes.ts +++ b/src/parser-includes.ts @@ -105,12 +105,12 @@ export class ParserIncludes { includeDatas = includeDatas.concat(await this.init(fileDoc, opts)); } } else if (value["component"]) { - const {domain, projectPath, componentName, ref} = this.parseIncludeComponent(value["component"]); + const {domain, port, projectPath, componentName, ref} = this.parseIncludeComponent(value["component"]); // converts component to project const files = [`${componentName}.yml`, `${componentName}/template.yml`, null]; for (const f of files) { - assert(f !== null, `This GitLab CI configuration is invalid: component: \`${value["component"]}\`. One of the file [${files}] must exists in \`${domain}/${projectPath}\``); + assert(f !== null, `This GitLab CI configuration is invalid: component: \`${value["component"]}\`. One of the files [${files}] must exist in \`${domain}:${port}/${projectPath}\``); const isLocalComponent = projectPath === `${gitData.remote.group}/${gitData.remote.project}` && ref === gitData.commit.SHA; if (isLocalComponent) { @@ -199,15 +199,16 @@ export class ParserIncludes { }; } - static parseIncludeComponent (component: string): {domain: string; projectPath: string; componentName: string; ref: string} { + static parseIncludeComponent (component: string): {domain: string; port: string; projectPath: string; componentName: string; ref: string} { assert(!component.includes("://"), `This GitLab CI configuration is invalid: component: \`${component}\` should not contain protocol`); // eslint-disable-next-line no-useless-escape - const pattern = /(?[^/\s]+)\/(?.+)\/(?[^@]+)@(?.+)/; // regexr.com/7v7hm + const pattern = /(?[^/:\s]+)(:(?[0-9]+))?\/(?.+)\/(?[^@]+)@(?.+)/; // https://regexr.com/86q5d const gitRemoteMatch = pattern.exec(component); if (gitRemoteMatch?.groups == null) throw new Error(`This is a bug, please create a github issue if this is something you're expecting to work. input: ${component}`); return { domain: gitRemoteMatch.groups["domain"], + port: gitRemoteMatch.groups["port"], projectPath: gitRemoteMatch.groups["projectPath"], componentName: `templates/${gitRemoteMatch.groups["componentName"]}`, ref: gitRemoteMatch.groups["ref"], @@ -239,7 +240,7 @@ export class ParserIncludes { await Utils.bash(` cd ${cwd}/${stateDir} \\ && git clone --branch "${ref}" -n --depth=1 --filter=tree:0 \\ - ${remote.schema}://${remote.host}/${project}.git \\ + ${remote.schema}://${remote.host}:${remote.port}/${project}.git \\ ${cwd}/${target}.${ext} \\ && cd ${cwd}/${target}.${ext} \\ && git sparse-checkout set --no-cone ${normalizedFile} \\ diff --git a/src/predefined-variables.ts b/src/predefined-variables.ts index 0668b0900..a1501a845 100644 --- a/src/predefined-variables.ts +++ b/src/predefined-variables.ts @@ -13,16 +13,29 @@ export function init ({gitData, argv, envMatchedVariables}: PredefinedVariablesO // 1. gitlab variables files // 2. values derieved implicitly from `git remote -v` // 3. default value + const CI_SERVER_PROTOCOL = envMatchedVariables["CI_SERVER_PROTOCOL"] + ?? ((gitData.remote.schema === "http" || gitData.remote.schema === "https") + ? gitData.remote.schema + : "https"); const CI_SERVER_PORT = envMatchedVariables["CI_SERVER_PORT"] - ?? (gitData.remote.schema === "http" || gitData.remote.schema === "https") + ?? ((gitData.remote.schema === "http" || gitData.remote.schema === "https") ? gitData.remote.port - : "443"; + : "443"); const CI_SERVER_SHELL_SSH_PORT = envMatchedVariables["CI_SERVER_SHELL_SSH_PORT"] - ?? (gitData.remote.schema === "ssh") + ?? ((gitData.remote.schema === "ssh") ? gitData.remote.port - : "22"; + : "22"); + const CI_SERVER_HOST = envMatchedVariables["CI_SERVER_HOST"] + ?? `${gitData.remote.host}`; + const CI_SERVER_FQDN = envMatchedVariables["CI_SERVER_FQDN"] + ?? (CI_SERVER_PORT == "443" + ? gitData.remote.host + : `${gitData.remote.host}:${CI_SERVER_PORT}`); + const CI_SERVER_URL = envMatchedVariables["CI_SERVER_URL"] + ?? `${CI_SERVER_PROTOCOL}://${CI_SERVER_FQDN}`; + const CI_PROJECT_ROOT_NAMESPACE = gitData.remote.group.split("/")[0]; + const CI_PROJECT_NAMESPACE = gitData.remote.group; - const CI_SERVER_URL = `https://${gitData.remote.host}:${CI_SERVER_PORT}`; const predefinedVariables: {[key: string]: string} = { CI: "true", GITLAB_USER_LOGIN: gitData.user["GITLAB_USER_LOGIN"], @@ -35,7 +48,8 @@ export function init ({gitData, argv, envMatchedVariables}: PredefinedVariablesO CI_PROJECT_TITLE: `${camelCase(gitData.remote.project)}`, CI_PROJECT_PATH: `${gitData.remote.group}/${gitData.remote.project}`, CI_PROJECT_PATH_SLUG: `${gitData.remote.group.replace(/\//g, "-")}-${gitData.remote.project}`.toLowerCase(), - CI_PROJECT_NAMESPACE: `${gitData.remote.group}`, + CI_PROJECT_ROOT_NAMESPACE: CI_PROJECT_ROOT_NAMESPACE, + CI_PROJECT_NAMESPACE: CI_PROJECT_NAMESPACE, CI_PROJECT_VISIBILITY: "internal", CI_PROJECT_ID: "1217", CI_COMMIT_REF_PROTECTED: "false", @@ -50,12 +64,12 @@ export function init ({gitData, argv, envMatchedVariables}: PredefinedVariablesO CI_COMMIT_DESCRIPTION: "More commit text", CI_DEFAULT_BRANCH: gitData.branches.default, CI_PIPELINE_SOURCE: "push", - CI_SERVER_FQDN: `${gitData.remote.host}:${CI_SERVER_PORT}`, - CI_SERVER_HOST: `${gitData.remote.host}`, + CI_SERVER_FQDN: CI_SERVER_FQDN, + CI_SERVER_HOST: CI_SERVER_HOST, CI_SERVER_PORT: CI_SERVER_PORT, CI_SERVER_SHELL_SSH_PORT: CI_SERVER_SHELL_SSH_PORT, CI_SERVER_URL: CI_SERVER_URL, - CI_SERVER_PROTOCOL: "https", + CI_SERVER_PROTOCOL: CI_SERVER_PROTOCOL, CI_API_V4_URL: `${CI_SERVER_URL}/api/v4`, CI_PROJECT_URL: `${CI_SERVER_URL}/${gitData.remote.group}/${gitData.remote.project}`, CI_TEMPLATE_REGISTRY_HOST: "registry.gitlab.com", diff --git a/src/utils.ts b/src/utils.ts index 19b6ad5fc..afb49dcf1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -345,7 +345,7 @@ ${evalStr} case "http": case "https": { try { - const {status} = await axios.get(`${protocol}://${domain}/${projectPath}/-/raw/${ref}/${file}`); + const {status} = await axios.get(`${protocol}://${domain}:${port}/${projectPath}/-/raw/${ref}/${file}`); return (status === 200); } catch (e) { return false;