Generate PlantUML in you CI/CD Pipeline
This is part of a series creating around PlantUML and Diagrams as Code.
- Authoring Plant UML files in Visual Studio Code
- Creating Image file from PlantUML using command line
- Creating Pipeline to build and check-in Diagrams as Code (This Post)
- Optimise the Pipeline
In the previous posts in the series we have looked how to author as well as how to build the PlantUML files using command line tools.
In this post will we build the PlantUML diagrams on check-in.
Getting started
Step 1 - Setup Azure DevOps Project to enable build agent
We are going to configure Azure DevOps Pipeline to build the PNG when we check in the wiki. So the PNG needs to be added to the Repository. Configure the security for the given project follow the steps in this tip https://azuredevops.tips/2020/02/29/repo-allow-build-agent-to-commit-code/ .
Step 2 - Configure Pipeline
Create new Pipeline
Select the location where your PlantUML documents are located.
Create Starter Pipeline
Pipeline to generate PlantUML diagrams
Build PNG files from PUML
|
|
Double check Security
The build agent security needs to be configured properly look at https://azuredevops.tips/allow-build-agent-to-commit-code/ this will give the instructions on how to enable the right security settings.
If these settings are set you the pipeline won’t be able to check in the images.
Break it Down
- Define Windows Build Agent
|
|
Specify a Windows build agent, at the moment this script only works on Windows, I have yet to get it working on other platforms. The build agent will clear out all the content on the build agent, it will also make sure that if multiple changes happen in a short period it will batch up these changes.
- dfd
|
|
Persist Credentials, enables the pipeline to use OAuth token to check in the new PNGs.
|
|
task: [email protected] displayName: Check Out inputs: targetType: ‘inline’ script: | git config –global user.email “[email protected]” git config –global user.name “Your Name” git status
git checkout "$Env:BUILD_SOURCEBRANCHNAME"
|
|
- task: [email protected]
displayName: Build Images
inputs:
targetType: ‘inline’
script: |
$sourceRoot = “$Env:Build.SourcesDirectory”
Get-ChildItem $sourceDir -Recurse -Filter “*.puml” |
Foreach-Object {
$fileWithPath = $_.Fullname
$png = Join-Path -Path $_.DirectoryName -ChildPath ("{0}.png” -f $_.Basename ) $execute = “puml generate $fileWithPath -o $png “; Invoke-Expression -Command $execute }
|
|
- task: [email protected] displayName: Check In inputs: targetType: ‘inline’ script: | git add –all git commit -m “$Env:BUILD_BUILDNUMBER [skip ci]” git push origin
|
|
# Starter pipeline# Start with a minimal pipeline that you can customize to build and deploy your code.# Add steps that build, run tests, deploy, and more:# https://aka.ms/yaml
trigger: batch: true paths: include: - plantuml/* exclude: - wiki/*
pool: vmImage: ‘windows-2019'
steps: - checkout: self persistCredentials: true clean: true- task: [email protected] displayName: Check for Changed YAML inputs: targetType: ‘inline’ script: | Write-Output "##vso[task.setvariable variable=continue]false” $files=$(git diff HEAD HEAD~ –name-only) $temp=$files -split ' ' $count=$temp.Length For ($i=0; $i -lt $temp.Length; $i++) { $name=$temp[$i] if ($name.EndsWith('.puml’)) { echo “this is $name file” Write-Output "##vso[task.setvariable variable=continue]true” Break } }
- task: [email protected] displayName: Install Node inputs: version: ‘6.x’ condition: eq(variables[‘continue’],‘true’)
- task: [email protected] displayName: Install Node Plant UML inputs: command: ‘custom’ customCommand: ‘install node-plantuml -g’ condition: eq(variables[‘continue’],‘true’)
- task: [email protected] displayName: Install PlantUML Latest and GraphViz inputs: targetType: ‘inline’ script: | choco install graphviz choco install plantuml condition: eq(variables[‘continue’],‘true’)
- task: [email protected] displayName: Build Images just for the changed Puml inputs: targetType: ‘inline’ script: | git config –global user.email “[email protected]” git config –global user.name “Your Name” # if ($Env:BUILD_SOURCEBRANCH -eq “refs/heads/master”) # { # Write-Host “Building master branch so no merge is needed." # exit # } # $sourceBranch= “origin/$Env:BUILD_SOURCEBRANCH:refs/heads/=” # $editedFiles = git diff HEAD HEAD~ –name-only # $editedFiles | ForEach-Object { # Switch -Wildcard ($_ ) { # ‘SubFolderA/*' { Write-Output "##vso[task.setvariable variable=MicroserviceA]True” } # The rest of your path filters # } # } git branch “$Env:BUILD_BUILDNUMBER” git checkout “$Env:BUILD_BUILDNUMBER” -q #git checkout master $files=$(git diff HEAD HEAD~ –name-only) $temp=$files -split ' ' $count=$temp.Length For ($i=0; $i -lt $temp.Length; $i++) { $name=$temp[$i] if ($name.EndsWith('.puml’)) { $fileWithPath = $name $png = Join-Path -Path (Get-Item $fileWithPath).DirectoryName -ChildPath ("{0}.png” -f (Get-Item $fileWithPath).Basename ) Write-Host $png $execute = “puml generate $fileWithPath -o $png "; Invoke-Expression -Command $execute } } git add –all git commit $sourceBranch -m “Add Generated Images-[ci skip]" git checkout ${Env:BUILD_SOURCEBRANCHNAME} -q git merge “$Env:BUILD_BUILDNUMBER” -q git push -q
|
|