functions/Publish-PuppetModule.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function Publish-PuppetModule {
    <#
      .SYNOPSIS
        Build and Publish Puppet Module
      .DESCRIPTION
        Generate package for the module and publish to the forge.
      .PARAMETER PuppetModuleFolderPath
        The path, relative or absolute, to the Puppet module's root folder.
      .PARAMETER ForgeUploadUrl
        The URL for the Forge Upload API. Defaults to the public forge.
      .PARAMETER ForgeToken
        The Forge API Token for the target account.
      .PARAMETER Build
        Flag whether to build the package.
      .PARAMETER Publish
        Flag whether to publish the package.
      .EXAMPLE
        Publish-PuppetModule -PuppetModuleFolderPath C:\output\testmodule -ForgeUploadUrl https://forgeapi.puppetlabs.com/v3/releases -ForgeToken testmoduletoken -Build true -Publish true
        This command will create or use existing pkg and Publishes the <tarball> to the Forge , for the `testmodule` depends on the options passed for pdk release command.
    #>

    #>
    [CmdletBinding()]
    param (
      [Parameter(Mandatory=$True)]
      $PuppetModuleFolderPath,
      [string]$ForgeToken,
      [string]$ForgeUploadUrl,
      [switch]$Build,
      [switch]$Publish
    )

    begin {
      $PuppetModuleFolderPath = Resolve-Path -Path $PuppetModuleFolderPath -ErrorAction Stop
      If ($Publish) {
        If ([string]::IsNullOrEmpty($ForgeUploadUrl)) {
          $CommandPublish = "pdk release publish --forge-token $ForgeToken"
        } Else {
          $CommandPublish = "pdk release publish --forge-token $ForgeToken --forge-upload-url $ForgeUploadUrl"
        }
      }
      If ($Build) {
        $CommandBuild = 'pdk build'
      }
    }
    process {
      Try {
        $ErrorActionPreference = 'Stop'
        If ($Build)  {
          Invoke-PdkCommand -Path $PuppetModuleFolderPath -Command $CommandBuild -SuccessFilterScript {
            $_ -match "completed successfully"
          }
        }
        If ($Publish) {
          Invoke-PdkCommand -Path $PuppetModuleFolderPath -Command $CommandPublish -SuccessFilterScript {
            $_ -match "Publish to Forge was successful"
          }
        }
      } Catch {
        $PSCmdlet.ThrowTerminatingError($PSItem)
      }
    }
    end {}
  }