Skip to content

Instantly share code, notes, and snippets.

@chetanku
Last active May 13, 2024 15:54
Show Gist options
  • Save chetanku/c51526d7322989ba5a62f186df261cd3 to your computer and use it in GitHub Desktop.
Save chetanku/c51526d7322989ba5a62f186df261cd3 to your computer and use it in GitHub Desktop.
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
variables:
major: 2
minor: 0
stages:
# Versioning master branch builds
- stage:
displayName: Build_Master_Version_Number
condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')
jobs:
- job: Build_Master_Version_Number
variables:
patch: $[counter(variables['minor'], 0)]
steps:
- bash: |
echo "##vso[build.updatebuildnumber]$(major).$(minor).$(patch)"
name: SetMasterBuildName
# Versioning feature branch and PR builds
- stage:
displayName: Build_Branch_Version_Number
condition: ne(variables['Build.SourceBranch'], 'refs/heads/master')
jobs:
- job: Build_Branch_Version_Number
variables:
prpatch: $[counter(variables['system.pullrequest.pullrequestid'], 0)]
brpatch: $[counter(variables['build.sourcebranchname'], 0)]
steps:
- bash: |
echo "##vso[build.updatebuildnumber]$(major).$(minor)-PullRequest.$(prpatch)"
condition: eq(variables['Build.Reason'], 'PullRequest')
name: SetPRBuildName
- bash: |
echo "##vso[build.updatebuildnumber]$(major).$(minor)-$(Build.SourceBranchName).$(brpatch)"
condition: ne(variables['Build.Reason'], 'PullRequest')
name: SetBranchBuildName
# Stage for building your application
- stage: Build_Steps
displayName: Build_Steps
condition: always()
jobs:
- job: Build_Steps
displayName: Build_Steps
steps:
- script: echo Hello, world!
name: 'Run_a_one_line_script'
- script: |
echo Add other tasks to build, test, and deploy your project.
echo See https://aka.ms/yaml
name: 'Run_a_multi_line_cript'
@nickalbrecht
Copy link

Thanks for the blogpost on this. After trying to get my YAML work using pieces of this, I wanted to point out that you don't necessarily need dedicated stages for these. I found doing it that way slowed down the build immensely as it tried to spin up a new environment for each stage. Instead I tried to move each of these as close to an individual task as possible (Stage>Job>Step/Task). In the end, I have two stages (Versioning and Build), with the version related parts split up by job and task. You can further speed up the process by explicitly specifying that a repository is not needed with a - checkout: none statement immediately under each of your steps: lines to have it skip doing a full checkout each time. Hope this helps anyone else looking to do something similar :-)

@chetanku
Copy link
Author

Hey @nickalbrecht, This example was just a way to show versioning, and I didn't focus on structuring the yaml. Apologize for this. Glad you have a optimized way of doing it. Can you please share your yaml file as well ? Thanks

@nickalbrecht
Copy link

nickalbrecht commented Sep 30, 2020

No worries, just wanted to point out those tips in case they could be of use to anyone else. after writing that comment, I actually had to revert some of my work trying to move down to specific tasks/jobs. I can't specify variables: on a task it seems, only on a job. But jobs can apparently run in parallel, so I actually hit a situation where my build ran before the script to get build number. It's possible to specify a dependsOn:, but I don't think that supports an 'or' operator to specify one job OR another. So, I had to go back to separate stages so that I had a parent stage to use as a dependsOn:

My use it actually for .NET Core, but you can easily see the similarities
https://gist.github.com/nickalbrecht/51aa4a9ae2535ccc1b74110d1cf98d14

@githubabhinav1
Copy link

Thank You for this !! It is very helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment