Streamlining Changelog Creation with Azure DevOps and Confluence

In the fast-paced world of software development, maintaining a detailed record of changes is crucial. Changelogs serve as a vital tool, tracking every update, fix, or feature added to a project. Integrating Azure DevOps with Confluence can create a seamless changelog management system that enhances transparency and efficiency. This blog post explores how to automate changelog creation in Confluence using a PowerShell script that extracts release information from an Azure DevOps repository.

Self-Hosted build Agent for Azure Pipelines
Self-Hosted build Agent for Azure Pipelines

By the end of this article, you will have a script that automates the extraction of changelog information from the latest and previous versions of a repository using Git commands. It then uses the ConfluencePS module to create a new Confluence page displaying the changelog. The script is configured to run in Azure Pipelines under certain conditions, such as when a new tag is created in the repository.

Let’s start!

What do we need?

  1. Receive release information.
  2. Retrieving the changelog between previous and latest version.
  3. Create a Confluence page.
  4. Integration with Azure pipelines.

Let's start by preparing Confluence to create a new changelogs folder. To do this we need to follow these steps:

Step 1: Creating a Parent Page:

  • Sign in to your Confluence account.
  • Go to the Space where you want to create the changelogs folder.
  • Create a new page that will serve as the parent page for all future changelogs pages. This can be done by selecting "Create" and following the instructions to create a page.

Step 2: Getting SpaceKey:

  • The SpaceKey is usually shown in the URL when you are in a Confluence space.
  • If the key does not appear in the URL, you can find it by going to Space tools > Overview > Space details.

Step 3: Getting Parent Page ID:

  • The Parent Page ID (ParentID) can be found in the URL when you are on the page that will serve as the parent of changelogs. In the page URL, look at the pageId parameter, this will be the ParentID.

Step 4: Confluence Site URL entry:

You now have all the information you need to create new changelogs pages using the script.

[⚠️ Warning:]Make sure you have the appropriate permissions to create and edit pages in your chosen Confluence space.

Step 5: Creating PowerShell Script

Now we create a file confluence.ps1 in the root of our repository.

Firstly our script gets a list of changes between these versions. (Line 1-11)

Next the script creates a new Confluence page that displays the change log, using the ConfluencePS module, passes it to the environment variable for our pipeline. (Line 13-33)

Param ([String]$username, [String]$password) # Get username and password options from Azure Pipeline
$repoPath = "." # Set the repository path as the current directory
$currentVersion = git describe --abbrev=0 HEAD  # Get the latest version of the repository using git
# Write-Host $currentVersion
$prevVersion = git describe --abbrev=0 $currentVersion^  #Get the previous version of the repository using git
# Write-Host $prevVersion
$changelog = git log --no-merges --pretty="- %s<br />" "$prevVersion..$currentVersion"  #Get the list of changes between the two versions using git
# Write-Host $changelog
# exit -0
$confluenceUrl = "https://your.atlassian.net/wiki"  #Set the Confluence URL
$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force #Convert the password to a secure string
$credentials = New-Object System.Management.Automation.PSCredential `
     -ArgumentList $username, $securePassword   #Create a credential object for Confluence
#Check if the module ConfluencePS is installed
if (-not (Get-Module -Name ConfluencePS -ListAvailable)) {
    Install-Module -Name ConfluencePS -Scope CurrentUser -Force
}
Import-Module ConfluencePS # Import the module ConfluencePS
Set-ConfluenceInfo -BaseURI $confluenceUrl -Credential $credentials   #Set the information about Confluence using the URL and credentials
$body = @"
<h2>What's new in comparison with version $prevVersion</h2>
<pre>$changelog</pre>
"@
$page = New-ConfluencePage -Title "What's new in $currentVersion" -SpaceKey YOURSPACEKEY -ParentID YOUR_PARENT_ID -Body $body  #Create a new Confluence page with the given title, space key and body
$pageObj = Get-ConfluencePage -PageID $page.ID     # Get the Confluence page object by ID
Write-Output $pageObj.URL     #Print the page URL to the screen
Write-Host "##vso[task.setvariable variable=confluenceUrl]$($pageObj.URL)"    #Set the variable confluenceUrl for Azure Pipeline using the page URL

[⚠️ Warning:] To use this script, you need to change the following:
> $confluenceUrl to your Confluence site URL. (line 13)

YOURSPACEKEY on the key of your space in Confluence. (line 30)
YOUR_PARENT_ID on the ID of the parent page where the new page is to be created. (line 30)
• Make sure you have Git installed and available in your script path.
• Make sure you have rights to create pages in the specified Confluence space.
• If you are not using Azure Pipeline, remove or modify the lines associated with Write-Host "##vso[task.setvariable variable=confluenceUrl]$($pageObj.URL)" to suit your runtime environment.

In the script, you can experiment with the body of the page and the list of data that you want to send to the changelog.

Step 6: Integration with Azure pipelines.

The task is configured to run in Azure Pipelines and only runs under certain conditions, such as when a new tag is created in the repository. $USERNAME and $PASSWORD variables are stored in pipeline secrets and are passed during pipeline startup. The task causes our script to run.

Azure Pipeline Task

- task: PowerShell@2
  condition: and(not(eq(variables['Build.Reason'], 'PullRequest')), BeginsWith(variables['Build.SourceBranch'], 'refs/tags/'))
  displayName: Create a changelog and create a page in Confluence.
  inputs:
    file path: .\confluence.ps1
    arguments: '-username $(USERNAME) -password $(PASSWORD)'

Add it to your Azure pipeline. As a result, we have a page with changes!

Conclusion

Automating change logs with Azure DevOps and Confluence simplifies the documentation process and keeps information up to date. This approach allows teams to focus on development, leaving the routine work of documenting changes.

This blog provides a high-level overview of the integration process and can serve as a starting point for teams looking to improve their change management processes. We hope it was useful.

We wish you a great mood and interesting tasks!