xmljenkinscredentialsjenkins-clijenkins-api

Manage "secret file" Jenkins credentials using XML files


I would like to manage the "secret file" Jenkins credentials using XML files as described in this thread's comment, but the resulting credential is not readable by my pipeline.

I create the XML file as follows:

<org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl plugin="plain-credentials">
    <id>CREDENTIAL_ID</id>
    <description>CREDENTIAL_DESCRIPTION</description>
    <fileName>SECRET_FILE_NAME</fileName>
    <secretBytes>SECRET_FILE_CONTENTS</secretBytes>
</org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl>

SECRET_FILE_CONTENTS is simply the text contained in the actual file: a Kubeconfig file in JSON format in my case.

The Jenkins credential is successfully stored, but when I try to use it in my Jenkins pipeline this error is returned:

error: error loading config file "/root/.kube/config": yaml: invalid leading UTF-8 octet

In order to exclude that the file is the problem, I have then tried to upload manually the file via the GUI and the pipeline reads the secret file successfully and it runs without encountering the same problem. So the issue is really in the way I pass the contents of the secret file to the XML file.


Solution

  • I have managed to solve it by encoding the file's contents in base64 and then store the resulting string in the XML file

    <org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl plugin="plain-credentials">
        <id>CREDENTIAL_ID</id>
        <description>CREDENTIAL_DESCRIPTION</description>
        <fileName>SECRET_FILE_NAME</fileName>
        <secretBytes>SECRET_FILE_CONTENTS_ENCONDED_IN_BASE64</secretBytes>
    </org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl>