jenkinsjenkins-pipelineartifactoryjfrog-cli

How to replace Artifactory File Spec "Spec Vars" from Jenkins Declarative Pipeline


I'm in the process of converting a bunch of old scripts to Jenkins pipeline jobs. For many of our scripts, we used the JFrog CLI to handle all interactions with Artifactory. With Jenkins pipelines, we now have the option to do it through the Artifactory plugin. However, I cannot seem to find a way to replace variables inside of a file this way.

With JFrog CLI:

jfrog rt dl --spec spec.json  --spec-vars="foo=fooValue;bar=barValue"

When running this command, all instances of ${foo} and ${bar} inside of the spec.json would be replaced with the corresponding values. Everything works cleanly with a single line.

With Jenkins Pipeline:

 rtDownload(
    serverId: 'Artifactory',
    spec: '''{
       "files": [
           {
              "pattern": "spec.json"
           }
        ]
    }'''
)

followed by

rtDownload(
   serverId: 'Artifactory',
   specPath: 'spec.json'
)

This gets me the spec file I want, and downloads most of the files listed, but fails to locate the files that require variables. I've been unable to find any options to include variable replacement anywhere in the download of the file or after it.

I can still use the JFrog CLI like normal in the script, or add another function to manually modify this file to include the variables, though ideally I'd like everything done cleanly through the included pipeline integrations.


Solution

  • The Jenkins Artifactory Plugin does not currently support changing a predefined FileSpec like the JFrog CLI does with the spec-vars option.

    You can however set variables in a FileSpec if you provide the FileSpec inside your pipeline, like so:

    pipeline {
    agent any
    environment {
        fileVar="myfile"
    }
    stages {
        stage ('Download') {
            steps {
                rtDownload (
                    serverId: SERVER_ID,
                    spec: """{
                            "files": [
                                    {
                                        "pattern": "generic-local/${fileVar}",
                                        "target": "/tmp/"
                                    }
                            ]
                    }"""
                 )
             }
          }
       }
    }
    

    This means you will now maintain your FileSpec as part of your Jenkinsfile instead of separately, which will also allow you to avoid downloading it everytime.

    If it doesn't meet your needs, the alternative is to use a third party function or JFrog CLI like you mentioned.