Azure Pipelines environments approvals - azure-devops

I have set up 2 environments and protected only one environment.
However pipeline run expect me to approve even before it starts.
I am assuming that Build and DevEnv deployment should happen un attended and should stop for QAEnv alone. Am I missing anything?

You need to add dependsOn: <environment> to your jobs. As it stands, it's trying to run all of the stages at once.
You also have all of those jobs within a single stage, which looks off to me.
You need to split them into multiple stages:
stages:
- stage: Build
jobs: ...
- stage: DEV
jobs: ...
- stage: QA
jobs: ...

Agree with Daniel Mann.
You could split the jobs into two stages (Dev stage and QA stage).
Here is an example:
stages:
- stage: Dev_Stage
jobs:
- deployment: DeployWeb
displayName: deploy Web App
pool:
vmImage: 'Ubuntu-latest'
environment: 'env1'
strategy:
runOnce:
deploy:
steps:
- script: echo Hello world
- stage: QA_Stage
jobs:
- deployment: DeployWeb
displayName: deploy Web App
pool:
vmImage: 'Ubuntu-latest'
environment: 'env2'
strategy:
runOnce:
deploy:
steps:
- script: echo Hello world
Result:
In this case, the stage1 has no check steps , the stage2 needs to be checked.
If you set the environment for the two stages separately, the two stages are independent of each other, they will not interfere with the other stage.
Hope this helps.

Related

deployment dependencies condition skipped azure pipeline

Created a pipeline. Have 2 stages. the stage job is deployment second stage has a condition derived from the first stage variable. the pipeline sample is given below
pool:
vmImage: 'ubuntu-latest'
stages:
- stage: Stage
jobs:
- deployment: Staging
displayName: Stage the WebApp
environment: stg
strategy:
runOnce:
deploy:
steps:
- bash: echo "##vso[task.setvariable variable=myStageOutputVar;isOutput=true]true"
env:
myVar: 'this is a stage output var'
name: printvar
- stage: Swap
dependsOn: Stage
condition: eq(dependencies.Stage.Staging.outputs['Staging.printvar.myStageOutputVar'], 'true')
variables:
myVarfromStage: $[ stageDependencies.Stage.Staging.outputs['Staging.printvar.myStageOutputVar'] ]
jobs:
- deployment: Production
displayName: Swap to production
environment: adt
strategy:
runOnce:
deploy:
steps:
- script: echo $(myVarfromStage)
the Swap - stage is getting skipped every time . Any changes need to do it in the condition ?
I suppose that you could look into this doc for the expression.
You will need to edit your condition as below. Notice Staging.Staging
- stage: Swap
dependsOn: Stage1
condition: eq(dependencies.Stage1.outputs['Staging.Staging.printvar.myStageOutputVar'], 'true')

Azure DevOps yaml file recommendation when I have multi build and multi environments

