AzureDevOps-透過Command Merge

環境
  • AzureDevOps
  • Jenkins
  • git

問題
    目前的專案每天中午會透過手動的方式,固定發pull request將develop分支併到relesae。由於是常態性質,大部份是不需要review,所以一來浪費時間,二來有時一忙就會忘了作=.=。

解決方式

經測試後,可以透過command及Jenkins的建置工作來完成每日自動合併,以下為測試後的2種方式 (使用jenkins pipeline)

1.使用merge的方式
withCredentials([usernamePassword(credentialsId: '0f57466e-8dc0-444b-a88a-d56d663d3378', usernameVariable: 'username', passwordVariable: 'password')]){
    bat "git pull https://${username}:${password}@xxx.visualstudio.com/KimGitLab/_git/KimGitLab"
    bat "git merge origin/develop"
    bat "git push https://${username}:${password}@xxx.visualstudio.com/KimGitLab/_git/KimGitLab"
}
  • 透過withCredentials取得在Jenins上已儲存的AzureDevOps token
  • pull回來後,merge orgin/devlop到本地的release 在pull到遠端

2.使用Pull Request的方式
  • 前面第1點雖然可以每天合併分支,但缺點是在release分支上無法看出develop每次併回來的commit有哪些。而pull request則可以。

withCredentials([usernamePassword(credentialsId: '0f57466e-8dc0-444b-a88a-d56d663d3378', usernameVariable: 'username', passwordVariable: 'password')]){
     bat "vsts login --token ${password} --instance https://xxx.visualstudio.com/"
     bat "vsts code pr create -s develop -t release --auto-complete -r KimGitLab  -p KimGitLab"
}                
  • 透過token先登入
  • 建立pull request 並自動完成

2.1 第2點不使用--auto-complete方式

使用--auto-complete方式,雖然會自動完成此PR,但會發一筆完成通知給開單的帳號。若不想通知可以使用以下方式,用取得建立的PR編號,再發一次Complete Command。
withCredentials([usernamePassword(credentialsId: '0f57466e-8dc0-444b-a88a-d56d663d3378', usernameVariable: 'username', passwordVariable: 'password')]){

powershell """
vsts login --token ${password} --instance "https://xxx.visualstudio.com/"
\$prId=vsts code pr create -s develop -t release -p KimGitLab -r KimGitLab --instance "https://xxx.visualstudio.com/" --query pullRequestId
vsts code pr complete --id \$prId --instance "https://xxx.visualstudio.com/"

"""
}   
  • 透過token先登入
  • 建立pull request 並透過參數--query pullRequestId取得PR編號,放在變數 $prId
  • 透過pr complete 完成此PR

這個網誌中的熱門文章

IIS 設定只允許特定IP進入