Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Dejulia489 committed Nov 10, 2020
2 parents e42066b + 9f565f4 commit 02e18de
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 6 deletions.
12 changes: 6 additions & 6 deletions AzurePipelinesPS/AzurePipelinesPS.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ FunctionsToExport = 'Add-APDeploymentGroup', 'Add-APGroupMembership', 'Add-APLog
'New-APPipeline', 'New-APPolicyConfiguration', 'New-APProject',
'New-APRelease', 'New-APRepository', 'New-APServiceEndpoint',
'New-APSession', 'New-APTeam', 'Publish-APBuildDefinition',
'Publish-APReleaseDefinition', 'Register-APPSRepository',
'Remove-APAgent', 'Remove-APBuild', 'Remove-APBuildDefinition',
'Remove-APBuildLease', 'Remove-APDashboard',
'Remove-APDeploymentGroup', 'Remove-APEnvironment', 'Remove-APFeed',
'Remove-APGitFile', 'Remove-APGroupMembership',
'Remove-APInstalledExtensionDocument',
'Publish-APPipelineDefinition', 'Publish-APReleaseDefinition',
'Register-APPSRepository', 'Remove-APAgent', 'Remove-APBuild',
'Remove-APBuildDefinition', 'Remove-APBuildLease',
'Remove-APDashboard', 'Remove-APDeploymentGroup',
'Remove-APEnvironment', 'Remove-APFeed', 'Remove-APGitFile',
'Remove-APGroupMembership', 'Remove-APInstalledExtensionDocument',
'Remove-APNotificationSubscription', 'Remove-APNugetPackageVersion',
'Remove-APNugetPackageVersionFromRecycleBin', 'Remove-APPlan',
'Remove-APPolicyConfiguration', 'Remove-APPool', 'Remove-APProject',
Expand Down
199 changes: 199 additions & 0 deletions AzurePipelinesPS/Public/Publish-APPipelineDefinition.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
function Publish-APPipelineDefinition
{
<#
.SYNOPSIS
Creates an Azure Pipelines release definition.
.DESCRIPTION
Creates an Azure Pipelines release definition by using a template.
The template can be retrieved by using Get-APPipelineDefinition.
.PARAMETER Instance
The Team Services account or TFS server.
.PARAMETER Collection
For Azure DevOps the value for collection should be the name of your orginization.
For both Team Services and TFS The value should be DefaultCollection unless another collection has been created.
.PARAMETER Project
Project ID or project name.
.PARAMETER ApiVersion
Version of the api to use.
.PARAMETER PersonalAccessToken
Personal access token used to authenticate that has been converted to a secure string.
It is recomended to uses an Azure Pipelines PS session to pass the personal access token parameter among funcitons, See New-APSession.
https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=vsts
.PARAMETER Credential
Specifies a user account that has permission to send the request.
.PARAMETER Proxy
Use a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server.
.PARAMETER ProxyCredential
Specifie a user account that has permission to use the proxy server that is specified by the -Proxy parameter. The default is the current user.
.PARAMETER Session
Azure DevOps PS session, created by New-APSession.
.PARAMETER Template
The template provided by Get-APPipelineDefinition.
.INPUTS
None, does not support pipeline.
.OUTPUTS
PSobject, Azure Pipelines build.
.EXAMPLE
Creates AP release definition from the $template provided. $template is a JSON representation of the definition that can be found in the history of the release deinifiton during editing.
Publish-APPipelineDefinition -Instance 'https://dev.azure.com' -Collection 'myCollection' -Project 'myFirstProject' -DefinitionObject $template
.LINK
https://docs.microsoft.com/en-us/rest/api/azure/devops/release/definitions/create?view=azure-devops-rest-5.0
#>
[CmdletBinding(DefaultParameterSetName = 'ByPersonalAccessToken')]
Param
(
[Parameter(Mandatory,
ParameterSetName = 'ByPersonalAccessToken')]
[Parameter(Mandatory,
ParameterSetName = 'ByCredential')]
[uri]
$Instance,

[Parameter(Mandatory,
ParameterSetName = 'ByPersonalAccessToken')]
[Parameter(Mandatory,
ParameterSetName = 'ByCredential')]
[string]
$Collection,

[Parameter(Mandatory,
ParameterSetName = 'ByPersonalAccessToken')]
[Parameter(Mandatory,
ParameterSetName = 'ByCredential')]
[string]
$Project,

[Parameter(Mandatory,
ParameterSetName = 'ByPersonalAccessToken')]
[Parameter(Mandatory,
ParameterSetName = 'ByCredential')]
[string]
$ApiVersion,

[Parameter(ParameterSetName = 'ByPersonalAccessToken')]
[Security.SecureString]
$PersonalAccessToken,

[Parameter(ParameterSetName = 'ByCredential')]
[pscredential]
$Credential,

[Parameter(ParameterSetName = 'ByPersonalAccessToken')]
[Parameter(ParameterSetName = 'ByCredential')]
[string]
$Proxy,

[Parameter(ParameterSetName = 'ByPersonalAccessToken')]
[Parameter(ParameterSetName = 'ByCredential')]
[pscredential]
$ProxyCredential,

[Parameter(Mandatory,
ParameterSetName = 'BySession')]
[object]
$Session,

[Parameter(Mandatory)]
[PSobject]
$Template
)

begin
{
If ($PSCmdlet.ParameterSetName -eq 'BySession')
{
$currentSession = $Session | Get-APSession
If ($currentSession)
{
$Instance = $currentSession.Instance
$Collection = $currentSession.Collection
$Project = $currentSession.Project
$PersonalAccessToken = $currentSession.PersonalAccessToken
$Credential = $currentSession.Credential
$Proxy = $currentSession.Proxy
$ProxyCredential = $currentSession.ProxyCredential
If ($currentSession.Version)
{
$ApiVersion = (Get-APApiVersion -Version $currentSession.Version)
}
else
{
$ApiVersion = $currentSession.ApiVersion
}
}
}
}

process
{
$body = $Template
$apiEndpoint = Get-APApiEndpoint -ApiType 'pipelines'
$setAPUriSplat = @{
Collection = $Collection
Instance = $Instance
Project = $Project
ApiVersion = $ApiVersion
ApiEndpoint = $apiEndpoint
}
[uri] $uri = Set-APUri @setAPUriSplat
$invokeAPRestMethodSplat = @{
ContentType = 'application/json'
Body = $body
Method = 'POST'
Uri = $uri
Credential = $Credential
PersonalAccessToken = $PersonalAccessToken
Proxy = $Proxy
ProxyCredential = $ProxyCredential
}
$results = Invoke-APRestMethod @invokeAPRestMethodSplat
If ($results.count -eq 0)
{
Return
}
ElseIf ($results.value)
{
Return $results.value
}
Else
{
Return $results
}
}

end
{
}
}

0 comments on commit 02e18de

Please sign in to comment.