DEV Community

Cover image for Create docker registry service connection using Devops CLI
Arindam Mitra
Arindam Mitra

Posted on • Updated on

Create docker registry service connection using Devops CLI

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:-
  1. Azure Subscription.
  2. Azure Container Registry with "Admin" User Enabled.
  3. Azure DevOps Organisation and Project.
  4. Azure DevOps Project ID.
  5. Full Access PAT (Personal Access Token).
CODE REPOSITORY:-

GitHub logo arindam0310018 / 30-Jan-2023-DevOpsCLI__Create-Docker-Registry-Service-Connection

CREATE DOCKER REGISRY SERVICE CONNECTION USING DEVOPS CLI

CREATE DOCKER REGISRY SERVICE CONNECTION USING DEVOPS CLI:-

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:-
  1. Azure Subscription.
  2. Azure Container Registry with "Admin" User Enabled.
  3. Azure DevOps Organisation and Project.
  4. Azure DevOps Project ID.
  5. Full Access PAT (Personal Access Token).
AZURE CONTAINER REGISTRY DETAILS:-
Image description
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
AZURE CONTAINER REGISTRY DETAILS:-
Image description
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



Enter fullscreen mode Exit fullscreen mode
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


Enter fullscreen mode Exit fullscreen mode
JSON FORMAT CONFIGURATION FILE:-


{
  "data": {},
  "name": "AM-ACR-Srv-Connection",
  "type": "dockerregistry",
  "authorization": {
    "parameters": {
      "username": "ampocapplacr",
      "password": "XXXXXXXXXXXXXXXXXXXXXXXXX",
      "email": "mail2arindam2003@yahoo.com",
      "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"
    }
  ]
}


Enter fullscreen mode Exit fullscreen mode
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


Enter fullscreen mode Exit fullscreen mode
SAMPLE OUTPUT:-
Image description
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"


Enter fullscreen mode Exit fullscreen mode

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"


Enter fullscreen mode Exit fullscreen mode
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


Enter fullscreen mode Exit fullscreen mode

OR

Set PAT as an environment variable for DevOps Login:-



$env:AZURE_DEVOPS_EXT_PAT = $pat


Enter fullscreen mode Exit fullscreen mode

Set Default DevOps Organisation and Project:-



az devops configure --defaults organization=$devopsOrg project=$devopsPrj


Enter fullscreen mode Exit fullscreen mode

Create Docker Registry DevOps Service Connection using the JSON Format Configuration File:-



az devops service-endpoint create --service-endpoint-configuration .\$configJSON


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

NOW ITS TIME TO TEST:-

TEST CASES:-
1. Output of the script when executed successfully.
Image description
Output Continued...
Image description
2. Docker Registry DevOps Service Connection created successfully with access granted to all pipelines.
Image description
Output Continued...
Image description

Hope You Enjoyed the Session!!!

Stay Safe | Keep Learning | Spread Knowledge

Top comments (0)