Mastering Azure DevOps: A Step-by-Step Guide to Moving a Pipeline to /Archived using PowerShell
Image by Jhonna - hkhazo.biz.id

Mastering Azure DevOps: A Step-by-Step Guide to Moving a Pipeline to /Archived using PowerShell

Posted on

Are you tired of cluttered Azure DevOps pipelines? Do you want to keep your pipelines organized and tidy? Look no further! In this comprehensive guide, we’ll take you on a journey to master the art of moving a pipeline to /Archived using PowerShell. Buckle up, and let’s dive in!

Why Archive Pipelines?

Pipelines can become outdated, redundant, or simply take up valuable space in your Azure DevOps organization. Archiving them is an excellent way to declutter your pipeline list, improve performance, and maintain a clean environment. Moreover, archiving pipelines doesn’t delete them; it merely moves them to a separate section, allowing you to easily retrieve or restore them if needed.

Prerequisites

Before we begin, ensure you have the following:

  • Azure DevOps organization with a pipeline to archive
  • Azure DevOps CLI installed on your machine (version 2.30.0 or later)
  • Powershell (version 7 or later)
  • Administrative permissions in your Azure DevOps organization

Step 1: Install the Azure DevOps CLI

If you haven’t already, install the Azure DevOps CLI using the following command:

iex "& { $(irm https://aka.ms/installazurecli.ps1) }"

This will download and install the Azure DevOps CLI on your machine.

Step 2: Authenticate with Azure DevOps

Next, authenticate with Azure DevOps using the following command:

az devops login --organization https://dev.azure.com/{your-organization-name}

Replace `{your-organization-name}` with your actual Azure DevOps organization name.

Step 3: Identify the Pipeline to Archive

Identify the pipeline you want to archive by noting its ID or name. You can do this by:

  • Accessing the pipeline in Azure DevOps and copying the URL, which should contain the pipeline ID
  • Using the Azure DevOps CLI command: az pipelines list --query "[].{name,id}" to list all pipelines with their IDs

For this example, let’s assume the pipeline ID is `123456` and the name is `MyPipeline`.

Step 4: Archive the Pipeline using PowerShell

Now, it’s time to archive the pipeline using PowerShell. Use the following script:

$pipelineId = "123456"
$projectId = "{your-project-id}"
$organizationUrl = "https://dev.azure.com/{your-organization-name}"

$azureDevOpsAuth = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($env:AZURE_DEVOPS_EXT_PAT)"))
$headers = @{ 
  "Authorization" = "Basic $azureDevOpsAuth"
  "Content-Type" = "application/json"
}

$body = @{
  "state" = @{
    "result" = "archived"
  }
} | ConvertTo-Json -Depth 100

$uri = "$organizationUrl/$projectId/_apis/pipelines/$pipelineId?api-version=6.0"

Invoke-RestMethod -Uri $uri -Method Patch -Headers $headers -Body $body

Replace `{your-project-id}`, `{your-organization-name}`, and `$env:AZURE_DEVOPS_EXT_PAT` with your actual project ID, organization name, and Azure DevOps personal access token, respectively.

Step 5: Verify the Pipeline Archive Status

After running the script, verify that the pipeline has been successfully archived:

az pipelines show --id $pipelineId --query "state.result"

This should return `archived` as the pipeline status.

Step 6: Confirm Pipeline Archive in Azure DevOps

Log in to your Azure DevOps organization and navigate to the pipeline list. You should now see the archived pipeline in the `/Archived` section:

Pipeline Name Status
MyPipeline Archived

Tips and Variations

Here are some additional tips and variations to keep in mind:

  • To restore an archived pipeline, simply update the `state.result` to `setEnabled` instead of `archived` in the PowerShell script.
  • You can also use the Azure DevOps CLI to archive pipelines: az pipelines update --id $pipelineId --state archived
  • To archive multiple pipelines, create a loop in your PowerShell script and iterate through the pipeline IDs.
  • Remember to update your Azure DevOps personal access token periodically to maintain security and compliance.

Conclusion

With these steps, you’ve successfully mastered the art of moving a pipeline to `/Archived` using PowerShell in Azure DevOps! By following this guide, you’ll be able to maintain a tidy and organized pipeline list, improving your overall Azure DevOps experience.

Remember to bookmark this article and share it with your colleagues to spread the knowledge. Happy archiving!

Frequently Asked Questions

Got stuck while trying to move a pipeline in Azure DevOps to /archived using PowerShell? Don’t worry, we’ve got you covered! Here are some common questions and answers to help you out:

Q: What is the correct PowerShell command to move a pipeline to the archived state in Azure DevOps?

A: The correct PowerShell command is `Update-AzPipeline -ProjectName -PipelineName -State Archived`. Make sure to replace `` and `` with your actual project and pipeline names!

Q: Do I need to have specific permissions to move a pipeline to the archived state using PowerShell?

A: Yes, you need to have the necessary permissions to update the pipeline. You should be a member of the Project Administrators group or have the “Edit Pipelines” permission to move a pipeline to the archived state.

Q: What happens to the pipeline runs and history when I move a pipeline to the archived state?

A: When you move a pipeline to the archived state, its runs and history remain intact. You can still view the pipeline’s runs and history, but you won’t be able to edit or trigger new runs for the pipeline.

Q: Can I undo the archival of a pipeline in Azure DevOps using PowerShell?

A: Yes, you can undo the archival of a pipeline by running the command `Update-AzPipeline -ProjectName -PipelineName -State Enabled`. This will restore the pipeline to its original state.

Q: Are there any additional PowerShell modules required to move a pipeline to the archived state in Azure DevOps?

A: Yes, you need to have the Azure DevOps PowerShell module installed and imported. You can install it by running `Install-Module -Name Az.DevOps` and then import it using `Import-Module -Name Az.DevOps`.