Giter Club home page Giter Club logo

24-apr-2022-devops__dynamic-deployment-of-sonarqube-in-aci's Introduction

DYNAMIC DEPLOYMENT OF SONARQUBE IN AZ CONTAINER INSTANCE USING DEVOPS

Greetings my fellow Technology Advocates and Specialists.

In this Session, I will demonstrate how to Dynamically Deploy SONARQUBE in AZURE CONTAINER INSTANCE using AZURE DEVOPS PIPELINES

LIVE RECORDED SESSION:-
LIVE DEMO was Recorded as part of my Presentation in CLOUD LUNCH AND LEARN Forum/Platform
Duration of My Demo = 44 Mins 19 Secs
IMAGE ALT TEXT HERE
REQUIREMENTS:-
  1. Azure Subscription
  2. Azure DevOps Organisation and Project
  3. Azure Resource Manager Service Connection
Below follows the contents of the YAML File (Azure DevOps):-
trigger:
  none

######################
#DECLARE PARAMETERS:-
######################
parameters:
- name: SubscriptionID
  displayName: Please Provide the Subscription ID:-
  type: object
  default: 210e66cb-55cf-424e-8daa-6cad804ab604

- name: ServiceConnection
  displayName: Please Provide the Service Connection Name:-
  default: amcloud-cicd-service-connection
  values:
  - amcloud-cicd-service-connection

- name: RGName
  displayName: Please Provide the Name of the Resource Group:-
  type: object
  default: DemoRGSonarQube

- name: RGLocation
  displayName: Please Provide Location of Resource Group:-
  default: WestEurope
  values:
  - WestEurope

- name: validateIfRGExists
  displayName: Does the provided Resource Group Exists or do we create ?
  default: Existing
  values:
  - Existing
  - New

- name: ACIName
  displayName: Please Provide the Name of the Azure Container Instance:-
  type: object
  default: am-poc-sonarqube-aci

######################
#DECLARE VARIABLES:-
######################
variables:
  Artifact: AM
  cpu: 2 
  memory: 3.5
  port: 9000
  
#########################
# Declare Build Agents:-
#########################
pool:
  vmImage: windows-latest

###################
# Declare Stages:-
###################
stages:

- stage: VALIDATE_SELECTION_EXISTING
  condition: |
     and(eq('${{ parameters.validateIfRGExists }}', 'Existing'), 
       eq(variables['build.sourceBranch'], 'refs/heads/main')) 
  jobs:
  - job: IF_RG_EXISTS_DEPLOY_SONARQUBE 
    displayName: IF RG EXISTS DEPLOY SONARQUBE
    steps:
    - task: AzureCLI@2
      displayName: CHECK RG AND DEPLOY SONARQUBE
      inputs:
        azureSubscription: ${{ parameters.ServiceConnection }}
        scriptType: ps
        scriptLocation: inlineScript
        inlineScript: |
          az --version
          az account set --subscription ${{ parameters.SubscriptionID }}
          az account show  
          $i = az group exists -n ${{ parameters.RGName }}
          if ($i -eq "true") {
          $j = az container list --query "[?contains(name, '${{ parameters.ACIName }}')].[provisioningState]" -o tsv
            if ($j -eq "Succeeded") {
            echo "##########################################################################################################################################"
            echo "${{ parameters.ACIName }} CONTAINER INSTANCE EXISTS IN THE ${{ parameters.RGName }} RESOURCE GROUP. CANNOT PROCEED WITH THE DEPLOYMENT"
            echo "##########################################################################################################################################"  
              }
            else {   
            az container create -g ${{ parameters.RGName }} --name ${{ parameters.ACIName }} --image sonarqube --ports $(port) --dns-name-label ${{ parameters.ACIName }} --cpu $(cpu) --memory $(memory)
              }
            }
          else {
          az group create -l ${{ parameters.RGLocation }} -n ${{ parameters.RGName }} 
          az container create -g ${{ parameters.RGName }} --name ${{ parameters.ACIName }} --image sonarqube --ports $(port) --dns-name-label ${{ parameters.ACIName }} --cpu $(cpu) --memory $(memory)    
            }

