0

I want to use Jenkins environment variable for credentials as shown in code below, to specify the Salesforce login username and password, and pass the same to build.xml file so that I can deploy the code to Salesforce platform using Ant migration tool. How can this be done?

Jenkinsfile:

environment
            {
                SERVICE_CREDS=credentials('salesforce-creds')

            }


    stages
    {

         stage('Deploy')
         {
            steps
            {
                echo "Deploying"

                bat 'ant -f ./build.xml CIDeploy'
            }   

         }
      }

build.xml:

<project basedir="." xmlns:sf="antlib:com.salesforce">

    <property environment="env"/>


   <condition property="sf.username" value=""> <not> <isset property="sf.username"/> </not> </condition>
   <condition property="sf.password" value=""> <not> <isset property="sf.password"/> </not> </condition>
   <condition property="sf.deployroot" value=""> <not> <isset property="sf.deployroot"/>  </not> </condition>




<!--##################################################### Deploy #########################################-->

<target name="CIDeploy">
<sf:deploy
username="$SERVICE_CREDS_USR"
password="$SERVICE_CREDS_PSW"
serverurl="https://login.salesforce.com"
deployRoot="src"/>

  </target>
</project>

Error message from Jenkins console even after logging in to login.salesforce.com site:

BUILD FAILED

C:\Program Files (x86)\Jenkins\workspace\abc\build.xml:26: Invalid username, password, security token; or user locked out.

DeV
  • 13
  • 3

1 Answers1

0

Ant lets you pass parameters via command line using -D. Check How do I pass an argument to an Ant task? for some inspiration.

Try to read them from your credentials (sorry, been a while since I configured Jenkins) and pass as ant -f ./build.xml -Dsf.username=joe@joe.com -Dsf.password=MySuperSecretPass!!!andOptionallySecurityToken etc.

eyescream
  • 18,088
  • 2
  • 34
  • 46
  • Glad I could help :) Thanks for coming back to the question & accepting. – eyescream Aug 26 '19 at 21:30
  • Is there any way to mask the credentials? I tried using the following: `-Dsf.username=$SERVICE_CREDS_USR -Dsf.password=$SERVICE_CREDS_USR` but I was getting the error : **Invalid username, password, security token; or user locked out**. – DeV Sep 04 '19 at 11:07
  • Sorry man, my Jenkins is too rusty. If you don't want to see them in commandline's history and they're already passed from some credentials storage and you're sure that's how you pass (environment) variables... It's stupid but why not create the build.properties file in-flight and delete it afterwards? – eyescream Sep 04 '19 at 11:54