基于Jenkins Pipeline Artifactory插件,如何检查包是否已上传至Artifactory?
Checking if a Package Exists in Artifactory via Jenkins Pipeline Artifactory Plugin
Absolutely! You have a couple of reliable ways to verify if a package is already uploaded to Artifactory when using the Jenkins Pipeline Artifactory plugin. Below are two common, practical approaches tailored for pipeline execution:
1. Using the Artifactory Pipeline Plugin's Built-in Methods
This is the most streamlined approach since it leverages the plugin's native integration with your Jenkins Artifactory configuration:
- First, define your Artifactory server instance (this assumes you've already set up an Artifactory server in Jenkins via Manage Jenkins → Configure System → Artifactory):
def artifactoryServer = Artifactory.server('YOUR_ARTIFACTORY_SERVER_ID') - Construct the full path to your target package in Artifactory. This follows the format
{repository-key}/{full-package-path}. For example, a JAR in thelibs-release-localrepo atcom/example/myapp/1.2.3/myapp-1.2.3.jarwould use:def packagePath = "libs-release-local/com/example/myapp/1.2.3/myapp-1.2.3.jar" - Use the plugin's
exists()method to check for the package's existence. This uses Artifactory's REST API under the hood (no full download needed, so it's efficient):def packageExists = artifactoryServer.exists(packagePath) - Use the boolean result to control your pipeline flow:
if (packageExists) { echo "*Package already exists in Artifactory. Skipping upload step.*" // Add custom logic here: skip upload, send a notification, etc. } else { echo "*Package not found. Proceeding with upload.*" // Your upload logic (e.g., using `artifactoryUpload`) goes here }
2. Directly Calling Artifactory REST API (Alternative)
If you want more control over the request or need to retrieve additional metadata, you can send a HEAD request to Artifactory's storage API:
- First, gather your Artifactory details and credentials (retrieve credentials from Jenkins' credential store to avoid hardcoding):
def artifactoryUrl = "https://your-artifactory-instance.com/artifactory" def creds = credentials('YOUR_ARTIFACTORY_CREDENTIALS_ID') def packagePath = "libs-release-local/com/example/myapp/1.2.3/myapp-1.2.3.jar" def apiEndpoint = "${artifactoryUrl}/api/storage/${packagePath}" - Use the
httpRequestplugin (ensure it's installed in Jenkins) to send the HEAD request. Check the response code to confirm existence:def response = httpRequest( url: apiEndpoint, httpMode: 'HEAD', authentication: creds.id, validResponseCodes: '200,404' // Accept both success and "not found" as valid responses ) def packageExists = response.status == 200 if (packageExists) { echo "*Package exists at: ${apiEndpoint}*" } else { echo "*Package does not exist at: ${apiEndpoint}*" }
Key Notes
- Replace all placeholders (like
YOUR_ARTIFACTORY_SERVER_IDand package paths) with your actual values. - The plugin's
exists()method is generally preferred because it ties into your existing Jenkins Artifactory setup, reducing manual configuration. - Ensure your Jenkins agent has network access to your Artifactory instance.
- For broader searches (e.g., checking if any version of a package exists), use the
artifactory.search()method with a search specification to find matching items.
内容的提问来源于stack exchange,提问作者Pete Whitehead
相关产品推荐
相关产品推荐

