Reputation: 59
Hello Stack Overflow community, I'm encountering an issue with my Azure DevOps YAML pipeline configuration. Specifically, the DeployCode stage is skipping unexpectedly, even though there are no explicit conditions set for it. I've reviewed my YAML code carefully and couldn't find any obvious reasons for this behavior.
Here's a simplified version of my YAML code:
trigger:
branches:
include:
- develop
- master
- feature/x
schedules:
- cron: '30 * * * *'
displayName: Every hour at 30th minute...
branches:
include:
- develop
- master
- feature/x
always: true
pr: none
pool:
vmImage: ubuntu-20.04
variables:
BRANCH_NAME: ${{ replace(variables['Build.SourceBranch'], 'refs/heads/', '') }}
${{ if in(variables['BRANCH_NAME'], 'develop', 'feature/x') }}:
TF_ENVIRONMENT: test
${{ elseif eq(variables['BRANCH_NAME'], 'x') }}:
TF_ENVIRONMENT: x
RESOURCE_GROUP: '${TF_ENVIRONMENT}x-rg'
STORAGE_ACCOUNT_NAME: '${TF_ENVIRONMENT}x'
CDN_ENDPOINT_NAME: '${TF_ENVIRONMENT}x'
CDN_PROFILE_NAME: '${TF_ENVIRONMENT}x-profile'
AZURE_SUBSCRIPTION: 'Your-Subscription-Name-Here'
AZURE_SUBSCRIPTION_ID: 'Your-Subscription-ID-Here'
stages:
- stage: DiagnosticJob
jobs:
- job: DiagnosticJob
steps:
- script: |
echo "Build Reason: $BUILD_REASON"
displayName: Print Build Reason
- stage: DeployInfrastructure
condition: ne(variables['Build.Reason'], 'Schedule')
jobs:
- job: ConditionalInfrastructureDeployment
steps:
- script: |
echo "BRANCH_NAME: $BRANCH_NAME"
echo "TF_ENVIRONMENT: $TF_ENVIRONMENT"
echo "AZURE_SUBSCRIPTION: $AZURE_SUBSCRIPTION"
echo "AZURE_SUBSCRIPTION_ID: $AZURE_SUBSCRIPTION_ID"
displayName: Print Expression Result
#infrastructure deployment steps here
- stage: DeployCode
jobs:
- job: DeployWebsite
steps:
# code deployment steps here
The DeployInfrastructure stage is conditionally skipped based on the Build.Reason, but there are no conditions or dependencies defined for the DeployCode stage. According to Azure DevOps documentation, it should execute unconditionally as long as the previous stages have completed successfully.
I've also checked the build and deployment logs for any errors or issues, but everything seems to be in order.
Could someone please help me understand why the DeployCode stage is being skipped unexpectedly? Any insights or suggestions for troubleshooting would be greatly appreciated.
Thank you!
Update:
To provide further clarification, the "DeployCode" stage is skipped only when the "DeployInfrastructure" stage is also skipped, and this occurs specifically when the build status is set to "Scheduled."
Upvotes: 1
Views: 112
Reputation: 40533
As it is written in documentation here:
You can specify the conditions under which each stage, job, or step runs. By default, a job or stage runs if it doesn't depend on any other job or stage, or if all of the jobs or stages it depends on have completed and succeeded. This includes not only direct dependencies, but their dependencies as well, computed recursively. By default, a step runs if nothing in its job has failed yet and the step immediately preceding it has finished. You can customize this behavior by forcing a stage, job, or step to run even if a previous dependency fails or by specifying a custom condition.
In you case it happens because previous step was not succeeded
You should change it to
stages:
- stage: DiagnosticJob
jobs:
- job: DiagnosticJob
steps:
- script: |
echo "Build Reason: $BUILD_REASON"
displayName: Print Build Reason
- stage: DeployInfrastructure
condition: ne(variables['Build.Reason'], 'Schedule')
jobs:
- job: ConditionalInfrastructureDeployment
steps:
- script: |
echo "BRANCH_NAME: $BRANCH_NAME"
echo "TF_ENVIRONMENT: $TF_ENVIRONMENT"
echo "AZURE_SUBSCRIPTION: $AZURE_SUBSCRIPTION"
echo "AZURE_SUBSCRIPTION_ID: $AZURE_SUBSCRIPTION_ID"
displayName: Print Expression Result
#infrastructure deployment steps here
- stage: DeployCode
dependsOn:
- DiagnosticJob
- DeployInfrastructure
condition: or(and(succeeded(), ne(variables['Build.Reason'], 'Schedule')), and(eq(variables['Build.Reason'], 'Schedule'), dependencies.DiagnosticJob.result == 'Succeeded'))
jobs:
- job: DeployWebsite
steps:
# code
The new condition works that way that if the pipeline was scheduled then it checks result of the first job, and if not than expects that all jobs finished successfully.
Upvotes: 2