Home > Software engineering >  freeStyleJob SCM credential from parameters not working
freeStyleJob SCM credential from parameters not working

Time:01-02

I'm trying to use a simple FreeStyleJob SCM , and set the credentialId UUID from build parameter. The problem is, it seems the credentials is not parsing the parameter correctly.

scm {
    git {
        remote {
            github('\${MY_REPO_HANDLE}', 'ssh')
            credentials('\${MY_REPO_CREDENTIALS}')
        }
        branch('\${MY_BRANCH}')
    }
}

My MY_REPO_CREDENTIALS is a simple String parameter

stringParam {
        name("MY_REPO_CREDENTIALS")
        defaultValue("teste-credential")
    }

Log:

Warning: CredentialId "${MY_REPO_CREDENTIALS}" could not be found.

UPDATE

This Jenkins Job is created by another Jenkins Job using DSL external. In resume, Job 1 whenn triggered will create the Job 2 on jenkins. When I try to use "$ (without \) the job will fail because this parameter doesn't exist on Job 1 context.

job config.xml:

<scm >
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>[email protected]:${MY_REPO_HANDLE}.git</url>
<credentialsId>${MY_REPO_CREDENTIALS}</credentialsId>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>${MY_BRANCH}</name>
</hudson.plugins.git.BranchSpec>
</branches>
<configVersion>2</configVersion>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<gitTool>Default</gitTool>
<browser >
<url>https://github.com/${MY_REPO_HANDLE}/</url>
</browser>
</scm>

CodePudding user response:

You can switch to using " instead of ' so you can use the autofill feature:

scm {
    git {
        remote {
            github("${MY_REPO_HANDLE}", 'ssh')
            credentials("${MY_REPO_CREDENTIALS}")
        }
        branch("${MY_BRANCH}")
    }
}

CodePudding user response:

The final solution was set the ssh agent before build, using wrapper:

wrappers {
        sshAgent("\${MY_REPO_CREDENTIALS}")
    }
    keepDependencies(false)
    scm {
        git {
            remote {
                github("\${MY_REPO_HANDLE}", 'ssh')
            }
            branch("\${MY_BRANCH}")
        }
    }
  • Related