最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to handle scheduled build and scheduled release in seperate yml pipeline in azure devops - Stack Overflow

programmeradmin1浏览0评论

we have a build pipeline and a release pipeline separately both in yaml, azure devops. we have a scheduled build every day and scheduled release every day. currently, what happens since the scheduled release takes the latest release assuming it was from the scheduled build and deploy. but, sometimes that may not be the case the latest build may be from different branch and the release end up using wrong build for the deployment. to control this my plan is to tag the build with build tag with predefined naming convention and then during the scheduled release I can use the build tag and get the runId to download the artifacts and deploy. My problem is I have to Handle 3 scenario's

  1. Handle Scheduled Release, should pick the scheduled build.
  2. Handle Manual release, where user choose the build id.
  3. Handle Manual release, where user doesnt choose any specific build where it need to pick the latest.

currently I am able to tag the build. but during release, its by default downloading the latest artifacts and started using unless we choose the build id manually. however, I added download as false, still its using the latest build. I tried hardcoding in the release artifacts as you see below, Its downloading the artifact but not using them like it only takes the latest. I am adding the build and release yaml.

Is my approach right? is there any other better way to handle the requirement?

Build Pipeline

trigger:
- none

schedules:
- cron: '*/5 * * * *'  # Runs every 5 minutes for testing, otherwise runs every 24 hours
  displayName: Daily midnight build
  branches:
    include:
    - feature/ps/schedule_change
  always: true

pool:
  vmImage: 'ubuntu-latest'

variables:
  buildTag: ''

steps:
- script: |
    date=$(date +%Y%m%d)
    tag="scheduled-build-$date-$(Build.BuildId)"
    echo "##vso[build.addbuildtag]$tag"
    echo "##vso[task.setvariable variable=buildTag]$tag"
  displayName: 'Tag build with date and ID'

Release Pipeline

trigger: none


variables:
- name: isScheduledRelease
  value: false
- name: buildTag
  value: ''
- name: selectedBuildId
  value: ''
  
resources:
  pipelines:
  - pipeline: apipipeline
    source: Build-API
    trigger:
      branches:
        include:
        - develop
schedules:
- cron: "0 11 * * *"
  displayName: Daily
  branches:
    include:
    - develop
    - main
    - feature/ps/schedule_change
  always: true

pool:
  vmImage: 'ubuntu-latest'

stages:
  - stage: ScheduleChange
    displayName: sample
    condition: or(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Build.Reason'], 'Manual')) 
    dependsOn: 
    jobs:
    - deployment: api
      condition:
      displayName:  API Deployment
      environment: 'Sample'
      variables:
      - group: Sample
  
      strategy:
        runOnce:
          deploy:
            steps:
              - download: none
              - task: PowerShell@2
                inputs:
                  targetType: 'inline'
                  script: |
                    if ($env:BUILD_REASON -eq "Schedule") {
                       Write-Host "inside schedule"
                       $date = Get-Date -Format "yyyyMMdd"
                       $tag = "scheduled-build-api-$date"
                       Write-Host "##vso[task.setvariable variable=buildTag;isOutput=true]$tag"
                       Write-Host "##vso[task.setvariable variable=isScheduledRelease;isOutput=true]true"
                       Write-Host "build tag is $tag"
                       Write-Host "build reason schedule"
                    } else ($env:BUILD_REASON -eq "Manual" -and $env:BUILD_BUILDID) {
                       Write-Host "##vso[task.setvariable variable=selectedBuildId;isOutput=true]$env:BUILD_BUILDID"
                       Write-Host "##vso[task.setvariable variable=isScheduledRelease;isOutput=true]false"
                       Write-Host "build tag is $env:BUILD_BUILDID"
                       Write-Host "build reason manual with build id"
                    } 
                    
                displayName: 'Check if release is scheduled'
              - checkout: self

              - task: PowerShell@2
                inputs:
                  targetType: 'inline'
                  script: |
                    if ($env:buildTag -eq '') {
                      $latestBuild = (Invoke-RestMethod -Uri ";\$top=1&api-version=6.0" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}).value[0].id
                      Write-Host "##vso[task.setvariable variable=latestBuildId;isOutput=true]$latestBuild"
                      Write-Host "latest build is $latestBuild"
                    }
                displayName: 'Get Latest Build ID'

              - task: DownloadPipelineArtifact@2
                inputs:
                  buildType: 'specific'
                  project: 'proj'
                  pipeline: 'pipelineid'
                  runVersion: 'specific'
                  runId: '123412' #${{ coalesce(variables['buildTag'], variables['selectedBuildId'], variables['latestBuildId']) }}
                  targetPath: '$(Pipeline.Workspace)/apipipeline'

we have a build pipeline and a release pipeline separately both in yaml, azure devops. we have a scheduled build every day and scheduled release every day. currently, what happens since the scheduled release takes the latest release assuming it was from the scheduled build and deploy. but, sometimes that may not be the case the latest build may be from different branch and the release end up using wrong build for the deployment. to control this my plan is to tag the build with build tag with predefined naming convention and then during the scheduled release I can use the build tag and get the runId to download the artifacts and deploy. My problem is I have to Handle 3 scenario's

  1. Handle Scheduled Release, should pick the scheduled build.
  2. Handle Manual release, where user choose the build id.
  3. Handle Manual release, where user doesnt choose any specific build where it need to pick the latest.

