Very often DevPos eng have to increment Build version in CI/CD:
I use Azure DevOps and TFS tools. You cat save variable between tasks by
-
echo "##vso[task.setvariable variable=BuildVersion; isOutput=false]$BuildVersionlocal" echo "##vso[build.updatebuildnumber]$BuildNumber"
But this is not enough when you increment version between builds. So you have to save changes in somewhere... I save in repo by git push. But for this I have to solve tasks:
-
For self-host agent or pool vmImage authorize in github
-
generate, save, sink, use PAT or sshKey/sshkeyPub
-
git push changes to REPO
-
So I have steps:
- Generate sshKey/sshkeyPub on Linux host
-
ssh-keygen -t ed25519 -C "Адрес электронной почты защищен от спам-ботов. Для просмотра адреса в вашем браузере должен быть включен Javascript." chmod =600 ~/.ssh/id_ed25519 cat /root/.ssh/id_ed25519.pub cat /root/.ssh/id_ed25519
-
- add to GitHud portal
- https://github.com/settings/ssh/new
- https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account
- Export and save Public key and PUB to Azure Library secure files
- https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/download-secure-file?view=azure-devops
- https://docs.microsoft.com/en-us/azure/devops/pipelines/library/?view=azure-devops
- Import to pool agent and use this for authorize in Repo Portal. Create this Yml
- create this YML,
-
trigger: branches: include: - master paths: exclude: - 'CI/Untrig/*' resources: - repo: self pool: vmImage: 'ubuntu-latest' variables: app: 'app' env: 'sandbox' tag: '$(Build.BuildId)' ######### GIT ################################################## GitBranch: 'master' GitLink: 'Адрес электронной почты защищен от спам-ботов. Для просмотра адреса в вашем браузере должен быть включен Javascript.:sergharkov/AKS.git' GitUserName: 'Azure_DevOps_Pipeline ' GitUserMAil: 'Адрес электронной почты защищен от спам-ботов. Для просмотра адреса в вашем браузере должен быть включен Javascript.' ######## Increment file ############################################ VarFileVersion: '$(Build.SourcesDirectory)/CI/UnTriger/CI-Version.txt' stages: - stage: Build displayName: Build image jobs: - job: Build displayName: Build steps: - task: DownloadSecureFile@1 inputs: secureFile: 'id_ed25519' - task: DownloadSecureFile@1 inputs: secureFile: 'id_ed25519.pub' - task: Bash@3 displayName: IncrementVar name: IncrementVar inputs: targetType: 'inline' script: | echo '#########################add public key ##########################' ls -l $(Agent.TempDirectory) mkdir -p ~/.ssh touch ~/.ssh/known_hosts ssh-keyscan github.com >> ~/.ssh/known_hosts cp -f $(Agent.TempDirectory)/id_ed25519 ~/.ssh/ cp -f $(Agent.TempDirectory)/id_ed25519.pub ~/.ssh/ chmod =600 ~/.ssh/id_ed25519 echo -e '\n##########################git pull################################' git remote set-url origin $(GitLink) git checkout $(GitBranch) git config --global user.email $(GitUserMail) git config --global user.name $(GitUserName) git pull origin $(GitBranch) echo "####################### Increment #################################" OldVersion=$(cat $(VarFileVersion) | grep -oP '<Version>\K.*(?=</Version>)') BuildNumber=$(($(echo $OldVersion | awk -F '.' '{print $3}')+1)) echo "OldVersion "$OldVersion echo "BuildNumber "$BuildNumber echo "DargVersion "$(DargVersion) echo "VarFileVersion "$(VarFileVersion) DargVersionLocal="$(DargVersion).$BuildNumber.$(env)" echo "<Version>$DargVersionLocal</Version>" > $(VarFileVersion) echo "######################## git push ################################" git add $(VarFileVersion) git commit -m "change file version.txt add Version $NewVersion" git status git push origin $(GitBranch) echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" ####change global variables################## echo "##vso[task.setvariable variable=DargVersion; isOutput=false]$DargVersionLocal" echo "##vso[build.updatebuildnumber]$BuildNumber"