Azure pipeline 配置根據條件執行腳本
Intro
我的應用通過 azure pipeline 來做持續集成,之前已經介紹了根據不同分支去打包不同的package,具體的就不再這里詳細介紹了,可以參考 持續集成之nuget進階,nuget 包可以做到根據不同的分支來
發布不同的包,那么我的應用一定也可以做到不同的分支發布不同 tag 的 docker 鏡像,最后通過 azure pipeline 內置的 Condition 來做判斷,可以加一些條件腳本在滿足特定條件下才執行的腳本再加上變量控制,就可以比較好的實現根據分支策略來發布不同 tag 的 docker 鏡像了
Solution
來看一下修改之后的 azure-pipelines.yaml
示例配置吧:
steps:
# ...
- script: docker push $(latestImageName)
displayName: 'Push latest image'
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/dev'))
- script: docker push $(stableImageName)
displayName: 'Push stable image'
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
- task: SSH@0
displayName: 'Run shell inline on remote machine'
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/dev'))
inputs:
sshEndpoint: 'weihanli-vm'
runOptions: inline
inline: |
kubectl set image deployment/activityreservation activityreservation=$(imageName) --record=true
當前面的 step 運行成功並且是 master 分支的 build 時,發布 tag 為 stable 的 docker 鏡像,如果是 dev 分支則發布 tag 為 latest 的 docker 鏡像,並且僅當當前分支為 dev 時才執行部署操作。
完整配置可以在 Github
上獲取
CI 執行過程
上圖是一個 dev 分支的 build,從上面的截圖可以看到,只有 master 分支才執行的 step,沒有被執行,直接跳過了