HodorNV / ALOps

ALOps
59 stars 24 forks source link

Question - how to get and store Imagename after "ALOpsDockerCreate" #263

Closed dNsl9r closed 3 years ago

dNsl9r commented 3 years ago

I'm using "ALOpsDockerCreate" in a scheduled pipeline to create new images. The images created by the task are something like "bcdecurr:sandbox-17.1.18256.19044-de-10.0.17763.1282-ltsc2019" etc. I'd love to store the full image naming in a variable in the Azure Devops Library.

Can I save the imagename to an outpur variable or something similar in the pipeline? if yes, how? if no, how to get the full imagename to store it in the DevOps Library?

waldo1001 commented 3 years ago

I don't think that is possible - if if you find that out, I'd be happy to know :-).

First of all: you can set up an image template for yourself. This way, you can predict the name. by default, it's: %IMAGE_PREFIX%:%ARTIFACT_TYPE%-%ARTIFACT_VERSION%-%ARTIFACT_COUNTRY%-%OS_VERSION%-%OS_LTSC%

second: you'll get output like this: image so you get the image name AND you have a variable that contains it, should you want to so do something with it.

dNsl9r commented 3 years ago

Okay, found a solution. We need a few prerequisites.

  1. Variable Group with a Variable of course
  2. Set permission for "Project Collection Build Service" in Variable Group to "Administrator" (Reader or User won't be enough)
  3. Write some powershell (of course..)

The YAML could look like the following:

name: Build CI Docker Image $(Build.BuildId)

trigger:
    - none

schedules:
  - cron: "0 0 1 * *"
    displayName: Monthly Build Image Creation
    branches:
      include: 
        - master
    always: true

variables:
    - group: 'ALOps License'
    - group: DockerVariables

stages:

- stage: BuildDockerCIImages
  pool: Default
  jobs:

  - job: BuildDockerImages
    dependsOn: CleanBuildMachine
    variables:
    - name: DEImageName
    steps:
    - checkout: none

    - task: ALOpsDockerCreate@1
      displayName: 'Create DE Docker Image'
      name: CreateDEDockerImage
      inputs:
        artifacttype: 'Sandbox'
        artifactcountry: 'de'
        imageprefix: 'bcdecurr'
        includetesttoolkit: true'
      #Here we are building the image on our build server

    - powershell: |
        $VariableGroupId = n 
        #this has to be an integer, you'll find out when you open the variable group in the browser. See the URL!
        $VariableNameDE = "BCCurrentMajorDEImageName"
        #this is the name of the variable in your variable group.
        $NewValueDE = "$(ALOPS_BC_IMAGE)"
        #this is - wow! - the new value you want to store. the step "ALOpsDockerCreate" stores the generated image name in this variable

        $varGroupUri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/distributedtask/variablegroups/$($VariableGroupId)?api-version=4.1-preview.1"
        $authHeader = @{Authorization = "Bearer $(system.accesstoken)"}
        $varGroupDefinition = Invoke-RestMethod -UseBasicParsing -Uri $varGroupUri -Headers $authHeader
        if ($varGroupDefinition) {
            $varGroupDefinition.variables.$VariableNameDE.Value = "$($NewValueDE)"
            $varGroupDefinitionJson = $varGroupDefinition | ConvertTo-Json -Depth 100 -Compress
            Write-Verbose "Updating variable group: $($varGroupUri)"           
            Invoke-RestMethod -UseBasicParsing -Method Put -Uri $varGroupUri -Headers $authHeader -ContentType "application/json" -Body ([System.Text.Encoding]::UTF8.GetBytes($varGroupDefinitionJson)) | Out-Null
            #Here we are calling the REST API, updating the variable in the variable group.   
        }
        else {
            Write-Error "The variable group can not be found, or the 'Project Collection Build Service' doesn't have administrator role in the variable group."
        }    

The powershell code is based on the Azure DevOps Extension "Shared variable updater": https://github.com/lanalua/azure-pipeline-variables-updater

As we have strict rules for DevOps AddOns I could not just install it. Otherwise you do not need the powershell step, instead you could just use the step that the addon brings you.

So - my full automization of everything is getting better and better 😆

waldo1001 commented 3 years ago

Smart!

Thanks a lot for sharing!

dNsl9r commented 3 years ago

Sure, maybe it will help someone else