I am working on implementing a YAML pipeline to deploy application to the four different environments. Unfortunately I need to perform build every time specific to environment.
I have four front end portals and 8 microservices (.NET web API's) related code in a single repo.
For example I have a angular code for one of the front end and I need to execute npm run dev:build, npm run dev:qa ...npm run prod:prod to generate artifacts per environment and then I can deploy on the respective environment.
With environment branching strategy I can deploy easily mean maintaining branches like Dev, QA, UAT and PROD. By creating pull requests I can merge and deploy to the respective environment. But I don't want this complex branching strategy.
So I decided to create a single yaml, and used parameters and conditionals to deploy on respective environment with one click like below
Azure DevOps yaml not picking up the parameters
Since I have 4 frontend portals and 8 microservices its difficult to run pipeline with one click manually. So i decided to add triggers for Dev and QA at least and using Release/* branch I want to deploy the code to higher environments.
But when I use triggers and parameters pipeline is not running. When I include parameters I guess Trigger is None.
trigger:
- dev
parameters:
- name: environment
displayName: Environment to choose
type: string
values:
- UAT
variables:
vmImageName: 'windows-2019'
stages:
- stage: DevBuildStage
dependsOn: []
displayName: Dev_Build
jobs:
- job: Build_Job
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: PowerShell#2
displayName: command1
inputs:
targetType: 'inline'
script: |
Write-Host "This is Dev build"
- stage: UATBuildStage
condition: and(succeeded(), eq('${{ parameters.environment }}', 'UAT'))
dependsOn: []
displayName: UAT_Build
jobs:
- job: Build_Job
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: PowerShell#2
displayName: command1
inputs:
targetType: 'inline'
script: |
Write-Host "This is UAT build"
- stage: DeploytoDev
displayName: Deploy to Dev
dependsOn: DevBuildStage
condition: succeeded()
jobs:
- deployment: Deploy
displayName: Deploy
environment: 'development'
variables:
- group: dev
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
Write-Host "Deploy to Dev"
- stage: DeploytoUAT
displayName: Deploy to UAT
dependsOn: UATBuildStage
condition: succeeded()
jobs:
- deployment: Deploy
displayName: Deploy
environment: 'uat'
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
# Write your PowerShell commands here.
Write-Host "Deploy to UAT"
This is how I want to run Dev build and deploy automatically but UAT I can run whenever I need manually using parameters. This way I want to reduce the number of branches.
The simple branching strategy like Dev(Dev environement) --- Release/*(QA, UAT and PROD) --- main (Just to maintain the stable code)
Please help me on this issue.
Finally I have decided to use environmental branching strategy means a specific branch to deploy to the environment ...Dev(Branch) --- DEV(environment). Also I have used templates to reuse step.

azure devops yml environment checkout

I'm trying to implement Approval gates in an Azure Devops YML pipeline and following these steps. An issue I am seeing is that since configuring the pipeline to use a blank environment (so it is only used for the approval process as per the article) when the pipeline runs I am no longer seeing the "Checkout" step where my repo contents are loaded into the virtual machine runner used by the pipeline. How can I use an environment and load in the repo contents?
This code has the checkout step:
trigger: none
pool:
vmImage: ubuntu-latest
stages:
- stage: tempname
jobs:
- job: tempname
steps:
- bash: |
echo hello world
This code does not have the checkout step:
trigger: none
pool:
vmImage: ubuntu-latest
stages:
- stage: tempname
jobs:
- deployment: tempname
environment: test
strategy:
runOnce:
deploy:
steps:
- bash: |
echo hello world
checkout is a step, so should go in the steps section - see https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/steps-checkout?view=azure-pipelines.
Example of how it might look:
trigger: none
pool:
vmImage: ubuntu-latest
stages:
- stage: tempname
jobs:
- deployment: tempname
environment: test
strategy:
runOnce:
deploy:
steps:
- checkout: self # Use none to avoid checking out code.
- bash: |
echo hello world

Azure Pipelines Deploy Stage Failing without Error

The deploy stage of the pipeline fails without error after build stage completes successfully.
Enabling system diagnostics does not give in any additional information (see the screenshot below).
The following pipelines yaml file was used:
trigger:
- master
resources:
- repo: self
variables:
vmImageName: 'ubuntu-latest'
stages:
- stage: Build
displayName: Build stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: CmdLine#2
inputs:
script: |
ls -la
- stage: Deploy
displayName: Deploy Notebook Instance Stage
dependsOn: Build
jobs:
- deployment: Deploy
displayName: Deploy
pool:
vmImage: $(vmImageName)
environment: 'myenv.default'
strategy:
runOnce:
deploy:
steps:
- task: CmdLine#2
inputs:
script: echo Some debug text!
I used your script and I change only environment as I don't have myenv.default and all is fine.
Please double check your environment setting.

Deselect Stages By Default

In Azure Devops multistage YAML pipeline we got multiple environments.
In stages to run normally we do a build and deploy only in QA, so we need to deselect each stage manually. By default all stages are selected is it possible to have exact opposite, where all stages are deselected by default???
trigger: none
pr: none
stages:
- stage: 'Build'
jobs:
- deployment: 'Build'
pool:
name: Default
# testing
environment: INT
strategy:
runOnce:
deploy:
steps:
- checkout: none
- powershell: |
echo "Hello Testing"
Start-Sleep -Seconds 10
- stage: 'Sandbox'
jobs:
- job: 'Sandbox'
pool:
name: Default
steps:
- checkout: none
# testing
- powershell: |
echo "Hello Testing"
- stage: 'Test'
jobs:
- job: 'DEV'
pool:
name: Default
steps:
- checkout: none
- powershell: |
echo "Hello Testing"
- stage: 'QA'
dependsOn: ['Test','Test1','Test2']
jobs:
- job: 'QA'
pool:
name: Default
steps:
- checkout: none
# Testing
- powershell: |
echo "Hello Testing"
I am afraid that there is no UI (like stage to run) method that can meet your needs.
You could try to add parameters to your Yaml Sample.
Here is an example:
trigger: none
pr: none
parameters:
- name: stageTest
displayName: Run Stage test
type: boolean
default: false
- name: stageBuild
displayName: Run Stage build
type: boolean
default: false
stages:
- ${{ if eq(parameters.stageBuild, true) }}:
- stage: 'Build'
jobs:
- deployment: 'Build'
pool:
name: Default
environment: INT
strategy:
runOnce:
deploy:
steps:
- checkout: none
- powershell: |
echo "Hello Testing"
Start-Sleep -Seconds 10
- ${{ if eq(parameters.stageTest, true) }}:
- stage: Test
dependsOn: []
jobs:
- job: B1
steps:
- script: echo "B1"
The parameters are used to determine whether to run these stages. You could add expressions before the stage to check if the parameter value could meet expression.
The default value is false. This means that the stage will not run by default.
Here is the result:
You can select the stage you need to run by clicking the selection box.
Update
Workaround has some limitations. When the select stage has depenencies, you need to select all dependent stages to run.
For example:
- stage: 'QA'
dependsOn: ['Test','Test1','Test2']
On the other hand, I have created a suggestion ticket to report this feature request. Here is the suggestion ticket link: Pipeline Deselect Stages By Default You could vote and add comment in it .
I've used this solution to build a nuget-package, and:
always push packages from master
conditionally push packages from other branches
Using GitVersion ensures that the packages from other branches get prerelease version numbers, e.g. 2.2.12-my-branch-name.3 or 2.2.12-PullRequest7803.4. The main branch simply gets 2.2.12, so the master branch is recognized as a "regular" version.
The reason I'm repeating the answer above, is that I chose to make the stage conditional instead of using an if:
trigger:
- master
parameters:
- name: pushPackage
displayName: Push the NuGet package
type: boolean
default: false
stages:
- stage: Build
jobs:
- job: DoBuild
steps:
- script: echo "I'm building a NuGet package (versioned with GitVersion)"
- stage: Push
condition: and(succeeded('build'), or(eq('${{ parameters.pushPackage }}', true), eq(variables['build.sourceBranch'], 'refs/heads/master')))
jobs:
- job: DoPush
steps:
- script: echo "I'm pushing the NuGet package"
Like the other answer, this results in a dialog:
But what's different from the (equally valid) solution with '${{ if }}', is that the stage is always shown (even if it's skipped):