Home > Software design >  syntax to include Jenkins pipeline optional argument
syntax to include Jenkins pipeline optional argument

Time:10-05

I currently have a sample pipeline that works but in the Jenkins output does not return the confirmation that the script worked.


pipeline {
    agent any
     environment {
        CRED_APP_CATALOG =  credentials('id-app-tenant')
    }
    stages {
       
        stage('ConnectToApp'){
            
            steps {
                script{
                    pwsh '''
                        $cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $env:CRED_APP_CATALOG_USR , $(convertto-securestring $env:CRED_APP_CATALOG_PSW -asplaintext -force)
                        Connect-PnPOnline -Url  $env:app_catalog_path -Credentials $cred 
                        Add-PnPApp -Path "./sharepoint/solution/samplesolution.sppkg" -Publish -Overwrite
                    '''

                }
               
            }
        }
       

    }
}

I know I need to add the returnStdout from pwsh script some where, but I cant seem to find the syntax any where.

CodePudding user response:

This is the syntax to use:

pwsh returnStdout: true, script: '''
    $cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $env:CRED_APP_CATALOG_USR , $(convertto-securestring $env:CRED_APP_CATALOG_PSW -asplaintext -force)
    Connect-PnPOnline -Url  $env:app_catalog_path -Credentials $cred 
    Add-PnPApp -Path "./sharepoint/solution/samplesolution.sppkg" -Publish -Overwrite
'''

Alternatively you may use parentheses:

pwsh( returnStdout: true, script: '''
    $cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $env:CRED_APP_CATALOG_USR , $(convertto-securestring $env:CRED_APP_CATALOG_PSW -asplaintext -force)
    Connect-PnPOnline -Url  $env:app_catalog_path -Credentials $cred 
    Add-PnPApp -Path "./sharepoint/solution/samplesolution.sppkg" -Publish -Overwrite
''')

With both syntaxes, you may store the output in a variable for further processing:

def stdout = pwsh returnStdout: true, script: '''
    $cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $env:CRED_APP_CATALOG_USR , $(convertto-securestring $env:CRED_APP_CATALOG_PSW -asplaintext -force)
    Connect-PnPOnline -Url  $env:app_catalog_path -Credentials $cred 
    Add-PnPApp -Path "./sharepoint/solution/samplesolution.sppkg" -Publish -Overwrite
'''

echo stdout
  • Related