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

查看:110
本文介绍了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仅在工件不存在时才进行部署 并且它不是错误.

Q Is there a way to tell maven to deploy only if the artifact does not exist and that it is not an error if it does.

无论如何,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都认为已发布的工件是不可变的. 当充当Maven存储库时,Azure理解-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如果工件已经存在,则部署失败是可以的. (在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?

另请参阅:

  • How to update a maven dependency with a same version number in Azure Artifacts
  • Failed to deploy artifacts using maven, error code 409

更新23/06/2020

Update 23/06/2020

我快到这了,但是卡住了:

I am nearly there with this but stuck:

    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 feed中.但是,第一次尝试的上载成功,而随后的上载失败. 这些上载会去哪里,为什么我在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?

我发现此问题的版本仍在feed中,作为带有版本的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.

另请参见上传和下载天蓝色工件的等效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

更多详细信息请参见如果给出的(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天全站免登陆