From f18cf0a3092ba4011fa9317af909521dd2a1de68 Mon Sep 17 00:00:00 2001 From: Jacopo Date: Thu, 19 May 2022 16:45:01 +0200 Subject: [PATCH 1/3] added template parameters --- README.md | 1 + action.yml | 7 +- lib/main.js | 74 +++---- lib/pipeline.error.js | 20 +- lib/pipeline.runner.js | 386 ++++++++++++++++++------------------ lib/task.parameters.js | 78 ++++---- lib/util/logger.js | 74 +++---- lib/util/pipeline.helper.js | 184 ++++++++--------- lib/util/url.parser.js | 120 +++++------ src/pipeline.runner.ts | 9 +- src/task.parameters.ts | 8 +- 11 files changed, 492 insertions(+), 469 deletions(-) diff --git a/README.md b/README.md index 999a200d..19d4ce73 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Action takes Project URL, pipeline name and a [Personal Access Token (PAT)](http azure-pipeline-name: 'pipeline-name' # name of the Azure pipeline to be triggered azure-devops-token: '${{ secrets.AZURE_DEVOPS_TOKEN }}' azure-pipeline-variables: '{"variable1": "value1", "variable2": "value2"}' # optional stringified json + azure-template-parameters: '{"variable1": "value1", "variable2": "value2"}' # optional stringified json ``` # Contributing diff --git a/action.yml b/action.yml index 090a3e54..e026c50d 100644 --- a/action.yml +++ b/action.yml @@ -10,9 +10,12 @@ inputs: azure-pipeline-variables: description: 'Set/Overwrite pipeline variables' required: false - azure-devops-token: + azure-template-parameters: + description: 'Set pipeline template parameters' + required: false + azure-devops-token: description: 'Paste personal access token of the user as value of secret variable:AZURE_DEVOPS_TOKEN' required: true runs: using: 'node12' - main: 'lib/main.js' \ No newline at end of file + main: 'lib/main.js' diff --git a/lib/main.js b/lib/main.js index 64c7313f..45dc7615 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,37 +1,37 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; - result["default"] = mod; - return result; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const core = __importStar(require("@actions/core")); -const task_parameters_1 = require("./task.parameters"); -const pipeline_runner_1 = require("./pipeline.runner"); -function main() { - return __awaiter(this, void 0, void 0, function* () { - try { - const pipelineRunner = new pipeline_runner_1.PipelineRunner(task_parameters_1.TaskParameters.getTaskParams()); - core.debug("Starting pipeline runner"); - yield pipelineRunner.start(); - core.debug("pipeline runner completed"); - } - catch (error) { - const errorMessage = JSON.stringify(error); - core.setFailed(`Error: "${error.message}" Details: "${errorMessage}"`); - } - }); -} -exports.main = main; -main(); +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const core = __importStar(require("@actions/core")); +const task_parameters_1 = require("./task.parameters"); +const pipeline_runner_1 = require("./pipeline.runner"); +function main() { + return __awaiter(this, void 0, void 0, function* () { + try { + const pipelineRunner = new pipeline_runner_1.PipelineRunner(task_parameters_1.TaskParameters.getTaskParams()); + core.debug("Starting pipeline runner"); + yield pipelineRunner.start(); + core.debug("pipeline runner completed"); + } + catch (error) { + const errorMessage = JSON.stringify(error); + core.setFailed(`Error: "${error.message}" Details: "${errorMessage}"`); + } + }); +} +exports.main = main; +main(); diff --git a/lib/pipeline.error.js b/lib/pipeline.error.js index 25935ff3..86fb406e 100644 --- a/lib/pipeline.error.js +++ b/lib/pipeline.error.js @@ -1,10 +1,10 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -class PipelineNotFoundError extends Error { - constructor(m) { - super(m); - // Required to allow use of "instanceof" - Object.setPrototypeOf(this, PipelineNotFoundError.prototype); - } -} -exports.PipelineNotFoundError = PipelineNotFoundError; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +class PipelineNotFoundError extends Error { + constructor(m) { + super(m); + // Required to allow use of "instanceof" + Object.setPrototypeOf(this, PipelineNotFoundError.prototype); + } +} +exports.PipelineNotFoundError = PipelineNotFoundError; diff --git a/lib/pipeline.runner.js b/lib/pipeline.runner.js index d0e59cc6..d2cf6417 100644 --- a/lib/pipeline.runner.js +++ b/lib/pipeline.runner.js @@ -1,191 +1,195 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; - result["default"] = mod; - return result; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const core = __importStar(require("@actions/core")); -const azdev = __importStar(require("azure-devops-node-api")); -const task_parameters_1 = require("./task.parameters"); -const pipeline_error_1 = require("./pipeline.error"); -const ReleaseInterfaces = __importStar(require("azure-devops-node-api/interfaces/ReleaseInterfaces")); -const BuildInterfaces = __importStar(require("azure-devops-node-api/interfaces/BuildInterfaces")); -const pipeline_helper_1 = require("./util/pipeline.helper"); -const logger_1 = require("./util/logger"); -const url_parser_1 = require("./util/url.parser"); -class PipelineRunner { - constructor(taskParameters) { - this.repository = pipeline_helper_1.PipelineHelper.processEnv("GITHUB_REPOSITORY"); - this.branch = pipeline_helper_1.PipelineHelper.processEnv("GITHUB_REF"); - this.commitId = pipeline_helper_1.PipelineHelper.processEnv("GITHUB_SHA"); - this.githubRepo = "GitHub"; - this.taskParameters = taskParameters; - } - start() { - return __awaiter(this, void 0, void 0, function* () { - try { - var taskParams = task_parameters_1.TaskParameters.getTaskParams(); - let authHandler = azdev.getPersonalAccessTokenHandler(taskParams.azureDevopsToken); - let collectionUrl = url_parser_1.UrlParser.GetCollectionUrlBase(this.taskParameters.azureDevopsProjectUrl); - core.info(`Creating connection with Azure DevOps service : "${collectionUrl}"`); - let webApi = new azdev.WebApi(collectionUrl, authHandler); - core.info("Connection created"); - let pipelineName = this.taskParameters.azurePipelineName; - try { - core.debug(`Triggering Yaml pipeline : "${pipelineName}"`); - yield this.RunYamlPipeline(webApi); - } - catch (error) { - if (error instanceof pipeline_error_1.PipelineNotFoundError) { - core.debug(`Triggering Designer pipeline : "${pipelineName}"`); - yield this.RunDesignerPipeline(webApi); - } - else { - throw error; - } - } - } - catch (error) { - let errorMessage = `${error.message}`; - core.setFailed(errorMessage); - } - }); - } - RunYamlPipeline(webApi) { - return __awaiter(this, void 0, void 0, function* () { - let projectName = url_parser_1.UrlParser.GetProjectName(this.taskParameters.azureDevopsProjectUrl); - let pipelineName = this.taskParameters.azurePipelineName; - let buildApi = yield webApi.getBuildApi(); - // Get matching build definitions for the given project and pipeline name - const buildDefinitions = yield buildApi.getDefinitions(projectName, pipelineName); - pipeline_helper_1.PipelineHelper.EnsureValidPipeline(projectName, pipelineName, buildDefinitions); - // Extract Id from build definition - let buildDefinitionReference = buildDefinitions[0]; - let buildDefinitionId = buildDefinitionReference.id; - // Get build definition for the matching definition Id - let buildDefinition = yield buildApi.getDefinition(projectName, buildDefinitionId); - logger_1.Logger.LogPipelineObject(buildDefinition); - // Fetch repository details from build definition - let repositoryId = buildDefinition.repository.id.trim(); - let repositoryType = buildDefinition.repository.type.trim(); - let sourceBranch = null; - let sourceVersion = null; - // If definition is linked to existing github repo, pass github source branch and source version to build - if (pipeline_helper_1.PipelineHelper.equals(repositoryId, this.repository) && pipeline_helper_1.PipelineHelper.equals(repositoryType, this.githubRepo)) { - core.debug("pipeline is linked to same Github repo"); - sourceBranch = this.branch, - sourceVersion = this.commitId; - } - else { - core.debug("pipeline is not linked to same Github repo"); - } - let build = { - definition: { - id: buildDefinition.id - }, - project: { - id: buildDefinition.project.id - }, - sourceBranch: sourceBranch, - sourceVersion: sourceVersion, - reason: BuildInterfaces.BuildReason.Triggered, - parameters: this.taskParameters.azurePipelineVariables - }; - logger_1.Logger.LogPipelineTriggerInput(build); - // Queue build - let buildQueueResult = yield buildApi.queueBuild(build, build.project.id, true); - if (buildQueueResult != null) { - logger_1.Logger.LogPipelineTriggerOutput(buildQueueResult); - // If build result contains validation errors set result to FAILED - if (buildQueueResult.validationResults != null && buildQueueResult.validationResults.length > 0) { - let errorAndWarningMessage = pipeline_helper_1.PipelineHelper.getErrorAndWarningMessageFromBuildResult(buildQueueResult.validationResults); - core.setFailed("Errors: " + errorAndWarningMessage.errorMessage + " Warnings: " + errorAndWarningMessage.warningMessage); - } - else { - logger_1.Logger.LogPipelineTriggered(pipelineName, projectName); - if (buildQueueResult._links != null) { - logger_1.Logger.LogOutputUrl(buildQueueResult._links.web.href); - } - } - } - }); - } - RunDesignerPipeline(webApi) { - return __awaiter(this, void 0, void 0, function* () { - let projectName = url_parser_1.UrlParser.GetProjectName(this.taskParameters.azureDevopsProjectUrl); - let pipelineName = this.taskParameters.azurePipelineName; - let releaseApi = yield webApi.getReleaseApi(); - // Get release definitions for the given project name and pipeline name - const releaseDefinitions = yield releaseApi.getReleaseDefinitions(projectName, pipelineName, ReleaseInterfaces.ReleaseDefinitionExpands.Artifacts); - pipeline_helper_1.PipelineHelper.EnsureValidPipeline(projectName, pipelineName, releaseDefinitions); - let releaseDefinition = releaseDefinitions[0]; - logger_1.Logger.LogPipelineObject(releaseDefinition); - // Create ConfigurationVariableValue objects from the input variables - let variables = undefined; - if (this.taskParameters.azurePipelineVariables) { - variables = JSON.parse(this.taskParameters.azurePipelineVariables); - Object.keys(variables).map(function (key, index) { - let oldValue = variables[key]; - variables[key] = { value: oldValue }; - }); - } - // Filter Github artifacts from release definition - let gitHubArtifacts = releaseDefinition.artifacts.filter(pipeline_helper_1.PipelineHelper.isGitHubArtifact); - let artifacts = new Array(); - if (gitHubArtifacts == null || gitHubArtifacts.length == 0) { - core.debug("Pipeline is not linked to any GitHub artifact"); - // If no GitHub artifacts found it means pipeline is not linked to any GitHub artifact - } - else { - // If pipeline has any matching Github artifact - core.debug("Pipeline is linked to GitHub artifact. Looking for now matching repository"); - gitHubArtifacts.forEach(gitHubArtifact => { - if (gitHubArtifact.definitionReference != null && pipeline_helper_1.PipelineHelper.equals(gitHubArtifact.definitionReference.definition.name, this.repository)) { - // Add version information for matching GitHub artifact - let artifactMetadata = { - alias: gitHubArtifact.alias, - instanceReference: { - id: this.commitId, - sourceBranch: this.branch, - sourceRepositoryType: this.githubRepo, - sourceRepositoryId: this.repository, - sourceVersion: this.commitId - } - }; - core.debug("pipeline is linked to same Github repo"); - artifacts.push(artifactMetadata); - } - }); - } - let releaseStartMetadata = { - definitionId: releaseDefinition.id, - reason: ReleaseInterfaces.ReleaseReason.ContinuousIntegration, - artifacts: artifacts, - variables: variables - }; - logger_1.Logger.LogPipelineTriggerInput(releaseStartMetadata); - // create release - let release = yield releaseApi.createRelease(releaseStartMetadata, projectName); - if (release != null) { - logger_1.Logger.LogPipelineTriggered(pipelineName, projectName); - logger_1.Logger.LogPipelineTriggerOutput(release); - if (release != null && release._links != null) { - logger_1.Logger.LogOutputUrl(release._links.web.href); - } - } - }); - } -} -exports.PipelineRunner = PipelineRunner; +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const core = __importStar(require("@actions/core")); +const azdev = __importStar(require("azure-devops-node-api")); +const task_parameters_1 = require("./task.parameters"); +const pipeline_error_1 = require("./pipeline.error"); +const ReleaseInterfaces = __importStar(require("azure-devops-node-api/interfaces/ReleaseInterfaces")); +const BuildInterfaces = __importStar(require("azure-devops-node-api/interfaces/BuildInterfaces")); +const pipeline_helper_1 = require("./util/pipeline.helper"); +const logger_1 = require("./util/logger"); +const url_parser_1 = require("./util/url.parser"); +class PipelineRunner { + constructor(taskParameters) { + this.repository = pipeline_helper_1.PipelineHelper.processEnv("GITHUB_REPOSITORY"); + this.branch = pipeline_helper_1.PipelineHelper.processEnv("GITHUB_REF"); + this.commitId = pipeline_helper_1.PipelineHelper.processEnv("GITHUB_SHA"); + this.githubRepo = "GitHub"; + this.taskParameters = taskParameters; + } + start() { + return __awaiter(this, void 0, void 0, function* () { + try { + var taskParams = task_parameters_1.TaskParameters.getTaskParams(); + let authHandler = azdev.getPersonalAccessTokenHandler(taskParams.azureDevopsToken); + let collectionUrl = url_parser_1.UrlParser.GetCollectionUrlBase(this.taskParameters.azureDevopsProjectUrl); + core.info(`Creating connection with Azure DevOps service : "${collectionUrl}"`); + let webApi = new azdev.WebApi(collectionUrl, authHandler); + core.info("Connection created"); + let pipelineName = this.taskParameters.azurePipelineName; + try { + core.debug(`Triggering Yaml pipeline : "${pipelineName}"`); + yield this.RunYamlPipeline(webApi); + } + catch (error) { + if (error instanceof pipeline_error_1.PipelineNotFoundError) { + core.debug(`Triggering Designer pipeline : "${pipelineName}"`); + yield this.RunDesignerPipeline(webApi); + } + else { + throw error; + } + } + } + catch (error) { + let errorMessage = `${error.message}`; + core.setFailed(errorMessage); + } + }); + } + RunYamlPipeline(webApi) { + return __awaiter(this, void 0, void 0, function* () { + let projectName = url_parser_1.UrlParser.GetProjectName(this.taskParameters.azureDevopsProjectUrl); + let pipelineName = this.taskParameters.azurePipelineName; + let buildApi = yield webApi.getBuildApi(); + // Get matching build definitions for the given project and pipeline name + const buildDefinitions = yield buildApi.getDefinitions(projectName, pipelineName); + pipeline_helper_1.PipelineHelper.EnsureValidPipeline(projectName, pipelineName, buildDefinitions); + // Extract Id from build definition + let buildDefinitionReference = buildDefinitions[0]; + let buildDefinitionId = buildDefinitionReference.id; + // Get build definition for the matching definition Id + let buildDefinition = yield buildApi.getDefinition(projectName, buildDefinitionId); + logger_1.Logger.LogPipelineObject(buildDefinition); + // Fetch repository details from build definition + let repositoryId = buildDefinition.repository.id.trim(); + let repositoryType = buildDefinition.repository.type.trim(); + let sourceBranch = null; + let sourceVersion = null; + // If definition is linked to existing github repo, pass github source branch and source version to build + if (pipeline_helper_1.PipelineHelper.equals(repositoryId, this.repository) && pipeline_helper_1.PipelineHelper.equals(repositoryType, this.githubRepo)) { + core.debug("pipeline is linked to same Github repo"); + sourceBranch = this.branch, + sourceVersion = this.commitId; + } + else { + core.debug("pipeline is not linked to same Github repo"); + } + let templateParameters = this.taskParameters.azureTemplateParameters ? + JSON.parse(this.taskParameters.azureTemplateParameters) + : undefined; + let build = { + definition: { + id: buildDefinition.id + }, + project: { + id: buildDefinition.project.id + }, + sourceBranch: sourceBranch, + sourceVersion: sourceVersion, + reason: BuildInterfaces.BuildReason.Triggered, + parameters: this.taskParameters.azurePipelineVariables, + templateParameters: templateParameters + }; + logger_1.Logger.LogPipelineTriggerInput(build); + // Queue build + let buildQueueResult = yield buildApi.queueBuild(build, build.project.id, true); + if (buildQueueResult != null) { + logger_1.Logger.LogPipelineTriggerOutput(buildQueueResult); + // If build result contains validation errors set result to FAILED + if (buildQueueResult.validationResults != null && buildQueueResult.validationResults.length > 0) { + let errorAndWarningMessage = pipeline_helper_1.PipelineHelper.getErrorAndWarningMessageFromBuildResult(buildQueueResult.validationResults); + core.setFailed("Errors: " + errorAndWarningMessage.errorMessage + " Warnings: " + errorAndWarningMessage.warningMessage); + } + else { + logger_1.Logger.LogPipelineTriggered(pipelineName, projectName); + if (buildQueueResult._links != null) { + logger_1.Logger.LogOutputUrl(buildQueueResult._links.web.href); + } + } + } + }); + } + RunDesignerPipeline(webApi) { + return __awaiter(this, void 0, void 0, function* () { + let projectName = url_parser_1.UrlParser.GetProjectName(this.taskParameters.azureDevopsProjectUrl); + let pipelineName = this.taskParameters.azurePipelineName; + let releaseApi = yield webApi.getReleaseApi(); + // Get release definitions for the given project name and pipeline name + const releaseDefinitions = yield releaseApi.getReleaseDefinitions(projectName, pipelineName, ReleaseInterfaces.ReleaseDefinitionExpands.Artifacts); + pipeline_helper_1.PipelineHelper.EnsureValidPipeline(projectName, pipelineName, releaseDefinitions); + let releaseDefinition = releaseDefinitions[0]; + logger_1.Logger.LogPipelineObject(releaseDefinition); + // Create ConfigurationVariableValue objects from the input variables + let variables = undefined; + if (this.taskParameters.azurePipelineVariables) { + variables = JSON.parse(this.taskParameters.azurePipelineVariables); + Object.keys(variables).map(function (key, index) { + let oldValue = variables[key]; + variables[key] = { value: oldValue }; + }); + } + // Filter Github artifacts from release definition + let gitHubArtifacts = releaseDefinition.artifacts.filter(pipeline_helper_1.PipelineHelper.isGitHubArtifact); + let artifacts = new Array(); + if (gitHubArtifacts == null || gitHubArtifacts.length == 0) { + core.debug("Pipeline is not linked to any GitHub artifact"); + // If no GitHub artifacts found it means pipeline is not linked to any GitHub artifact + } + else { + // If pipeline has any matching Github artifact + core.debug("Pipeline is linked to GitHub artifact. Looking for now matching repository"); + gitHubArtifacts.forEach(gitHubArtifact => { + if (gitHubArtifact.definitionReference != null && pipeline_helper_1.PipelineHelper.equals(gitHubArtifact.definitionReference.definition.name, this.repository)) { + // Add version information for matching GitHub artifact + let artifactMetadata = { + alias: gitHubArtifact.alias, + instanceReference: { + id: this.commitId, + sourceBranch: this.branch, + sourceRepositoryType: this.githubRepo, + sourceRepositoryId: this.repository, + sourceVersion: this.commitId + } + }; + core.debug("pipeline is linked to same Github repo"); + artifacts.push(artifactMetadata); + } + }); + } + let releaseStartMetadata = { + definitionId: releaseDefinition.id, + reason: ReleaseInterfaces.ReleaseReason.ContinuousIntegration, + artifacts: artifacts, + variables: variables + }; + logger_1.Logger.LogPipelineTriggerInput(releaseStartMetadata); + // create release + let release = yield releaseApi.createRelease(releaseStartMetadata, projectName); + if (release != null) { + logger_1.Logger.LogPipelineTriggered(pipelineName, projectName); + logger_1.Logger.LogPipelineTriggerOutput(release); + if (release != null && release._links != null) { + logger_1.Logger.LogOutputUrl(release._links.web.href); + } + } + }); + } +} +exports.PipelineRunner = PipelineRunner; diff --git a/lib/task.parameters.js b/lib/task.parameters.js index 56ab17bc..e0b33009 100644 --- a/lib/task.parameters.js +++ b/lib/task.parameters.js @@ -1,37 +1,41 @@ -"use strict"; -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; - result["default"] = mod; - return result; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const core = __importStar(require("@actions/core")); -class TaskParameters { - constructor() { - this._azureDevopsProjectUrl = core.getInput('azure-devops-project-url', { required: true }); - this._azurePipelineName = core.getInput('azure-pipeline-name', { required: true }); - this._azureDevopsToken = core.getInput('azure-devops-token', { required: true }); - this._azurePipelineVariables = core.getInput('azure-pipeline-variables', { required: false }); - } - static getTaskParams() { - if (!this.taskparams) { - this.taskparams = new TaskParameters(); - } - return this.taskparams; - } - get azureDevopsProjectUrl() { - return this._azureDevopsProjectUrl; - } - get azurePipelineName() { - return this._azurePipelineName; - } - get azureDevopsToken() { - return this._azureDevopsToken; - } - get azurePipelineVariables() { - return this._azurePipelineVariables; - } -} -exports.TaskParameters = TaskParameters; +"use strict"; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const core = __importStar(require("@actions/core")); +class TaskParameters { + constructor() { + this._azureDevopsProjectUrl = core.getInput('azure-devops-project-url', { required: true }); + this._azurePipelineName = core.getInput('azure-pipeline-name', { required: true }); + this._azureDevopsToken = core.getInput('azure-devops-token', { required: true }); + this._azurePipelineVariables = core.getInput('azure-pipeline-variables', { required: false }); + this._azureTemplateParameters = core.getInput('azure-template-parameters', { required: false }); + } + static getTaskParams() { + if (!this.taskparams) { + this.taskparams = new TaskParameters(); + } + return this.taskparams; + } + get azureDevopsProjectUrl() { + return this._azureDevopsProjectUrl; + } + get azurePipelineName() { + return this._azurePipelineName; + } + get azureDevopsToken() { + return this._azureDevopsToken; + } + get azurePipelineVariables() { + return this._azurePipelineVariables; + } + get azureTemplateParameters() { + return this._azureTemplateParameters; + } +} +exports.TaskParameters = TaskParameters; diff --git a/lib/util/logger.js b/lib/util/logger.js index 4c9b611a..3d284ea0 100644 --- a/lib/util/logger.js +++ b/lib/util/logger.js @@ -1,37 +1,37 @@ -"use strict"; -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; - result["default"] = mod; - return result; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const core = __importStar(require("@actions/core")); -class Logger { - static LogOutputUrl(url) { - if (url) { - core.setOutput('pipeline-url', url); - core.info(`More details on triggered pipeline can be found here : "${url}"`); - } - } - static LogInfo(message) { - core.info(message); - } - static LogPipelineTriggered(pipelineName, projectName) { - core.info(`\Pipeline '${pipelineName}' is triggered in project '${projectName}'`); - } - static LogPipelineObject(object) { - core.debug("Pipeline object : " + this.getPrintObject(object)); - } - static LogPipelineTriggerInput(input) { - core.debug("Input: " + this.getPrintObject(input)); - } - static LogPipelineTriggerOutput(output) { - core.debug("Output: " + this.getPrintObject(output)); - } - static getPrintObject(object) { - return JSON.stringify(object, null, 4); - } -} -exports.Logger = Logger; +"use strict"; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const core = __importStar(require("@actions/core")); +class Logger { + static LogOutputUrl(url) { + if (url) { + core.setOutput('pipeline-url', url); + core.info(`More details on triggered pipeline can be found here : "${url}"`); + } + } + static LogInfo(message) { + core.info(message); + } + static LogPipelineTriggered(pipelineName, projectName) { + core.info(`\Pipeline '${pipelineName}' is triggered in project '${projectName}'`); + } + static LogPipelineObject(object) { + core.debug("Pipeline object : " + this.getPrintObject(object)); + } + static LogPipelineTriggerInput(input) { + core.debug("Input: " + this.getPrintObject(input)); + } + static LogPipelineTriggerOutput(output) { + core.debug("Output: " + this.getPrintObject(output)); + } + static getPrintObject(object) { + return JSON.stringify(object, null, 4); + } +} +exports.Logger = Logger; diff --git a/lib/util/pipeline.helper.js b/lib/util/pipeline.helper.js index 78c77e6b..86ff73c6 100644 --- a/lib/util/pipeline.helper.js +++ b/lib/util/pipeline.helper.js @@ -1,92 +1,92 @@ -"use strict"; -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; - result["default"] = mod; - return result; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const BuildInterfaces = __importStar(require("azure-devops-node-api/interfaces/BuildInterfaces")); -const pipeline_error_1 = require("./../pipeline.error"); -class PipelineHelper { - static EnsureValidPipeline(projectName, pipelineName, pipelines) { - // If definition not found then Throw Error - if (pipelines == null || pipelines.length == 0) { - let errorMessage = `Pipeline named "${pipelineName}" not found in project "${projectName}"`; - throw new pipeline_error_1.PipelineNotFoundError(errorMessage); - } - if (pipelines.length > 1) { - // If more than 1 definition found, throw ERROR - let errorMessage = `More than 1 Pipeline named "${pipelineName}" found in project "${projectName}"`; - throw Error(errorMessage); - } - } - static equals(str1, str2) { - if (str1 === str2) { - return true; - } - if (str1 === null) { - return false; - } - if (str2 === null) { - return false; - } - return str1.trim().toUpperCase() === str2.trim().toUpperCase(); - } - static processEnv(envVarName) { - const variable = process.env[envVarName]; - if (!variable) { - throw new Error(`env.${envVarName} is not set`); - } - return variable; - } - ; - static isGitHubArtifact(arifact) { - if (arifact != null && arifact.type != null && arifact.type.toUpperCase() === "GITHUB") { - return true; - } - return false; - } - static getErrorAndWarningMessageFromBuildResult(validationResults) { - let errorMessage = ""; - let warningMessage = ""; - if (validationResults && validationResults.length > 0) { - let errors = validationResults.filter((result) => { - return result.result === BuildInterfaces.ValidationResult.Error; - }); - if (errors.length > 0) { - errorMessage = this._joinValidateResults(errors); - } - else { - warningMessage = this._joinValidateResults(validationResults); - } - } - // Taking into account server errors also which comes not in form of array, like no build queue permissions - else if (validationResults) { - errorMessage = this._getErrorMessageFromServer(validationResults); - } - return { - errorMessage: errorMessage, - warningMessage: warningMessage - }; - } - static _joinValidateResults(validateResults) { - let resultMessages = validateResults.map((validationResult) => { - return validationResult.message; - }); - resultMessages = resultMessages.filter((message) => !!message); - return resultMessages.join(","); - } - static _getErrorMessageFromServer(validationResult) { - let errorMessage = ""; - if (validationResult) { - errorMessage = validationResult.message || ""; - } - if (validationResult && validationResult.serverError && errorMessage.length === 0) { - errorMessage = validationResult.serverError.message || ""; - } - return errorMessage; - } -} -exports.PipelineHelper = PipelineHelper; +"use strict"; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const BuildInterfaces = __importStar(require("azure-devops-node-api/interfaces/BuildInterfaces")); +const pipeline_error_1 = require("./../pipeline.error"); +class PipelineHelper { + static EnsureValidPipeline(projectName, pipelineName, pipelines) { + // If definition not found then Throw Error + if (pipelines == null || pipelines.length == 0) { + let errorMessage = `Pipeline named "${pipelineName}" not found in project "${projectName}"`; + throw new pipeline_error_1.PipelineNotFoundError(errorMessage); + } + if (pipelines.length > 1) { + // If more than 1 definition found, throw ERROR + let errorMessage = `More than 1 Pipeline named "${pipelineName}" found in project "${projectName}"`; + throw Error(errorMessage); + } + } + static equals(str1, str2) { + if (str1 === str2) { + return true; + } + if (str1 === null) { + return false; + } + if (str2 === null) { + return false; + } + return str1.trim().toUpperCase() === str2.trim().toUpperCase(); + } + static processEnv(envVarName) { + const variable = process.env[envVarName]; + if (!variable) { + throw new Error(`env.${envVarName} is not set`); + } + return variable; + } + ; + static isGitHubArtifact(arifact) { + if (arifact != null && arifact.type != null && arifact.type.toUpperCase() === "GITHUB") { + return true; + } + return false; + } + static getErrorAndWarningMessageFromBuildResult(validationResults) { + let errorMessage = ""; + let warningMessage = ""; + if (validationResults && validationResults.length > 0) { + let errors = validationResults.filter((result) => { + return result.result === BuildInterfaces.ValidationResult.Error; + }); + if (errors.length > 0) { + errorMessage = this._joinValidateResults(errors); + } + else { + warningMessage = this._joinValidateResults(validationResults); + } + } + // Taking into account server errors also which comes not in form of array, like no build queue permissions + else if (validationResults) { + errorMessage = this._getErrorMessageFromServer(validationResults); + } + return { + errorMessage: errorMessage, + warningMessage: warningMessage + }; + } + static _joinValidateResults(validateResults) { + let resultMessages = validateResults.map((validationResult) => { + return validationResult.message; + }); + resultMessages = resultMessages.filter((message) => !!message); + return resultMessages.join(","); + } + static _getErrorMessageFromServer(validationResult) { + let errorMessage = ""; + if (validationResult) { + errorMessage = validationResult.message || ""; + } + if (validationResult && validationResult.serverError && errorMessage.length === 0) { + errorMessage = validationResult.serverError.message || ""; + } + return errorMessage; + } +} +exports.PipelineHelper = PipelineHelper; diff --git a/lib/util/url.parser.js b/lib/util/url.parser.js index 6688ca32..6f1d22e4 100644 --- a/lib/util/url.parser.js +++ b/lib/util/url.parser.js @@ -1,60 +1,60 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -class UrlParser { - static GetProjectName(projectUrl) { - if (this.IsNullOrEmpty(projectUrl)) { - throw new Error(this.NullOrEmptyProjectUrl); - } - try { - projectUrl = projectUrl.trim(); - this.EnsureProjectName(projectUrl); - var index = projectUrl.lastIndexOf("/"); - var projectNamePart = projectUrl.substr(index + 1); - var projectName = decodeURI(projectNamePart); - if (projectName) { - return projectName; - } - else { - throw Error(); - } - } - catch (error) { - var errorMessage = this.GetUrlParseExceptionMessage(projectUrl); - throw new Error(errorMessage); - } - } - static GetCollectionUrlBase(projectUrl) { - if (this.IsNullOrEmpty(projectUrl)) { - throw new Error(this.NullOrEmptyProjectUrl); - } - try { - projectUrl = projectUrl.trim(); - var collectionUrl = projectUrl.substr(0, projectUrl.lastIndexOf("/")); - if (collectionUrl) { - return collectionUrl; - } - else { - throw Error(); - } - } - catch (error) { - var errorMessage = this.GetUrlParseExceptionMessage(projectUrl); - throw new Error(errorMessage); - } - } - static EnsureProjectName(projectUrl) { - var index = projectUrl.lastIndexOf("/"); - if (index == (projectUrl.length - 1)) { - throw Error(); - } - } - static GetUrlParseExceptionMessage(projectUrl) { - let errorMessage = `Failed to parse project url: "${projectUrl}". Specify the valid project url (eg, https://dev.azure.com/organization/project-name or https://server.example.com:8080/tfs/DefaultCollection/project-name)) and try again.`; - return errorMessage; - } - static IsNullOrEmpty(value) { - return (!value); - } -} -exports.UrlParser = UrlParser; -UrlParser.NullOrEmptyProjectUrl = "Project url is null or empty. Specify the valid project url and try again"; +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +class UrlParser { + static GetProjectName(projectUrl) { + if (this.IsNullOrEmpty(projectUrl)) { + throw new Error(this.NullOrEmptyProjectUrl); + } + try { + projectUrl = projectUrl.trim(); + this.EnsureProjectName(projectUrl); + var index = projectUrl.lastIndexOf("/"); + var projectNamePart = projectUrl.substr(index + 1); + var projectName = decodeURI(projectNamePart); + if (projectName) { + return projectName; + } + else { + throw Error(); + } + } + catch (error) { + var errorMessage = this.GetUrlParseExceptionMessage(projectUrl); + throw new Error(errorMessage); + } + } + static GetCollectionUrlBase(projectUrl) { + if (this.IsNullOrEmpty(projectUrl)) { + throw new Error(this.NullOrEmptyProjectUrl); + } + try { + projectUrl = projectUrl.trim(); + var collectionUrl = projectUrl.substr(0, projectUrl.lastIndexOf("/")); + if (collectionUrl) { + return collectionUrl; + } + else { + throw Error(); + } + } + catch (error) { + var errorMessage = this.GetUrlParseExceptionMessage(projectUrl); + throw new Error(errorMessage); + } + } + static EnsureProjectName(projectUrl) { + var index = projectUrl.lastIndexOf("/"); + if (index == (projectUrl.length - 1)) { + throw Error(); + } + } + static GetUrlParseExceptionMessage(projectUrl) { + let errorMessage = `Failed to parse project url: "${projectUrl}". Specify the valid project url (eg, https://dev.azure.com/organization/project-name or https://server.example.com:8080/tfs/DefaultCollection/project-name)) and try again.`; + return errorMessage; + } + static IsNullOrEmpty(value) { + return (!value); + } +} +exports.UrlParser = UrlParser; +UrlParser.NullOrEmptyProjectUrl = "Project url is null or empty. Specify the valid project url and try again"; diff --git a/src/pipeline.runner.ts b/src/pipeline.runner.ts index 6030ce5e..ffd204b8 100644 --- a/src/pipeline.runner.ts +++ b/src/pipeline.runner.ts @@ -82,6 +82,10 @@ export class PipelineRunner { core.debug("pipeline is not linked to same Github repo"); } + let templateParameters = this.taskParameters.azureTemplateParameters ? + JSON.parse(this.taskParameters.azureTemplateParameters) + : undefined; + let build: BuildInterfaces.Build = { definition: { id: buildDefinition.id @@ -92,7 +96,8 @@ export class PipelineRunner { sourceBranch: sourceBranch, sourceVersion: sourceVersion, reason: BuildInterfaces.BuildReason.Triggered, - parameters: this.taskParameters.azurePipelineVariables + parameters: this.taskParameters.azurePipelineVariables, + templateParameters: templateParameters } as BuildInterfaces.Build; log.LogPipelineTriggerInput(build); @@ -185,4 +190,4 @@ export class PipelineRunner { } } } -} \ No newline at end of file +} diff --git a/src/task.parameters.ts b/src/task.parameters.ts index 8d485120..4e7fe9bf 100644 --- a/src/task.parameters.ts +++ b/src/task.parameters.ts @@ -6,12 +6,14 @@ export class TaskParameters { private _azurePipelineName: string; private _azureDevopsToken: string; private _azurePipelineVariables: string; + private _azureTemplateParameters: string; private constructor() { this._azureDevopsProjectUrl = core.getInput('azure-devops-project-url', { required: true }); this._azurePipelineName = core.getInput('azure-pipeline-name', { required: true }); this._azureDevopsToken = core.getInput('azure-devops-token', { required: true }); this._azurePipelineVariables = core.getInput('azure-pipeline-variables', { required: false }); + this._azureTemplateParameters = core.getInput('azure-template-parameters', { required: false }); } public static getTaskParams() { @@ -37,4 +39,8 @@ export class TaskParameters { public get azurePipelineVariables() { return this._azurePipelineVariables; } -} \ No newline at end of file + + public get azureTemplateParameters() { + return this._azureTemplateParameters; + } +} From e5338ef7bda1a47874a536e85c592d617b777a6d Mon Sep 17 00:00:00 2001 From: Jacopo Carlini Date: Fri, 29 Sep 2023 11:05:01 +0200 Subject: [PATCH 2/3] Update action.yml --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 63992aad..8f4bc291 100644 --- a/action.yml +++ b/action.yml @@ -1,4 +1,4 @@ -name: 'Azure Pipelines Action' +name: 'Azure Pipelines Action - JVersion' description: 'Trigger a run in Azure pipelines' inputs: azure-devops-project-url: From c335090be8b63430dcc69a5ef6fb24e014fa9286 Mon Sep 17 00:00:00 2001 From: Jacopo Date: Fri, 29 Sep 2023 11:06:47 +0200 Subject: [PATCH 3/3] rename action --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 8f4bc291..e9f3014e 100644 --- a/action.yml +++ b/action.yml @@ -1,4 +1,4 @@ -name: 'Azure Pipelines Action - JVersion' +name: 'Azure Pipelines Action' description: 'Trigger a run in Azure pipelines' inputs: azure-devops-project-url: