0
votes

I want to analyze the log of the Ansible task inside a release pipeline with a custom task extension (node). In generell this is no big thing, but I ask my self whether it is possible to get the position of my executing task inside the pipeline?

Usecase

The user could add an Ansible task and if he want he can add my custom task to analyze the log (and publish somewhere). To decide which Ansible task inside the release pipeline should analyzed, the user should but the custom task directly after the Ansible task.

Something like this:

Release Pipeline 1.) Stage:
- Initialize job
- Download artifacts
- CopyFilesOverSSH
- Ansible <--- this should analyzed
- Custom Task 
- Ansible
- Finialize job

Szenario

A release Pipeline with 3 Stages.

  • 1.) Stage linear
  • 2.) Stage & 3.) Stage parallel on second level
  • all stages contains a ansible task
  • all stages should contain my custom task extension to analyze the ansible task log

The Problem

When I now request the Get Release REST call I can loop though all environments, all jobs, all tasks looks like:

    let release: Release = await Api.getRelease(Env.System.TeamProject, Env.Release.ReleaseId);

    // loop environments (stages)
    for (let environment of release.environments) {
      // loop deploy steps
      for (let deployStep of environment.deploySteps || []) {
        // loop phases
        for (let releaseDeployPhase of deployStep.releaseDeployPhases || []) {
          // loop jobs
          for (let deploymentJob of releaseDeployPhase.deploymentJobs || []) {
            let ansible: ReleaseTask[] = [];
            // loop tasks
            for (let task of deploymentJob.tasks || []) {
              if (task.startTime && task.finishTime) {
                if (task.name === "Ansible") ansible.push(task);
              }
            }
            console.log(ansible);
          }
        }
      }
    }

on runtime on this state my executing custom task is not finished yet (TaskStatus.InProgress), so together with the id of my task I am should able to detect the position inside the pipeline.

Some better Solution?

But I hope there is a much better solution, I mean the task should know which position it has inside the pipeline or? Maybe there is an information you can get over the azure-pipelines-task-lib/task lib?

Something like:

task.getRank() >>> 3
task.getEnvironment >>> 2

What also could help is when you could request the task name and the id inside the task program. Currently I have to create own variables with these information.

Something like this:

me.Id() >>> "8bb50e0a-8efb-47a4-b12a-2190f3a4d16a"
me.Name() >>> "MyCustomTask"
1

1 Answers

0
votes

We could analyze the log of the Ansible task via power shell and REST API, an example is shown below:

  $listurl="https://vsrm.dev.azure.com/{Org name}/{Project name}/_apis/release/releases/{Release id}?api-version=6.1-preview.8"
    $PAT="{PAT}"
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
    $result = Invoke-RestMethod -Uri $listurl -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get
    
$result.environments.deploySteps.releaseDeployPhases.deploymentJobs.tasks.name
    
    foreach($deploySteps in $result.environments.deploySteps)
    {
        write-host $deploySteps.releaseDeployPhases.deploymentJobs.tasks.name
        foreach($Task in $deploySteps.releaseDeployPhases.deploymentJobs.tasks){
            if($Task.name -eq "{analyze task display name}")
            {
                #write-host $Task.logUrl
                $TaskLogURL = $Task.logUrl
            }
        }
    }
    #write-host $TaskLogURL
    $TaskLog = Invoke-RestMethod -Uri $TaskLogURL -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get
    
    write-host $TaskLog