Azure pipeline 配置根据条件执行脚本

Azure pipeline 配置根据条件执行脚本

Intro

个人应用经过 azure pipeline 来作持续集成,以前已经介绍了根据不一样分支去打包不一样的package,具体的就再也不这里详细介绍了,能够参考 持续集成之nuget进阶,nuget 包能够作到根据不一样的分支来
发布不一样的包,那么个人应用必定也能够作到不一样的分支发布不一样 tag 的 docker 镜像,最后经过 azure pipeline 内置的 Condition 来作判断,能够加一些条件脚本在知足特定条件下才执行的脚本再加上变量控制,就能够比较好的实现根据分支策略来发布不一样 tag 的 docker 镜像了git

Solution

来看一下修改以后的 azure-pipelines.yaml 示例配置吧:github

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 时才执行部署操做。docker

完整配置能够在 Github
上获取shell

CI 执行过程

上图是一个 dev 分支的 build,从上面的截图能够看到,只有 master 分支才执行的 step,没有被执行,直接跳过了ssh

Reference

  • < https://docs.microsoft.com/zh-cn/azure/devops/pipelines/process/conditions?view=azure-devops&tabs=yaml >
相关文章
相关标签/搜索