- stage: VALIDATE_SELECTION_NEW
  condition: |
     and(eq('${{ parameters.validateIfRGExists }}', 'New'), 
       eq(variables['build.sourceBranch'], 'refs/heads/main')) 
  jobs:
  - job: CREATE_RG_AND_DEPLOY_SONARQUBE 
    displayName: CREATE RG AND DEPLOY SONARQUBE
    steps:
    - task: AzureCLI@2
      displayName: CHECK RG AND DEPLOY SONARQUBE
      inputs:
        azureSubscription: ${{ parameters.ServiceConnection }}
        scriptType: ps
        scriptLocation: inlineScript
        inlineScript: |
          az --version
          az account set --subscription ${{ parameters.SubscriptionID }}
          az account show  
          $i = az group exists -n ${{ parameters.RGName }}
          if ($i -eq "true") { 
          $j = az container list --query "[?contains(name, '${{ parameters.ACIName }}')].[provisioningState]" -o tsv
            if ($j -eq "Succeeded") {
            echo "##########################################################################################################################################"
            echo "${{ parameters.ACIName }} CONTAINER INSTANCE EXISTS IN THE ${{ parameters.RGName }} RESOURCE GROUP. CANNOT PROCEED WITH THE DEPLOYMENT"
            echo "##########################################################################################################################################"  
              }
            else {   
            az container create -g ${{ parameters.RGName }} --name ${{ parameters.ACIName }} --image sonarqube --ports $(port) --dns-name-label ${{ parameters.ACIName }} --cpu $(cpu) --memory $(memory)
              }
            }             
          else {
          az group create -l ${{ parameters.RGLocation }} -n ${{ parameters.RGName }} 
          az container create -g ${{ parameters.RGName }} --name ${{ parameters.ACIName }} --image sonarqube --ports $(port) --dns-name-label ${{ parameters.ACIName }} --cpu $(cpu) --memory $(memory)    
            }

Let me explain each part of YAML Pipeline for better understanding.

PART #1:-
BELOW FOLLOWS PIPELINE RUNTIME VARIABLES CODE SNIPPET:-
######################
#DECLARE PARAMETERS:-
######################
parameters:
- name: SubscriptionID
  displayName: Please Provide the Subscription ID:-
  type: object
  default: 210e66cb-55cf-424e-8daa-6cad804ab604

- name: ServiceConnection
  displayName: Please Provide the Service Connection Name:-
  default: amcloud-cicd-service-connection
  values:
  - amcloud-cicd-service-connection

- name: RGName
  displayName: Please Provide the Name of the Resource Group:-
  type: object
  default: DemoRGSonarQube

- name: RGLocation
  displayName: Please Provide Location of Resource Group:-
  default: WestEurope
  values:
  - WestEurope

- name: validateIfRGExists
  displayName: Does the provided Resource Group Exists or do we create ?
  default: Existing
  values:
  - Existing
  - New

- name: ACIName
  displayName: Please Provide the Name of the Azure Container Instance:-
  type: object
  default: am-poc-sonarqube-aci
THIS IS HOW IT LOOKS WHEN YOU EXECUTE THE PIPELINE FROM AZURE DEVOPS:-
Image description
NOTE:-
Please feel free to change the name of the RESOURCE GROUP NAME and the NAME OF THE AZURE CONTAINER INSTANCE.
PART #2:-
BELOW FOLLOWS PIPELINE VARIABLES CODE SNIPPET:-
######################
#DECLARE VARIABLES:-
######################
variables:
  Artifact: AM
  cpu: 2 
  memory: 3.5
  port: 9000

