Giter VIP home page Giter VIP logo

arindam0310018 / 30-jan-2023-devopscli__create-docker-registry-service-connection Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 459 KB

CREATE DOCKER REGISRY SERVICE CONNECTION USING DEVOPS CLI

Home Page: https://dev.to/arindam0310018/create-docker-regisry-service-connection-using-devops-cli-1an8

License: MIT License

PowerShell 100.00%
azure azurecontainerregistry community devops service-connection

30-jan-2023-devopscli__create-docker-registry-service-connection's Introduction

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 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:-
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"

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:-

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

30-jan-2023-devopscli__create-docker-registry-service-connection's People

Contributors

arindam0310018 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.