,

Azure DevOps – Different Use Case (Part-6)

Posted by

Artifacts in Azure DevOps can be used in various scenarios to facilitate different aspects of the CI/CD pipeline. Here are some additional use cases that demonstrate the versatility of artifacts:

Use Case 1: Packaging and Distributing Libraries

Scenario: You have a library that other projects depend on. You want to build, test, package, and distribute this library as a NuGet package.

Steps:

  1. Build Pipeline:
    • Compile the library, run tests, and create a NuGet package.
    • Example (YAML):
pool:
  vmImage: 'ubuntu-latest'
steps:
  - task: UseDotNet@2
    inputs:
      packageType: 'sdk'
      version: '5.x'
  - script: dotnet build
    displayName: 'Build the library'
  - script: dotnet test
    displayName: 'Run unit tests'
  - script: dotnet pack -o $(Build.ArtifactStagingDirectory)
    displayName: 'Package the library'
  - task: PublishBuildArtifacts@1
    inputs:
      PathtoPublish: '$(Build.ArtifactStagingDirectory)'
      ArtifactName: 'nuget'

2.Release Pipeline:

  • Publish the NuGet package to an internal NuGet feed or an external repository.
  • Example (YAML):
stages:
  - stage: Publish
    jobs:
      - job: PublishJob
        steps:
          - download: current
            artifact: nuget
          - task: NuGetCommand@2
            inputs:
              command: 'push'
              packagesToPush: '$(Pipeline.Workspace)/nuget/*.nupkg'
              publishVstsFeed: 'YourFeedName'

Use Case 2: Building and Deploying Docker Images

Scenario: You want to containerize your application and deploy it to a Kubernetes cluster.

Steps:

  1. Build Pipeline:
    • Build the Docker image and push it to a container registry.
    • Example (YAML)
pool:
  vmImage: 'ubuntu-latest'
steps:
  - task: Docker@2
    inputs:
      containerRegistry: 'YourContainerRegistryConnection'
      repository: 'your-image-name'
      command: 'buildAndPush'
      Dockerfile: '**/Dockerfile'
      tags: '$(Build.BuildId)'

2.Release Pipeline:

  • Deploy the Docker image to a Kubernetes cluster.
  • Example (YAML)
stages:
  - stage: Deploy
    jobs:
      - job: DeployJob
        steps:
          - task: Kubernetes@1
            inputs:
              connectionType: 'Azure Resource Manager'
              azureSubscriptionEndpoint: 'YourSubscription'
              azureResourceGroup: 'YourResourceGroup'
              kubernetesCluster: 'YourCluster'
              namespace: 'default'
              command: 'apply'
              useConfigurationFile: true
              configuration: '$(Pipeline.Workspace)/k8s/deployment.yaml'

Use Case 3: Infrastructure as Code

Scenario: You want to manage your infrastructure using Terraform and ensure that the infrastructure changes go through the same CI/CD process as application code.

Steps:

  1. Build Pipeline:
    • Validate and plan Terraform scripts.
    • Example (YAML):
pool:
  vmImage: 'ubuntu-latest'
steps:
  - task: TerraformInstaller@0
    inputs:
      terraformVersion: '0.14.9'
  - script: terraform init
    displayName: 'Initialize Terraform'
  - script: terraform plan -out=plan.tfplan
    displayName: 'Plan Terraform'
  - task: PublishBuildArtifacts@1
    inputs:
      PathtoPublish: 'plan.tfplan'
      ArtifactName: 'terraform-plan'

2.Release Pipeline:

  • Apply the Terraform plan to the desired environment.
  • Example (YAML)
stages:
  - stage: Apply
    jobs:
      - job: ApplyJob
        steps:
          - download: current
            artifact: terraform-plan
          - script: terraform apply plan.tfplan
            displayName: 'Apply Terraform Plan'

Use Case 4: Static Website Deployment

Scenario: You have a static website that you want to build and deploy to an Azure Storage account as a static website.

Steps:

  1. Build Pipeline:
    • Build the static website using a static site generator like Jekyll, Hugo, or Gatsby.
    • Example (YAML):
pool:
  vmImage: 'ubuntu-latest'
steps:
  - script: npm install
    displayName: 'Install dependencies'
  - script: npm run build
    displayName: 'Build static website'
  - task: PublishBuildArtifacts@1
    inputs:
      PathtoPublish: 'public'  # Output directory of the static site generator
      ArtifactName: 'static-site'

2.Release Pipeline:

  • Deploy the static site to Azure Storage.
  • Example (YAML):
stages:
  - stage: Deploy
    jobs:
      - job: DeployJob
        steps:
          - download: current
            artifact: static-site
          - task: AzureCLI@2
            inputs:
              azureSubscription: 'YourSubscription'
              scriptType: 'bash'
              scriptLocation: 'inlineScript'
              inlineScript: |
                az storage blob upload-batch \
                  --destination '$web' \
                  --source '$(Pipeline.Workspace)/static-site' \
                  --account-name 'YourStorageAccount'

Use Case 5: Multi-Platform Build and Release

Scenario: You have an application that needs to be built for multiple platforms (e.g., Windows, Linux, macOS) and deployed to different environments.

Steps:

  1. Build Pipeline:
    • Define jobs for each platform to build the application.
    • Example (YAML):
pool:
  vmImage: 'ubuntu-latest'
jobs:
  - job: BuildWindows
    pool:
      vmImage: 'windows-latest'
    steps:
      - script: build-windows.sh
        displayName: 'Build for Windows'
      - task: PublishBuildArtifacts@1
        inputs:
          PathtoPublish: '$(Build.ArtifactStagingDirectory)/windows'
          ArtifactName: 'windows'

  - job: BuildLinux
    pool:
      vmImage: 'ubuntu-latest'
    steps:
      - script: build-linux.sh
        displayName: 'Build for Linux'
      - task: PublishBuildArtifacts@1
        inputs:
          PathtoPublish: '$(Build.ArtifactStagingDirectory)/linux'
          ArtifactName: 'linux'

  - job: BuildMacOS
    pool:
      vmImage: 'macos-latest'
    steps:
      - script: build-macos.sh
        displayName: 'Build for macOS'
      - task: PublishBuildArtifacts@1
        inputs:
          PathtoPublish: '$(Build.ArtifactStagingDirectory)/macos'
          ArtifactName: 'macos'

2.Release Pipeline:

  • Deploy the artifacts to their respective environments.
  • Example (YAML):
stages:
  - stage: DeployWindows
    jobs:
      - job: DeployWindowsJob
        steps:
          - download: current
            artifact: windows
          - script: deploy-windows.sh
            displayName: 'Deploy to Windows'

  - stage: DeployLinux
    jobs:
      - job: DeployLinuxJob
        steps:
          - download: current
            artifact: linux
          - script: deploy-linux.sh
            displayName: 'Deploy to Linux'

  - stage: DeployMacOS
    jobs:
      - job: DeployMacOSJob
        steps:
          - download: current
            artifact: macos
          - script: deploy-macos.sh
            displayName: 'Deploy to macOS'

Summary

Artifacts in Azure DevOps are versatile and can be used in various scenarios to manage the outputs of your build processes and ensure consistent, reliable deployments across different environments. By leveraging artifacts, you can streamline the CI/CD pipeline, facilitate collaboration, and maintain high-quality software releases.

guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x