NOTE:-
Please feel free to change the values of the variables.
The entire YAML pipeline is build using Parameters and variables. No Values are Hardcoded.
PART #3:-
PIPELINE STAGES:-
There are 2 Stages in the Pipeline 1) When User selects "EXISTING" Pipeline Runtime Environment 2) When User selects "NEW" Pipeline Runtime Environment
Pipeline Stage gets SKIPPED or EXECUTED based on the User Choice
BELOW FOLLOWS PIPELINE STAGE "EXISTING" CODE SNIPPET:-
- stage: VALIDATE_SELECTION_EXISTING
  condition: |
     and(eq('${{ parameters.validateIfRGExists }}', 'Existing'), 
       eq(variables['build.sourceBranch'], 'refs/heads/main')) 
  jobs:
  - job: IF_RG_EXISTS_DEPLOY_SONARQUBE 
    displayName: IF RG EXISTS DEPLOY SONARQUBE
    steps:
    - task: AzureCLI@2
      displayName: CHECK RG AND DEPLOY SONARQUBE
      inputs:
        azureSubscription: ${{ parameters.ServiceConnection }}
        scriptType: ps
        scriptLocation: inlineScript
        inlineScript: |
          az --version
          az account set --subscription ${{ parameters.SubscriptionID }}
          az account show  
          $i = az group exists -n ${{ parameters.RGName }}
          if ($i -eq "true") {
          $j = az container list --query "[?contains(name, '${{ parameters.ACIName }}')].[provisioningState]" -o tsv
            if ($j -eq "Succeeded") {
            echo "##########################################################################################################################################"
            echo "${{ parameters.ACIName }} CONTAINER INSTANCE EXISTS IN THE ${{ parameters.RGName }} RESOURCE GROUP. CANNOT PROCEED WITH THE DEPLOYMENT"
            echo "##########################################################################################################################################"  
              }
            else {   
            az container create -g ${{ parameters.RGName }} --name ${{ parameters.ACIName }} --image sonarqube --ports $(port) --dns-name-label ${{ parameters.ACIName }} --cpu $(cpu) --memory $(memory)
              }
            }
          else {
          az group create -l ${{ parameters.RGLocation }} -n ${{ parameters.RGName }} 
          az container create -g ${{ parameters.RGName }} --name ${{ parameters.ACIName }} --image sonarqube --ports $(port) --dns-name-label ${{ parameters.ACIName }} --cpu $(cpu) --memory $(memory)    
            }

BELOW FOLLOWS PIPELINE STAGE "NEW" CODE SNIPPET:-
- stage: VALIDATE_SELECTION_NEW
  condition: |
     and(eq('${{ parameters.validateIfRGExists }}', 'New'), 
       eq(variables['build.sourceBranch'], 'refs/heads/main')) 
  jobs:
  - job: CREATE_RG_AND_DEPLOY_SONARQUBE 
    displayName: CREATE RG AND DEPLOY SONARQUBE
    steps:
    - task: AzureCLI@2
      displayName: CHECK RG AND DEPLOY SONARQUBE
      inputs:
        azureSubscription: ${{ parameters.ServiceConnection }}
        scriptType: ps
        scriptLocation: inlineScript
        inlineScript: |
          az --version
          az account set --subscription ${{ parameters.SubscriptionID }}
          az account show  
          $i = az group exists -n ${{ parameters.RGName }}
          if ($i -eq "true") { 
          $j = az container list --query "[?contains(name, '${{ parameters.ACIName }}')].[provisioningState]" -o tsv
            if ($j -eq "Succeeded") {
            echo "##########################################################################################################################################"
            echo "${{ parameters.ACIName }} CONTAINER INSTANCE EXISTS IN THE ${{ parameters.RGName }} RESOURCE GROUP. CANNOT PROCEED WITH THE DEPLOYMENT"
            echo "##########################################################################################################################################"  
              }
            else {   
            az container create -g ${{ parameters.RGName }} --name ${{ parameters.ACIName }} --image sonarqube --ports $(port) --dns-name-label ${{ parameters.ACIName }} --cpu $(cpu) --memory $(memory)
              }
            }             
          else {
          az group create -l ${{ parameters.RGLocation }} -n ${{ parameters.RGName }} 
          az container create -g ${{ parameters.RGName }} --name ${{ parameters.ACIName }} --image sonarqube --ports $(port) --dns-name-label ${{ parameters.ACIName }} --cpu $(cpu) --memory $(memory)    
            }

