I'm trying to use azure pipeline to build a tar.gz package on a linux build server.Iis there way in VSTS/Azure Pipeline code to increment default major:minor:patch based on source branch?
Example:
If source branch is Develop: major:minor(increment):patch If source branch is bugfix: major:minor:patch(increment) If source branch is main: major(increment):minor:patch
Please let me know your suggestions. Thank you
I'm trying to use azure pipeline to build a tar.gz package on a linux build server.Iis there way in VSTS/Azure Pipeline code to increment default major:minor:patch based on source branch?
Example:
If source branch is Develop: major:minor(increment):patch If source branch is bugfix: major:minor:patch(increment) If source branch is main: major(increment):minor:patch
Please let me know your suggestions. Thank you
Your requirement doesn't have a built in feature.
There is a counter expression in DevOps pipeline, it can auto increase a variable for each pipeline run, but this feature will forcibly increased every time the pipeline is run, and cannot be increased or stopped arbitrarily.
counter expression is for pipeline, and it has two parameters, prefix and seed. Seed is based on the prefix.
When the prefix is been set and run for the first time, the counter result will start from the feed value. But for the later run based on the same prefix, the counter result will ignore the feed value, it will be 'last time counter result + 1'
Since the counter expression has such limitation, I Write a more free design for you to manage the major, minor and patch.
The below is the YAML definition of the pipeline.
trigger:
- none
pool:
vmImage: ubuntu-latest
parameters: #The default value of increase major, minor, patch are all false. If you change these to true, the major, minor, patch will auto_increase by default.
- name: increase_major
default: false
type: boolean
values:
- true
- false
- name: increase_minor
default: false
type: boolean
values:
- true
- false
- name: increase_patch
default: false
type: boolean
values:
- true
- false
variables:
- name: Personal_Access_Token
value: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" #Put your personal Access Token here.
- ${{ if eq(parameters.increase_major,false)}}:
- name: increase_major
value: false
- ${{ if eq(parameters.increase_major,true)}}:
- name: increase_major
value: true
- ${{ if eq(parameters.increase_minor,false)}}:
- name: increase_minor
value: false
- ${{ if eq(parameters.increase_minor,true)}}:
- name: increase_minor
value: true
- ${{ if eq(parameters.increase_patch,false)}}:
- name: increase_patch
value: false
- ${{ if eq(parameters.increase_patch,true)}}:
- name: increase_patch
value: true
steps:
#===============================
#Put your pipeline steps here.
#===============================
# The below code will help you auto increase the major, minor, patch.
- task: PythonScript@0
displayName: Change the Major, Minor, Patch.
inputs:
scriptSource: inline
script: |
import json
import requests
#This part is defnition of variables
org_name = "BowmanCP"
project_name = "BowmanCP"
pipeline_definition_id = "376"
personal_access_token = "$(Personal_Access_Token)"
key = 'variables'
increase_major_control = '$(increase_major)'
increase_minor_control = '$(increase_minor)'
increase_patch_control = '$(increase_patch)'
major = "major"
minor = "minor"
patch = "patch"
#This part is logic for auto increase major, minor, patch
def get_value_of_specific_variable(json_content, key, var_name):
data = json.loads(json_content)
return data[key][var_name].get('value')
def auto_increase(json_content, key, var_name):
data = json.loads(json_content)
data[key][var_name]['value'] = str(int(get_value_of_specific_variable(json_content,key,var_name)) + 1)
return data
def get_pipeline_definition(org_name, project_name, pipeline_definition_id, personal_access_token):
url = "https://dev.azure.com/"+org_name+"/"+project_name+"/_apis/build/definitions/"+pipeline_definition_id+"?api-version=6.0"
payload={}
headers = {
'Authorization': 'Basic '+personal_access_token
}
response = requests.request("GET", url, headers=headers, data=payload)
json_content = response.text
return json_content
def update_pipeline_definition(org_name, project_name, pipeline_definition_id, json_content, key, var_name):
json_data = auto_increase(json_content, key, var_name)
url2 = "https://dev.azure.com/"+org_name+"/"+project_name+"/_apis/build/definitions/"+pipeline_definition_id+"?api-version=6.0"
payload2 = json.dumps(json_data)
headers2 = {
'Authorization': 'Basic '+personal_access_token,
'Content-Type': 'application/json'
}
response2 = requests.request("PUT", url2, headers=headers2, data=payload2)
#increase major
if increase_major_control == 'true':
json_data = update_pipeline_definition(org_name, project_name, pipeline_definition_id, get_pipeline_definition(org_name, project_name, pipeline_definition_id, personal_access_token), key, major)
if increase_minor_control == 'true':
json_data = update_pipeline_definition(org_name, project_name, pipeline_definition_id, get_pipeline_definition(org_name, project_name, pipeline_definition_id, personal_access_token), key, minor)
if increase_patch_control == 'true':
json_data = update_pipeline_definition(org_name, project_name, pipeline_definition_id, get_pipeline_definition(org_name, project_name, pipeline_definition_id, personal_access_token), key, patch)
When you run it, just click the checkbox will determine whether increase the variable of major, minor and patcH:
The variables are in this place:
Design Ideas:
Use Definitions - Get REST API to get the pipeline information.
Use code to achieve auto increase.
Use Definitions - Update REST API to change the pipeline definition(variables of the pipeline.).