currently I am able to tag the build. but during release, its by default downloading the latest artifacts and started using unless we choose the build id manually. however, I added download as false, still its using the latest build. I tried hardcoding in the release artifacts as you see below, Its downloading the artifact but not using them like it only takes the latest. I am adding the build and release yaml.

Is my approach right? is there any other better way to handle the requirement?

Build Pipeline

trigger:
- none

schedules:
- cron: '*/5 * * * *'  # Runs every 5 minutes for testing, otherwise runs every 24 hours
  displayName: Daily midnight build
  branches:
    include:
    - feature/ps/schedule_change
  always: true

pool:
  vmImage: 'ubuntu-latest'

variables:
  buildTag: ''

steps:
- script: |
    date=$(date +%Y%m%d)
    tag="scheduled-build-$date-$(Build.BuildId)"
    echo "##vso[build.addbuildtag]$tag"
    echo "##vso[task.setvariable variable=buildTag]$tag"
  displayName: 'Tag build with date and ID'

Release Pipeline

trigger: none


variables:
- name: isScheduledRelease
  value: false
- name: buildTag
  value: ''
- name: selectedBuildId
  value: ''
  
resources:
  pipelines:
  - pipeline: apipipeline
    source: Build-API
    trigger:
      branches:
        include:
        - develop
schedules:
- cron: "0 11 * * *"
  displayName: Daily
  branches:
    include:
    - develop
    - main
    - feature/ps/schedule_change
  always: true

pool:
  vmImage: 'ubuntu-latest'

stages:
  - stage: ScheduleChange
    displayName: sample
    condition: or(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Build.Reason'], 'Manual')) 
    dependsOn: 
    jobs:
    - deployment: api
      condition:
      displayName:  API Deployment
      environment: 'Sample'
      variables:
      - group: Sample
  
      strategy:
        runOnce:
          deploy:
            steps:
              - download: none
              - task: PowerShell@2
                inputs:
                  targetType: 'inline'
                  script: |
                    if ($env:BUILD_REASON -eq "Schedule") {
                       Write-Host "inside schedule"
                       $date = Get-Date -Format "yyyyMMdd"
                       $tag = "scheduled-build-api-$date"
                       Write-Host "##vso[task.setvariable variable=buildTag;isOutput=true]$tag"
                       Write-Host "##vso[task.setvariable variable=isScheduledRelease;isOutput=true]true"
                       Write-Host "build tag is $tag"
                       Write-Host "build reason schedule"
                    } else ($env:BUILD_REASON -eq "Manual" -and $env:BUILD_BUILDID) {
                       Write-Host "##vso[task.setvariable variable=selectedBuildId;isOutput=true]$env:BUILD_BUILDID"
                       Write-Host "##vso[task.setvariable variable=isScheduledRelease;isOutput=true]false"
                       Write-Host "build tag is $env:BUILD_BUILDID"
                       Write-Host "build reason manual with build id"
                    } 
                    
                displayName: 'Check if release is scheduled'
              - checkout: self

              - task: PowerShell@2
                inputs:
                  targetType: 'inline'
                  script: |
                    if ($env:buildTag -eq '') {
                      $latestBuild = (Invoke-RestMethod -Uri "https://dev.azure/Org/Proj/_apis/build/builds?definitions=defId&\$top=1&api-version=6.0" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}).value[0].id
                      Write-Host "##vso[task.setvariable variable=latestBuildId;isOutput=true]$latestBuild"
                      Write-Host "latest build is $latestBuild"
                    }
                displayName: 'Get Latest Build ID'

              - task: DownloadPipelineArtifact@2
                inputs:
                  buildType: 'specific'
                  project: 'proj'
                  pipeline: 'pipelineid'
                  runVersion: 'specific'
                  runId: '123412' #${{ coalesce(variables['buildTag'], variables['selectedBuildId'], variables['latestBuildId']) }}
                  targetPath: '$(Pipeline.Workspace)/apipipeline'
Share Improve this question edited Mar 14 at 12:06 desertnaut 60.5k32 gold badges155 silver badges182 bronze badges asked Mar 13 at 13:37 prakashrajansakthivelprakashrajansakthivel 2,0423 gold badges39 silver badges73 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

what happens since the scheduled release takes the latest release assuming it was from the scheduled build and deploy. but, sometimes that may not be the case the latest build may be from different branch and the release end up using wrong build for the deployment.

Based on your description, my understanding is that your release is consuming artifact from your scheduled build. If so, why not add the scheduled build as a pipeline resource in your release?

resources:
  pipelines:
  - pipeline: BuildPipeline 
    source: BuildPipeline 
    trigger:
      branches:
        include:
        - {include all your target branches}

steps:
- download: BuildPipeline

You can add all the branches you mentioned "the latest build may be from different branch" as branch trigger filters.

When release is triggered manually, you can select the specific build version manually or use the default latest version. Click Run pipeline -> Resources -> Select your build pipeline -> Use selected run.

发布评论

评论列表(0)

  1. 暂无评论