PART #4:-
PIPELINE STAGE VALIDATE_SELECTION_EXISTING CONDITIONS:-
## CONDITIONS APPLIED
1. When User selects "Existing" Pipeline Runtime Environment, it means that the mentioned RESOURCE GROUP (RG) Exists in the Subscription.
2. Pipeline when executed first validates if RG Exists.
3. If RG EXISTS, it will then validate if the mentioned Azure Container Instance (ACI), provided using Pipeline Runtime Variables exists. If ACI exists, No Action to be Taken. If ACI does not Exists, Deploy ACI with SonarQube Image
4. If RG DOES NOT EXISTS, it will first deploy RG and then ACI with SonarQube Image
PART #5:-
PIPELINE STAGE VALIDATE_SELECTION_NEW CONDITIONS:-
## CONDITIONS APPLIED
1. When User selects "New" Pipeline Runtime Environment, it means that the mentioned RESOURCE GROUP (RG) does not Exists in the Subscription.
2. Pipeline when executed first validates if RG Exists.
3. If RG EXISTS, it will then validate if the mentioned Azure Container Instance (ACI), provided using Pipeline Runtime Variables exists. If ACI exists, No Action to be Taken. If ACI does not Exists, Deploy ACI with SonarQube Image
4. If RG DOES NOT EXISTS, it will first deploy RG and then ACI with SonarQube Image
PART #6:-

VALIDATE THE YAML DEPLOYMENT WITH BELOW TEST CASES.

TEST CASE #1: SELECT "NEW" (There is No RG and ACI with SonarQube Image):-
Desired Output: RG + ACI with SonarQube Image Gets Deployed.
PIPELINE VARIABLES:
Image description
PIPELINE RESULTS:
Image description
Image description
SonarQube URL: http://am-poc-sonarqube-aci.westeurope.azurecontainer.io:9000/
Image description
TEST CASE #2: SELECT "EXISTING" (RG and ACI with SonarQube Image already Exists):-
Desired Output: No Action Performed.
PIPELINE VARIABLES:
Image description
PIPELINE RESULTS:
Image description
Image description
Image description
SonarQube URL: http://am-poc-sonarqube-aci.westeurope.azurecontainer.io:9000/
Image description
TEST CASE #3: SELECT "NEW" (RG and ACI with SonarQube Image already Exists):-
Desired Output: No Action Performed.
PIPELINE VARIABLES:
Image description
PIPELINE RESULTS:
Image description
Image description
Image description
SonarQube URL: http://am-poc-sonarqube-aci.westeurope.azurecontainer.io:9000/
Image description
TEST CASE #4: SELECT "EXISTING" (RG Exists but there is No ACI with SonarQube Image):-
Desired Output: ACI with SonarQube Image gets Deployed Only.
PIPELINE VARIABLES:
Image description
PIPELINE RESULTS:
Image description
Image description
Image description
SonarQube URL: http://am-poc-sonarqube-aci.westeurope.azurecontainer.io:9000/
Image description
TEST CASE #5: SELECT "NEW" (RG Exists but there is No ACI with SonarQube Image):-
Desired Output: ACI with SonarQube Image gets Deployed Only.
PIPELINE VARIABLES:
Image description
PIPELINE RESULTS:
Image description
Image description
Image description
SonarQube URL: http://am-poc-sonarqube-aci.westeurope.azurecontainer.io:9000/
Image description

Hope You Enjoyed the Session!!!

Stay Safe | Keep Learning | Spread Knowledge

24-apr-2022-devops__dynamic-deployment-of-sonarqube-in-aci's People

Contributors

arindam0310018 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

selvacent

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.