Github和Azure DevOps的代碼同步


【前言】
Github和Azure DevOps都提供了Git代碼庫功能,那么有沒有辦法將兩邊的代碼庫進行同步呢,答案是肯定的。
這里的操作我都是用Azure DevOps的Pipelines功能來完成的,當然用Github的Actions應該也能達到類似的效果,其他小伙伴們不妨嘗試一下。
 
【從Azure DevOps到Github】
由於我個人平時習慣於用Azure DevOps存放代碼,所以這里就先講如何將你再Azure DevOps提交的代碼同步到Github倉庫
首先我們創建一個新的Pipeline,起名叫“Sync From Azure DevOps to GitHub”
 
因為這里是要同步Azure DevOps的代碼,所以Connect下選擇Azure Repo Git,不熟悉YAML的同學可以點擊下方的Use the classic editor,就會變成一個圖形化的設置界面
第二步,選擇你的代碼庫,這里我選的是Ant-Design-Blazor

第三步,選擇模板,這里圖片截不下了,往下拉選擇Starter pipeline

 

YAML內容如下,注意username里的‘@’要替換成‘%40’

關於如何編寫Pipeline YAML可以參考微軟的文檔 https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema
 1 # Starter pipeline
 2 # Start with a minimal pipeline that you can customize to build and deploy your code.
 3 # Add steps that build, run tests, deploy, and more:
 4 # https://aka.ms/yaml
 5 variables: # pipeline-level 
 6   branch: 'azure_branch'
 7  
 8 trigger:
 9 - master
10 pool:
11   vmImage: 'ubuntu-latest'
12 steps:
13 - script: |
14    git remote add github https://<username>:<password>@github.com/Brian-Ding/ant-design-blazor.git
15    git checkout -b $(branch)
16    git push -u github $(branch)
17    
18    
19   displayName: 'Command Line Script'

 

【從Github到Azure DevOps】

前面的操作都和之前類似,只是記得Connect那一步要選擇Github

 1 steps:
 2 - script: |
 3    mkdir sync
 4    cd sync
 5    git clone https://github.com/xxx/xxx.git
 6    cd <github project>
 7    git remote add azure https://<Azure DevOps Organization>:<token>@dev.azure.com/<Azure DevOps Organization>/<project>/_git/<repo.git> 8    
 9    git branch -D $(branch)
10    git checkout -b $(branch)
11        
12    git push -d azure $(branch)
13    git push -u azure $(branch) --force
14   displayName: 'Command Line Script'

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM