Azure DevOps Pipeline - Maven 仅在不存在时部署发布 [英] Azure DevOps Pipeline - Maven deploy release only if it does not exist

查看:29
本文介绍了Azure DevOps Pipeline - Maven 仅在不存在时部署发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Azure DevOps 和 Maven 的新手.

I am new to both Azure DevOps and Maven.

我们已经建立了一个 Azure 构建管道,以便它将为快照构建和发布部署工件.

We have set up an Azure build pipeline such that it will deploy artifacts for snapshot builds and also for releases.

我希望发布工件的部署是幂等的.也就是说,如果工件已经部署,它应该不会出错.

I want deployment of release artifacts to be idempotent. That is if the artifact has already been deployed it should not be an error.

问题是我收到了 409资源冲突"

Q 有没有办法告诉 maven 仅当工件不存在时才部署并且如果确实如此,则不是错误.

DevOps 有没有办法做到这一点?

Is there anyway to do this from DevOps?

对于我自己的教育,我也想知道如何为maven(没有Azure)做到这一点.这可以通过命令行开关、pom.xml 或 maven settings.xml

For my own education, I would also like to know how to do this for maven (without Azure). This could be via either a command line switch, the pom.xml or the maven settings.xml

似乎暗示没有,如果有,那是一个令人惊讶的遗漏.我想了解其中的原理.

It seems to be implied that there is not, if so it is a surprising omission. I would like to understand the rationale.

如果有一种方法可以检查部署的工件是否与管道刚刚构建的工件相同,则加分.

Bonus points if there is a way to check that the artifact deployed is actually the same as the one just built by the pipeline.

相关的管道片段是:

   task: Maven@3
#          condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
          inputs:
            mavenPomFile: 'pom.xml'
            options: '-B -s $(mvnSettings.secureFilePath) -DWHERE="AzureDevops" clean deploy'
            mavenAuthenticateFeed: true
            publishJUnitResults: true
            testResultsFiles: '**/TEST-*.xml'


作为背景,这是我对 Azure 和 Maven 的了解.如果我误解了什么,那可能是一个促成因素.


For background this is what I know about Azure and Maven. If I have misunderstood anything it may be a contributing factor.

Maven 允许你部署两种工件:

Maven allow you to deploy two kinds of artifact:

快照

  • 快照是包的开发版本.
  • 快照带有后缀 -SNAPSHOT.例如.1.2.0-快照
  • 快照是可变的.部署操作可以用新版本替换 SNAPSHOT(更新的开发版本可以替换开发版本)

发布

  • 任何不以 -SNAPSHOT 结尾的版本都被视为发布版本.
  • 版本是不可变的.如果版本已部署到存储库,则部署操作将失败.

Azure 和 Maven 都认为已发布的工件是不可变的.Azure 在充当 maven 存储库时理解 -SNAPSHOT,并允许覆盖开发版本.这个想法是你不能(或至少不容易)替换其他可能依赖的已发布工件.

Both Azure and Maven considered published artifacts as immutable. Azure understands -SNAPSHOT when acting as a maven repository and allow development versions to be overwritten. The idea is that you cannot (or at least not easily) replace a published artifact that something else might depend upon.

409 = 资源冲突

这可能意味着:

  • 工件已经发布,无法覆盖

  • The artifact has already been published and cannot be overwritten

无法发布工件,因为它的类型错误.例如将发布发布到只接受快照的仓库或将快照发布到只接受发布的仓库

The artifact could not be published because it was the wrong type. For example publishing a release to a repository that only accepts snapshots or publishing a snapshot to a repository that only accepts releases

我不知道如何告诉 Maven 如果工件已经存在,部署失败是可以的.明显且错误的 hack(在 Linux 中)是:

I am not sure how to tell maven that its okay for the deployment to fail if the artifact already exists. The obvious and wrong hack (in Linux) is:

mvn deploy || /bin/true

这很糟糕,因为如果部署步骤由于其他原因失败,它会报告部署步骤成功.

This is bad because it will report the deployment step as successful if it has failed for another reason.

有一个maven插件(https://github.com/chonton/exists-maven-plugin) 用于执行此操作.我不确定您将如何在 Azure 中使用它.这个插件是事实上的标准吗?

There is a maven plugin (https://github.com/chonton/exists-maven-plugin) for doing this. I am not sure how you would use this in Azure. Is this plugin a defacto standard?

另见:

23/06/2020 更新

Update 23/06/2020

我快到了,但卡住了:

    variables: 
    - name: artifactDoesNotExist
      value: '0'
    - name: mavenRepoURL
      value: 'https://blahblah.visualstudio.com/_packaging/myazurefeedname/maven/v1/com/mycompany/myproject'

    - task: Bash@3
      displayName: 'Check if Maven artifact exists'
      inputs:
        targetType: inline
        failOnStderr: false
        script: |
               #set variable iff artifact exists
               VERSION=`cat VERSION.MVN`; mvn -X -B -s $(mvnSettings.secureFilePath) -DWHERE="AzureDevops" -DremoteRepositories=$(mavenRepoUrl) dependency:get -Dartifact=com.mycompany.project:artifiactId:"$VERSION"
            echo "##vso[task.setvariable variable=artifactDoesNotExist]$?"

    - task: Bash@3
      condition: and(succeeded(), eq(variables['artifactDoesNotExist'], '0'))
      inputs:
        targetType: inline
        script: |
            echo artifactDoesNotExist == 0 -> true

    - task: Bash@3
      condition: and(succeeded(), eq(variables['artifactDoesNotExist'], '1'))
      inputs:
        targetType: inline
        script: |
            echo artifactDoesNotExist == 1 -> true

我怀疑dependency:get 命令行可能不太正确.

I suspect the dependency:get command line may not be quite right.

注意:在测试命令时,我必须记住从 ~/.m2/repository 中删除工件,因为它在本地看起来如此.

Note: when testing the command I have to remember to delete the artifact from ~/.m2/repository as it look in the local one.

另一件奇怪的事情正在发生.尽管我已经部署了工件的新测试版本,但它们并未出现在相关的 Azure 源中.然而,第一次尝试上传成功,而后续上传失败.这些上传文件去哪里了,为什么我在 Dev Ops 中看不到它们?

Another strange thing is occurring. Although I have deployed new test versions of the artifact they do not appear in the relevant Azure feed. And yet the first attempted upload succeeds while subsequent uploads fail. Where are these uploads going and why can't I see them in Dev Ops?

我发现此问题的版本仍作为带有版本的 maven 工件com.mycompany.myproject:artifactId"存在于提要中.

The version for which I discovered this issue is still in the feed as a maven artifact 'com.mycompany.myproject:artifactId' with a version.

另请参阅上传和下载 Azure 工件的等效 Maven 命令和设置是什么?

推荐答案

这应该和 Maven 相关,Azure DevOps 端没有什么具体的配置.

This should more related to Maven , there is nothing specific to configure in Azure DevOps side.

您可以尝试在构建管道中使用命令行任务首先检查该发布版本是否存在:

You could try to use a command line task in your build pipeline to check first if that release version exists:

mvn dependency:get -Dartifact=g:a:v -o -DrepoUrl=file://path/to/your/repo

更多细节看这个如何从命令行确定 Maven 工件是否在我的存储库中?

如果给定的 (group-artifact-version) 确实存在,那么您不要继续构建的其余部分.

If that gave (group-artifact-version) does exists, then you don't proceed with the rest of the build.

这篇关于Azure DevOps Pipeline - Maven 仅在不存在时部署发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