简体   繁体   中英

How do I convert this groovy class and run it in Jenkins as a library?

I have this groovy class that has a main method and some other methods.

public class secrets {
    public static void main(String[] args) {
        String secretName = args[0]; 
        String secretType = args[1];  //username, password or b64_authkey

        SecretsManagerClient sm = SecretsManagerClient.builder()
                .region(Region.EU_WEST_1)
                .build();

        getSecret(sm, secretName, secretType);
        sm.close();

    }
    public static String getSecret(SecretsManagerClient sm,String secretName, String secretType){
  .......

When trying to run this in Jenkins I get the error:

groovy.lang.MissingMethodException: No signature of method: com.dev.libs.secrets.call() is applicable for argument types: (java.lang.String, java.lang.String,) values: [secret, username]

I know I need a call function, but I'm not sure on how to use it in this context. How can I convert this code?

Assuming you want to do something with the resulting String object from getSecret method. I further assume, that you have already put your secrets.groovy file in a shared library (identified by the name test-library in my example) and have the correct @Library annotation in your build script.

Note: I had to "mock" some of the objects to test my code. So you can basically ignore the first few code snippets if you like.

Jenkins shared library code

Mock file for vars/Region.groovy

enum Region {
    EU_WEST_1
}

Mock file for vars/SecretsManagerClient.groovy

class SecretsManagerClient {
    static SecretsManagerClientBuilder builder() {
        new SecretsManagerClientBuilder()
    }

    def close() {
    }

    static class SecretsManagerClientBuilder {
        SecretsManagerClientBuilder region(Region region) {
            this
        }

        SecretsManagerClient build() {
            new SecretsManagerClient()
        }
    }
}

Code example for vars/secrets.goovy

// probably some import for SecretsManagerClient and Region

//public class secrets {

def call(String secretName, String secretType) {
    SecretsManagerClient sm = SecretsManagerClient.builder()
            .region(Region.EU_WEST_1)
            .build();

    try {
        return getSecret(sm, secretName, secretType);
    } finally {
        sm.close();
    }
}

static String getSecret(SecretsManagerClient sm, String secretName, String secretType) {
    "secret"
}

//}

Jenkins pipeline build script

I have used a very simple build script calling the build step and echoing the result from getSecret method.

// import library named "test-library" in settings (mind the underscore at the end!)
@Library('test-library')_

// run on master node (or any other label you might choose)
node('master') {
  // create a stage
  stage('Get secret from new step') {
    // run our custom build step named "secrets", which will use "call" function to call "getSecret"  
    def theSecret = secrets "name", "type"
    // print result
    sh "echo ${theSecret}"
  }
}

Output

[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/Test-Job
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Get secret from new step)
[Pipeline] sh
+ echo secret
secret
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM