Wim Deblauwe
Wim Deblauwe

Reputation: 26878

How to upload a file using Jenkins Declarative Pipeline to Artifactory

I have an Angular webapp that I want to publish as an artifact to Artifactory using Jenkins Declarative pipelines. How do I do this?

Upvotes: 4

Views: 9793

Answers (1)

Wim Deblauwe
Wim Deblauwe

Reputation: 26878

To upload to Artifactory, use the rtUpload command. For example:

steps {
    sh 'npm run build:production'

    echo "Deploy to Artifactory"

    script {
      VERSION = sh(script: 'cat package.json | grep \'version\' | grep -o -E \'[0-9]+\\.[0-9]+\\.[0-9]+(\\-SNAPSHOT)?\'',
                  returnStdout: true).trim()
    }

    sh "sed 's/REPLACE_VERSION_HERE/${VERSION}/g' upload-pom.xml.template > upload-pom-${VERSION}.xml"

    zip zipFile: "myapp-webapp-${VERSION}-distribution.zip", archive: true, dir: 'dist'

    rtUpload (
        serverId: "main",
        spec:
            """{
              "files": [
                {
                  "pattern": "myapp-webapp-(*)-distribution.zip",
                  "target": "reponame/com/company/product/myapp-webapp/{1}/myapp-webapp-{1}-distribution.zip"
                },
                {
                  "pattern": "upload-pom-(*).xml",
                  "target" : "reponame/com/company/product/myapp-webapp/{1}/myapp-webapp-{1}.pom"
                }
             ]
            }""",
        failNoOp: true
    )
  }

The code does the following:

  1. Run the npm production build
  2. Extract the version from package.json into a VERSION variable.
  3. Prepare a "dummy" pom.xml file
  4. Create a zip of the dist directory that contains the Angular application.
  5. Use the rtUpload command to upload the zip file + the pom file

The pom file is uploaded to ensure SNAPSHOT version that are uploaded this way work correctly. Without it, there is no Maven metadata generated at Artifactory and Maven will not download new SNAPSHOT versions (e.g. if you use this artifact in your Maven based Spring Boot application). With this, everything works fine with Maven.

The upload-pom.xml.template should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    http://maven.apache.org/xsd/maven-4.0.0.xsd"
  xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.company.product</groupId>
  <artifactId>myapp-webapp</artifactId>
  <version>REPLACE_VERSION_HERE</version>
  <packaging>zip</packaging>
  <description>Dummy pom.xml for Artifactory</description>
</project>

Upvotes: 5

Related Questions