0

Can we have both build and release in the same YAML script in Azure DevOps ? If yes, Can someone help me with the sample script. For the release part, we are deploying to multiple environments.

1 Answers1

0

We have multiple way to initiate build and release our configurations via Azure DevOps.

Firstly, we’ll be looking at building CI and CD in two different yml formats and create two pipelines to automate our tasks. For that we’ll be using Resources in YAML Pipelines.

Now we will be creating build pipeline by creating new DevOps project and new repository, can add below yaml file as build-pipeline.yml

trigger:
  branches:
    include:
    - master
  paths:
    exclude:
    - build-pipeline.yml
    - release-pipeline.yml

variables:
  vmImageName: 'ubuntu-latest'

jobs:  
- job: Build
  pool:
    vmImage: $(vmImageName)
  steps:
  - script: | 
      echo 'do some unit test'
    displayName: 'unit test'
  - script: | 
      echo 'compile application'
    displayName: 'compile'
  - task: ArchiveFiles@2
    displayName: 'Archive files'
    inputs:
      rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
      includeRootFolder: false
      archiveType: zip
      archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      replaceExistingArchive: true
  - upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
    artifact: drop

Similarly, we create a release pipeline and below is the yml format:

# Explicitly set none for repositry trigger
trigger:
- none

resources:
  pipelines:
  - pipeline: myappbuild  # Name of the pipeline resource
    source: myapp-build-pipeline # Name of the triggering pipeline
    trigger: 
      branches:
      - master

variables:
  vmImageName: 'ubuntu-latest'

jobs:
- deployment: Deploy
  displayName: Deploy
  environment: dev
  pool: 
    vmImage: $(vmImageName)
  strategy:
    runOnce:
      deploy:
        steps:          
        - download: myappbuild
          artifact: drop  
        - task: ExtractFiles@1
          inputs:
            archiveFilePatterns: '$(PIPELINE.WORKSPACE)/myappbuild/drop/$(resources.pipeline.myappbuild.runID).zip'
            destinationFolder: '$(agent.builddirectory)'
            cleanDestinationFolder: false
        - script: |  
            cat $(agent.builddirectory)/greatcode.txt

Here we can understand that build pipeline runs first, then release pipeline runs and these pipelines can be linked with multiple deployments.

Also, we have more information on few useful blogs such as c-sharpcorner, adamtheautomater. Thanks to the bloggers.

SaiKarri-MT
  • 1,174
  • 1
  • 3
  • 8