Run playlists in Jenkins

This topic shows you how to set up your Jenkins pipeline to run Tosca Cloud playlists. You can use the CI/CD integration with Tosca Cloud playlists to automate the complete test run process.

Here are the steps to integrate Tosca Cloud into your Jenkins CI pipeline: 

  1. Authenticate your pipeline

  2. Request access token

  3. Trigger a test run

  4. Check the status of the test run

  5. Get results

If you want to skip the detailed setup steps, you can go to the full code example to configure everything at once.

Once everything is set up, your pipeline will automatically manage the process.

Before you start

Here's what you need to get started:

Authenticate your pipeline

In Jenkins, create a new pipeline job and define the required authorization values in the environment block. These values allow your pipeline to authenticate with Tosca Cloud.

This example shows an environment block with authorization values:

Copy
pipeline{
    agent any
        environment {
            CLIENT_ID = '0Bx5q308ttNLhiXbf413'
            CLIENT_SECRET = 'X438PC4VVXccvIm2HdQrZL4zffuWZciKrx5tRw4'
            E2G_SCOPE = 'tta'
            E2G_AUTH_URL = 'https://vehicle_insurance_company.com/oauth2/default/v3/token'
            TOSCA_CLOUD_URL = 'https://vehicle_insurance_company.com/785fb70b-1621-4a71-a68f-05de9e1b40a3'
            PLAYLIST_ID = '0aa88a1c-0bd1-4ee5-8ada-e89e7cecc2d2'
    }

Request access token

If you integrate Tosca Cloud into a CI/CD pipeline, you must authenticate your API requests using an access token.

To get the access token, send a request to the Token URL including your client_id, client_secret, scope and grant_type from Tosca Cloud.

Example request: 

Copy
stages {
    stage('Get Token') {
            steps {
                powershell '''

                $headers = @{
                    "Content-Type" = "application/x-www-form-urlencoded"
                }

                $Body = "client_id=$env:CLIENT_ID&client_secret=$env:CLIENT_SECRET&scope=$env:E2G_SCOPE&grant_type=client_credentials"

                $maxRetries = 3
                $retryCount = 0
                $success = $false

                do {
                    try {
                        Write-Host "=== TOKEN ACQUISITION API CALL ==="
                        $response = Invoke-RestMethod -Uri "$env:E2G_AUTH_URL" -Method "Post" -Headers $headers -Body $Body
                        $accessToken = $response.access_token
                        $success = $true
                        Write-Host "Token acquired successfully."
                        }
                    catch {
                        $retryCount++
                        Write-Host "Token acquisition failed (attempt $retryCount of $maxRetries): $($_.Exception.Message)"
                        if ($retryCount -lt $maxRetries) {
                            Start-Sleep -Seconds 5
                        }
                    }
                } while (-not $success -and $retryCount -lt $maxRetries)

                if (-not $success) {
                    throw "Failed to acquire token after $maxRetries attempts"
                }
                
                # Save token to file for use in subsequent stages
                $accessToken | Out-File -FilePath "access_token.txt" -Encoding UTF8
                '''
                }
            }

If the authentication succeeds, Jenkins saves the access token to the access_token.txt file specified in the request. By default, it stores this file in C:\ProgramData\Jenkins\.jenkins\workspace\<YourProjectName>. You’ll need this token later to trigger a test run.

Trigger a test run

To send a request and trigger a test run via your Jenkins pipeline, follow these steps: 

  1. Authorize your request in the header: 

  • Set the Authorization header to Bearer <access_token>.

  • Set the Content-Type header to application/json.

  1. In the request body: 

  2. Send a POST request to the endpoint TOSCA_CLOUD_URL/_playlists/api/v2/playlistRuns/.

Example request: 

Copy
stage('Run Tests') {
            steps {
                powershell '''
                $accessToken = Get-Content -Path "access_token.txt" -Raw
                $accessToken = $accessToken.Trim()

                function StartPlaylistExecution {
                    param(
                        [string]$AccessToken,
                        [string]$TtaUrl,
                        [string]$PlaylistId
                    )

                    try {
                        $headers = @{
                            "Authorization" = "Bearer $AccessToken"
                            "Content-Type" = "application/json"
                            "Accept" = "application/json"
                        }

                        $body = @{
                            "playlistId" = $PlaylistId
                            "private" = $false
                        } | ConvertTo-Json

                        Write-Host "=== RUN PLAYLIST API CALL ==="
                        $fullUrl = "$env:TOSCA_CLOUD_URL/_playlists/api/v2/playlistRuns/"
                        Write-Host "Calling: $fullUrl"

                        $response = Invoke-RestMethod -Uri $fullUrl -Method Post -Headers $headers -Body $body

                        if ($response.executionId) {
                            return $response.executionId
                        } elseif ($response.id) {
                            return $response.id
                        } else {
                            throw "No execution ID returned."
                        }
                    }
                    catch {
                        Write-Error "Execution failed: $($_.Exception.Message)"
                        throw
                    }
                }

                $executionId = StartPlaylistExecution $accessToken $env:TOSCA_CLOUD_URL $env:PLAYLIST_ID
                Write-Host "Execution triggered. ID: $executionId"

                # Save execution ID to file for use in subsequent stages
                $executionId | Out-File -FilePath "execution_id.txt" -Encoding UTF8
                '''
            }
        }

If the request succeeds, Jenkins saves the execution ID to the execution_id.txt file specified in the request. By default, it stores this file in C:\ProgramData\Jenkins\.jenkins\workspace\<YourProjectName>. You’ll need this ID later to check the status of the test run.

Check the status of a test run

To send a request and let your Jenkins pipeline check the current status of your test run, follow these steps:

  1. Authorize your request in the header: 

  • Set the Authorization header to Bearer <access_token>.

  • Set the Content-Type header to application/json.

  1. Send a GET request to the endpoint TOSCA_CLOUD_URL/_playlists/api/v2/playlistRuns/<executionId>.

  2. Poll for the status of your test run. The final test run status will be one of the following: 

    • Succeeded

    • Failed

    • Canceled

Example request:

Copy
stage('Wait for Results') {
            steps {
                powershell '''
                # Read token and execution ID from files
                $accessToken = Get-Content -Path "access_token.txt" -Raw
                $accessToken = $accessToken.Trim()
                $executionId = Get-Content -Path "execution_id.txt" -Raw
                $executionId = $executionId.Trim()

                function WaitForExecution {
                param(
                    [Parameter(Mandatory=$true)][string]$AccessToken,
                    [Parameter(Mandatory=$true)][string]$TtaUrl,
                    [Parameter(Mandatory=$true)][string]$ExecutionId,
                    [int]$TimeoutMinutes = 120,
                    [int]$PollingIntervalSeconds = 30
                )

                try {
                    $headers = @{
                        "Authorization" = "Bearer $AccessToken"
                        "Content-Type" = "application/json"
                    }

                    $startTime = Get-Date
                    $timeoutTime = $startTime.AddMinutes($TimeoutMinutes)

                    do {
                        $fullUrl = "$env:TOSCA_CLOUD_URL/_playlists/api/v2/playlistRuns/$ExecutionId"

                        Write-Host "=== PLAYLIST RUN STATUS CHECK API CALL ==="
                        Write-Host "Calling: $fullUrl"

                        $response = Invoke-RestMethod -Uri $fullUrl -Method Get -Headers $headers

                        $status = $response.state

                        if ($status -eq "Completed" -or $status -eq "Succeeded") {
                            Write-Host "Execution finished with status: Succeeded"
                            return "succeeded"
                        }

                        if ($status -eq "Failed") {
                            Write-Host "Execution finished with status: Failed"
                            return "failed"
                        }

                        if ($status -eq "Cancelled" -or $status -eq "Canceled" -or $status -eq "Canceling") {
                            Write-Host "Execution was cancelled"
                            return "canceled"
                        }

                        if ((Get-Date) -gt $timeoutTime) {
                            Write-Host "Execution timed out after $TimeoutMinutes minutes"
                            return $null
                        }

                        Write-Host "Execution still running. Waiting $PollingIntervalSeconds seconds before next check..."
                        Start-Sleep -Seconds $PollingIntervalSeconds

                    } while ($status -eq "Running" -or $status -eq "Queued" -or $status -eq "Pending" -or $status -eq "Starting")

                    return $status
                }
                catch {
                    Write-Error "Failed to wait for execution: $($_.Exception.Message)"
                    throw
                }
                }

                $currentState = WaitForExecution $accessToken $env:TOSCA_CLOUD_URL $executionId

                if ($currentState -eq $null) {
                    throw "Execution timed out"
                }
                if ($currentState -eq 'Canceling' -or $currentState -eq 'Canceled') {
                    throw "Execution Canceled"
                }

                Write-Host "Test execution completed with state: $currentState"

                # Save test state to file for use in subsequent stages
                $currentState | Out-File -FilePath "test_state.txt" -Encoding UTF8
                '''
            }        
        }

If the request doesn't return one of the required test run states from above, the polling stops automatically.

If the request succeeds, Jenkins saves the status to the test_state.txt file specified in the request. By default, it stores this file in C:\ProgramData\Jenkins\.jenkins\workspace\<YourProjectName>. You'll need the final status later to get the JUnit results.

Get results

To let your Jenkins pipeline retrieve the test results in JUnit XML format from Tosca Cloud, follow these steps:

  1. Authorize the request in the header: 

    • Set the Authorization header to Bearer <access_token>.

    • Set the Content-Type header to application/xml.

  1. Send a GET request to the endpoint TOSCA_CLOUD_URL/_playlists/api/v2/playlistRuns/<executionId>/junit.

Example request:

Copy
stage('Get JUnit Results') {
            steps {
                powershell '''
                # Read token and execution ID from files
                $accessToken = Get-Content -Path "access_token.txt" -Raw
                $accessToken = $accessToken.Trim()
                $executionId = Get-Content -Path "execution_id.txt" -Raw
                $executionId = $executionId.Trim()

                function GetJUnitResults {
                    param(
                        [Parameter(Mandatory=$true)][string]$AccessToken,
                        [Parameter(Mandatory=$true)][string]$TtaUrl,
                        [Parameter(Mandatory=$true)][string]$ExecutionId
                    )

                    try {
                        $headers = @{
                            "Authorization" = "Bearer $AccessToken"
                            "Accept" = "application/xml"
                        }

                        $fullUrl = "$env:TOSCA_CLOUD_URL/_playlists/api/v2/playlistRuns/$ExecutionId/junit"
                        Write-Host "=== JUNIT RESULTS API CALL ==="
                        Write-Host "Calling: $fullUrl"
                        Write-Host "---------------------------------"

                        $response = Invoke-RestMethod -Uri $fullUrl -Method Get -Headers $headers

                        # Save JUnit XML to file
                        $junitXml = $response.OuterXml
                        $junitXml | Out-File -FilePath "junit-results.xml" -Encoding UTF8
                        Write-Host "JUnit results saved to junit-results.xml"

                        # Parse and display summary
                        $xmlDoc = [xml]$junitXml
                        $testsuites = $xmlDoc.testsuites
                        
                        Write-Host "=== JUNIT RESULTS SUMMARY ==="
                        Write-Host "Playlist: $($testsuites.name)"
                        Write-Host "Total Tests: $($testsuites.tests)"
                        Write-Host "Failures: $($testsuites.failures)"
                        Write-Host "Errors: $($testsuites.errors)"
                        Write-Host "Disabled: $($testsuites.disabled)"
                        Write-Host "---------------------------------"

                        # Display individual test cases
                        $testcases = $xmlDoc.SelectNodes("//testcase")
                        Write-Host "=== INDIVIDUAL TEST CASES RESULT==="
                        foreach ($testcase in $testcases) {
                            $status = "Succeeded"
                            
                            # Check if the testcase has a failure child element
                            $failureElements = $testcase.SelectNodes("failure")
                            if ($failureElements.Count -gt 0) {
                                $status = "Failed"
                            }
                            
                            Write-Host "$($testcase.name): $status"
                        }

                        return $junitXml
                    }
                    catch {
                        Write-Error "Failed to get JUnit results: $($_.Exception.Message)"
                        throw
                    }
                }

                $junitResults = GetJUnitResults $accessToken $env:TOSCA_CLOUD_URL $executionId
                Write-Host "---------------------------------"                
                Write-Host "JUnit results retrieved successfully"
                '''
                }
            }
    }
}

If the request succeeds, Jenkins saves the response to the junit-results.xml file specified in the request. By default, it stores this file in C:\ProgramData\Jenkins\.jenkins\workspace\<YourProjectName>.

A successful response gives a clear summary of how your tests performed and lets your Jenkins pipeline show the results visually. Check the XML file to see these test results:

  • Total number of tests

  • Number of failed tests

  • Number of errors

  • Individual test case results

Complete code example

Here's the full code example to help you set up a Jenkins pipeline for running Tosca Cloud playlists.