Greetings to my fellow Technology Advocates and Specialists.
In this Session, I will demonstrate How to Create Docker Registry Service Connection Using DevOps CLI.
USE CASES:- |
---|
Create Docker Registry DevOps Service Connection, Prompting PAT (Personal Access Token) |
Create Docker Registry DevOps Service Connection, Without Prompting PAT (Personal Access Token) |
REQUIREMENTS:- |
---|
- Azure Subscription.
- Azure Container Registry with "Admin" User Enabled.
- Azure DevOps Organisation and Project.
- Azure DevOps Project ID.
- Full Access PAT (Personal Access Token).
AZURE CONTAINER REGISTRY DETAILS:- |
---|
BELOW FOLLOWS THE CODE SNIPPET:- |
---|
USE CASE #1:- |
---|
CREATE DOCKER REGISTRY DEVOPS SERVICE CONNECTION WITH PAT AS USER INPUT (Create-Docker-Registry-DevOps-Service-Connection-Prompt-PAT.ps1):- |
---|
##############
# VARIABLES:-
##############
$devopsOrg = "https://dev.azure.com/arindammitra0251/"
$devopsPrj = "AMCLOUD"
$configJSON = "acr-srv-connection.json"
$dockersrvconn = "AM-ACR-Srv-Connection"
##############
# CORE SCRIPT:-
##############
# Perform DevOps Login. It will Prompt for PAT:-
az devops login
# Set Default DevOps Organisation and Project:-
az devops configure --defaults organization=$devopsOrg project=$devopsPrj
# Create DevOps Service Connection:-
az devops service-endpoint create --service-endpoint-configuration .\$configJSON
# Grant Access to all Pipelines to the Newly Created Docker Registry DevOps Service Connection:-
$srvEndpointID = az devops service-endpoint list --query "[?name=='$dockersrvconn'].id" -o tsv
az devops service-endpoint update --id $srvEndpointID --enable-for-all
USE CASE #2:- |
---|
CREATE DOCKER REGISTRY DEVOPS SERVICE CONNECTION WITH PAT AS ENVIRONMENT VARIABLE (Create-Docker-Registry-DevOps-Service-Connection-Without-Prompting-PAT.ps1):- |
---|
##############
# VARIABLES:-
##############
$pat = "<Provide your own PAT>"
$devopsOrg = "https://dev.azure.com/arindammitra0251/"
$devopsPrj = "AMCLOUD"
$configJSON = "acr-srv-connection.json"
$dockersrvconn = "AM-ACR-Srv-Connection"
##############
# CORE SCRIPT:-
##############
# Set environment variable for DevOps Login:-
$env:AZURE_DEVOPS_EXT_PAT = $pat
# Set Default DevOps Organisation and Project:-
az devops configure --defaults organization=$devopsOrg project=$devopsPrj
# Create DevOps Service Connection:-
az devops service-endpoint create --service-endpoint-configuration .\$configJSON
# Grant Access to all Pipelines to the Newly Created Docker Registry DevOps Service Connection:-
$srvEndpointID = az devops service-endpoint list --query "[?name=='$dockersrvconn'].id" -o tsv
az devops service-endpoint update --id $srvEndpointID --enable-for-all
JSON FORMAT CONFIGURATION FILE:- |
---|
{
"data": {},
"name": "AM-ACR-Srv-Connection",
"type": "dockerregistry",
"authorization": {
"parameters": {
"username": "ampocapplacr",
"password": "XXXXXXXXXXXXXXXXXXXXXXXXX",
"email": "[email protected]",
"registry": "https://ampocapplacr.azurecr.io"
},
"scheme": "UsernamePassword"
},
"isShared": false,
"isReady": true,
"serviceEndpointProjectReferences": [
{
"projectReference": {
"id": "36aaac58-e06f-47ed-8b98-003ad670ee3c",
"name": "AMCLOUD"
},
"name": "AM-ACR-Srv-Connection"
}
]
}
DIFFERENCE BETWEEN BOTH USE CASES:- |
---|
In Use Case 1, PAT is prompted as User Input during script execution. |
In Use Case 2, PAT is set as Environment variable so that it is not prompted as User Input during script execution. |
TASKS TO BE PERFORMED BEFORE RUNNING THE SCRIPTS:- |
---|
1. Fetch the ID of the DevOps Project for which, we need to create Docker Registry Service Connection. |
2. Prepare the JSON Format Configuration file. |
COMMANDS TO FETCH THE ID OF THE DEVOPS PROJECT:- |
---|
az devops login
az devops project show --project AMCLOUD --query "[id]" -o tsv
SAMPLE OUTPUT:- |
---|
HOW TO PREPARE THE JSON FORMAT CONFIGURATION FILE:- |
---|
Sample format of the JSON Config file can be found HERE |
Below Needs to be changed in the JSON Configuration file:- |
1. name: "Custom Name for Docker Registry DevOps Service Connection." |
2. username: "Name of Azure Container Registry." |
3. password: "Password of Azure Container Registry" |
4. email: "Custom Email Address." |
5. registry: "Azure Container Registry Login Server URL." |
6. id: "ID of the DevOps Project where the Docker Registry Service Connection will be created." |
7. name: "Name of the DevOps Project where the Docker Registry Service Connection will be created." |
Now, let me explain the script, part by part for better understanding.
VARIABLES:- |
---|
USE CASE 1:-
##############
# VARIABLES:-
##############
$devopsOrg = "https://dev.azure.com/arindammitra0251/"
$devopsPrj = "AMCLOUD"
$configJSON = "acr-srv-connection.json"
$dockersrvconn = "AM-ACR-Srv-Connection"
USE CASE 2:-
##############
# VARIABLES:-
##############
$pat = "<Provide your own PAT>"
$devopsOrg = "https://dev.azure.com/arindammitra0251/"
$devopsPrj = "AMCLOUD"
$configJSON = "acr-srv-connection.json"
$dockersrvconn = "AM-ACR-Srv-Connection"
NOTE:- |
---|
1. Please change the values of the variables accordingly. |
2. The entire script is build using Variables. No Values are Hardcoded. Changing the values of the variables should help you execute the script seamlessly. |
3. In Use Case #1, PAT is User Input, and in Use Case #2, PAT is defined as Variable and then passed as Environmental Variable for DevOps Login. |
4. The Value of the Variable "$configJSON" is the Custom Name of the JSON Format Configuration File. |
5. The Value of the Variable "$dockersrvconn" is the Custom Name of Docker Registry DevOps Service Connection. Please Remember to keep the Custom Name of Docker Registry Service Connection Same in both files - 1) In JSON Format Configuration, and 2) In Powershell Script. |
CORE SCRIPT:- |
---|
Perform DevOps Login. It will Prompt for PAT Token:-
az devops login
OR
Set PAT as an environment variable for DevOps Login:-
$env:AZURE_DEVOPS_EXT_PAT = $pat
Set Default DevOps Organisation and Project:-
az devops configure --defaults organization=$devopsOrg project=$devopsPrj
Create Docker Registry DevOps Service Connection using the JSON Format Configuration File:-
az devops service-endpoint create --service-endpoint-configuration .\$configJSON
Grant Access to all Pipelines to the Newly Created Docker Registry DevOps Service Connection:-
$srvEndpointID = az devops service-endpoint list --query "[?name=='$dockersrvconn'].id" -o tsv
az devops service-endpoint update --id $srvEndpointID --enable-for-all
NOW ITS TIME TO TEST:-
Hope You Enjoyed the Session!!!
Stay Safe | Keep Learning | Spread Knowledge