我有两个 Jenkins 管道。第一个是使用 Maven 构建项目。
其中一个插件创建了一些脚本的 ZIP。
这是pom.xml部分
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>script</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>script-${project.version}</finalName>
<appendAssemblyId>true</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/script.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
script.xml指定ZIP的目录和要排除的文件
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/scripts</directory>
<filtered>true</filtered>
<directoryMode>755</directoryMode>
<fileMode>755</fileMode>
<lineEnding>unix</lineEnding>
<outputDirectory></outputDirectory>
<excludes>
<exclude>
**/*.log
</exclude>
</excludes>
</fileSet>
</fileSets>
构建结束时,
artifactPublisher
将 ZIP 发布到带有 mvn deploy
的 Nexus 存储库。[withMaven] artifactsPublisher - Archive artifact target/checkout/target/script-2.0.0-script.zip under com/company/artifact_id/2.0.0/artifact_id-2.0.0-script.zip
现在我有一个部署管道,需要使用 Ansible 下载此工件。
这是下载 ZIP 的任务
- maven_artifact:
group_id: "{{maven_grpid}}"
artifact_id: artifact_id
extension: zip
version: "{{ zip_version }}"
repository_url: "{{ nexus_repo }}"
username: "{{ nexus_user }}"
password: "{{ nexus_password }}"
dest: '{{ install_path }}/script/artifact_id-{{ zip_version }}-script.tmp'
verify_checksum: change
mode: '0770'
validate_certs: false
become: yes
此操作失败,因为它没有构建 ZIP 文件的正确 URL
Failed to download artifact com.company:artifact_id:zip:2.0.0 because of HTTP Error 404: com/company/artifact_id/2.0.0/artifact_id-2.0.0.zipfor URL https://nexus_url/repository/maven-releases/com/company/artifact_id/2.0.0/artifact_id-2.0.0.zip
文件的正确网址是:
https://nexus_url/repository/maven-releases/com/company/artifact_id/2.0.0/artifact_id-2.0.0-script.zip
如
artifactPublisher
部分所示。
我尝试使用
{{ zip_version }}-script
调整 Ansible 中的版本,文件名是正确的,但 URL 中的版本却被搞乱了。
我该如何解决这个问题?我可以在 Ansible 任务中调整文件名吗? 或者也许有办法更改发布的工件名称?
Ansible 作为开源工具的好处是你可以在代码上达到巅峰。
community.general
集合存储库中找到代表该模块的文件。
有了这个,我们现在可以找到 Ansible 在方法中构建 URL 的方式了
_uri_for_artifact
。
对于您的用例来说,这可能是上述方法中最有趣的几行:
if artifact.classifier:
return posixpath.join(self.base, artifact.path(), artifact.artifact_id + "-" + version + "-" + artifact.classifier + "." + artifact.extension)
有了这个,回到文档,我们可以看到您只需指定要在 URL 中构建的
classifier
即可。
所以你缺少的部分是
classifier: script
这会引导你完成一项任务
- maven_artifact:
group_id: "{{ maven_grpid }}"
artifact_id: artifact_id
classifier: script
extension: zip
version: "{{ zip_version }}"
repository_url: "{{ nexus_repo }}"
username: "{{ nexus_user }}"
password: "{{ nexus_password }}"
dest: '{{ install_path }}/script/artifact_id-{{ zip_version }}-script.tmp'
verify_checksum: change
mode: '0770'
validate_certs: false
become: yes