Saturday, 26 January 2019

Continuous Integration Using Jenkins with SalesforceDX | JWT-Based Flow


In this post we will talk about what is Continuous Integration and how to setup Continuous Integration Using SalesforceDX with Jenkins (Salesforce dx jenkins integration). We will use the OAuth JSON Web Token (JWT) bearer flow that’s supported in the Salesforce CLI. The JWT bearer flow supports the RSA SHA256 algorithm, which uses an uploaded certificate as the signing secret.

What Is Continuous Integration (CI) ?

Continuous integration (CI) is a DevOps software development best practice. Where developers regularly merge their code changes into a central repository, after that automated builds and tests are run. This best practice helps development teams to detect problems, identify bugs, and fix those problems and bugs before releasing changes to their customers. Please check our YouTube Recording for step by step process.

How to setup Continuous Integration Using Jenkins.

Step 1) Install Salesforce CLI. Here is link to download.

 

Step 2) Install Jenkins. Download from here.

  • Install Git Plugins.

 

Step 3) Create a Self-Signed SSL Certificate and Private Key

  • Set OPENSSL_CONF path  
    set OPENSSL_CONF=C:\openssl\share\openssl.cnf 
  • Generate an RSA private key :  
    openssl genrsa -des3 -passout pass:x -out server.pass.key 2048
  • Create a key file from the server.pass.key file
    openssl rsa -passin pass:x -in server.pass.key -out server.key
  • Request and generate the certificate :
    openssl req -new -key server.key -out server.csr
  • Generate the SSL certificate
    openssl x509 -req -sha256 -days 365 -in server.csr -signkey server.key -out server.crt

 

Step 4) Create Connected App for JWT-Based Flow

  • Created Connected App
    • Callback URL
      http://localhost:1717/OauthRedirect
    • Use digital signatures To upload your server.crt file.
  • Edit policy and select "Admin approved users are pre-authorized" to avoid "Not approved for access in salesforce" issue. 
  • Assign Connected App to user or System Admin profile.
  • Validate Authorize an Org Using the JWT-Based Flow  
    sfdx force:auth:jwt:grant --clientid {ADD_YOUR_CLIENT_ID} --jwtkeyfile server.key --username amit.salesforce21@gmail.com --instanceurl https://login.salesforce.com --setdefaultdevhubusername
    • --clientid  :- provide Consumer Key
    • --jwtkeyfile :- Absolute path to the location where you generated your OpenSSL server.key file
    • --instanceurl :-provide instanceurl if you are using sandbox.
    • --setdefaultdevhubusername :- Set Default dev hub User Name.
Sample

sfdx force:auth:jwt:grant --clientid KeyHere --jwtkeyfile c:\openssl\bin\server.key --username amit.salesforce21@gmail.com --instanceurl https://login.salesforce.com --setdefaultdevhubusername

Step 5) Configure the Jenkins environment Variable

  • Configure the the Server.key in the credential plugins. 
  • Set Environment variable:-
    • HUB_ORG_DH:- The username for the Dev Hub org, such as amit@gmail.com
    • SFDC_HOST_DH:- The login URL of the Salesforce instance that is hosting the Dev Hub org. The default is https://login.salesforce.com
    • CONNECTED_APP_CONSUMER_KEY_DH :- The consumer key that was returned after you created a connected app in your Dev Hub org.
    • JWT_CRED_ID_DH:- The credentials ID for the private key file that you stored in the Jenkins Admin Credentials interface
  • Install the Custom Tools Plugin into your Jenkins console, and create a custom tool that references the Salesforce CLI
The names for these environment variables are just suggestions. You can use any name as long as you specify it in the Jenkinsfile.

 

Step 6) Configure the Jenkins 

  • Add New Item.
  • Add Source and provide repository URL.  You can use URL as sample.
    https://github.com/amit-salesforce/SFDXProject
  •  Start a build.


We will talk about different option in Jenkins in our future post.

Please check our YouTube Recording for step by step process.


URL :- https://youtu.be/IvTlx6lBkQY


Check below post for more detail:-
  1. Salesforce DX Developer Guide
  2. Continuous Integration Using Salesforce DX

Check this post to learn more about Salesforce Dx. Here is recording for SalesforceDx Session in ApexHours. If you want to learn about Lightning Web Components. Check this post.


Please share your feedback and question.


Thanks,
Amit Chaudhary

96 comments:

  1. Hi Amit,
    Thanks for the detailed documentation.
    I am setting up the DX by following the instructions as mentioned but in the set up 4
    when i gave command as below

    sfdx force:auth:jwt:grant --clientid 3MVG9ZL0ppGP5UrDaftJUyCSnIoXbPpQSP2TVYPQixfytB2jssu.YO9dqgcJP_.q_.qhVa3Is6eUX_QvkjiXu --jwtkeyfile C:\Program Files\OpenSSL-Win64\bin\server.key --username ac@demo.com --instanceurl https://login.salesforce.com --setdefaultdevhubusername

    Giving the error as ! Unexpected argument Files\OpenSSL-Win64\bin\server.key

    Do you have any idea why is this error is happening?.

    Thanks,
    Rama Injety

    ReplyDelete
  2. Hi Amit,

    After i watched the Youtube Recording video i have figured out the issue.

    Once again thanks for sharing the information.

    It really helped me a lot.

    Thanks,
    Rama

    ReplyDelete
    Replies
    1. sfdx force:auth:jwt:grant -i 3MVG9Y6d_Btp4xp7FvObDZ.uuShvn2_P3mjowC1mh6qFR7RKkJO27.eUNo8RMkVOgj7W_2OceT2wwxOlwkPNu -f server.key --username gaurav07goyat@gmail.com -r https://login.salesforce.com -d

      Delete
  3. Hi Amit,
    Thanks for the great post.
    I was trying to deploy to the target org from Github.
    the problem i am having with deployment although the build is successful but nothing added in the target org.
    Can you please help me where i am missing here.Seems like its not finding any data in manifest directory.
    Please help.
    My Github repository is https://github.com/bganguly1984/Salesforce.

    Thanks,
    Bhaskar

    ReplyDelete
    Replies
    1. Update your deployment command

      // need to pull out assigned username
      if (isUnix()) {
      rmsg = sh returnStdout: true, script: "${toolbelt} force:source:deploy --manifest manifest/package.xml -u ${HUB_ORG}"
      }else{
      rmsg = bat returnStdout: true, script: "\"${toolbelt}\" force:source:deploy --manifest manifest/package.xml -u ${HUB_ORG}"
      }

      Delete
    2. Hello Bhaskar, can you please help me to resolve the error?

      Delete
  4. Replies
    1. Try to update your Jenkine like below file
      https://github.com/amit-salesforce/ApexHours/blob/master/Jenkinsfile

      Delete
  5. Hi Amit

    I am getting below error while building the job,

    Started by user admin
    Checking out git https://github.com/amit-salesforce/SFDXProject into C:\Program Files (x86)\Jenkins\workspace\Salesforce22_SFDC@script to read Jenkinsfile
    No credentials specified
    > git.exe rev-parse --is-inside-work-tree # timeout=10
    Fetching changes from the remote Git repository
    > git.exe config remote.origin.url https://github.com/amit-salesforce/SFDXProject # timeout=10
    Fetching upstream changes from https://github.com/amit-salesforce/SFDXProject
    > git.exe --version # timeout=10
    > git.exe fetch --tags --force --progress https://github.com/amit-salesforce/SFDXProject +refs/heads/*:refs/remotes/origin/*
    > git.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
    > git.exe rev-parse "refs/remotes/origin/origin/master^{commit}" # timeout=10
    Checking out Revision da509ec48c48efbf9571cbc2dbf2d6b21218c49f (refs/remotes/origin/master)
    > git.exe config core.sparsecheckout # timeout=10
    > git.exe checkout -f da509ec48c48efbf9571cbc2dbf2d6b21218c49f
    Commit message: "Update package.xml"
    > git.exe rev-list --no-walk da509ec48c48efbf9571cbc2dbf2d6b21218c49f # timeout=10
    Running in Durability level: MAX_SURVIVABILITY
    [Pipeline] Start of Pipeline
    [Pipeline] node
    Running on Jenkins in C:\Program Files (x86)\Jenkins\workspace\Salesforce22_SFDC
    [Pipeline] {
    [Pipeline] echo
    KEY IS
    [Pipeline] echo
    b711b818-2992-46ed-a8dd-f1ac66b0deaa
    [Pipeline] echo
    chirag.dhangar07@gmail.com
    [Pipeline] echo
    login.salesforce.com
    [Pipeline] echo
    3MVG9ZL0ppGP5UrAvVhi0FZQlTUSCnzOEt2sncSqazmXTiyyxi5v5dOhlWS6iCjKsC3_klGjxwCwYLdB68zSK
    [Pipeline] tool
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    ERROR: No tool named toolbelt found
    Finished: FAILURE

    ReplyDelete
    Replies
    1. You need to setup the toolbelt

      Delete
    2. Hi Amit,
      I am getting the same error. What exactly do we need to do to setup toolbelt. Can you share a help link for the same.

      Delete
    3. Install the Custom Tools Plugin into your Jenkins console, and create a custom tool that references the Salesforce CLI

      Delete
    4. This comment has been removed by the author.

      Delete
    5. I am also getting the same error. "Install the Custom Tools Plugin into your Jenkins console" i have already installed the Custom tools plugin. "and create a custom tool that references the Salesforce CLI" how to do this part?

      Delete
    6. Hi Amit,

      I have install the Custom Tools plugin.
      Now how to create a custom tool that references the Salesforce CLI?
      thanks in advance

      Delete
    7. I am assuming that you have already installed the Salesforce CLI on your machine. If not, then install it first using this link.
      https://developer.salesforce.com/tools/sfdxcli

      Go to Jenkins > Manage Jenkins > Global Tool Configuration
      Under the heading Custom Tool, click on "Custom tool configurations...". Click on "Add Custom Tool"
      Name - toolbelt
      Click on "Custom Tool Configuration"
      (for windows) Installation directory - C:\Program Files\sfdx\bin
      (for mac) Installation directory - /usr/local/bin

      Apply and Save

      Delete
    8. Thanks Amith. now I am getting another error like ' Could not find credentials entry with ID '362c307f-3224-445f-9ea8-7bc280c9eee6'. do you have any idea about this error?

      Delete
    9. getting the sane error but could not understand the reason. this ID is basically the global credentials id in credentials of Jenkins

      Delete
  6. Failed to connect to repository : Command "git.exe ls-remote -h https://github.com/amit-salesforce/SFDXProject HEAD" returned status code 128:
    stdout:
    stderr: fatal: unable to access 'https://github.com/amit-salesforce/SFDXProject/': SSL certificate problem: unable to get local issuer certificate

    ReplyDelete
  7. Validate Authorize an Org Using the JWT-Based Flow
    sfdx force:auth:jwt:grant --clientid {ADD_YOUR_CLIENT_ID} --jwtkeyfile server.key --username amit.salesforce21@gmail.com --instanceurl https://login.salesforce.com --setdefaultdevhubusername

    Where can I get this client id?

    ReplyDelete
    Replies
    1. Its the consumer key that you get when making a New Connected App in Salesforce

      Delete
  8. I get the following error when building the first time

    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] withCredentials
    [Pipeline] // withCredentials
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    java.lang.NullPointerException
    at com.cloudbees.plugins.credentials.CredentialsProvider.findCredentialById(CredentialsProvider.java:874)
    at com.cloudbees.plugins.credentials.CredentialsProvider.findCredentialById(CredentialsProvider.java:852)
    at org.jenkinsci.plugins.credentialsbinding.MultiBinding.getCredentials(MultiBinding.java:151)
    at org.jenkinsci.plugins.credentialsbinding.impl.AbstractOnDiskBinding.bindSingle(AbstractOnDiskBinding.java:39)
    at org.jenkinsci.plugins.credentialsbinding.Binding.bind(Binding.java:150)
    at org.jenkinsci.plugins.credentialsbinding.impl.BindingStep$Execution2.doStart(BindingStep.java:134)
    at org.jenkinsci.plugins.workflow.steps.GeneralNonBlockingStepExecution.lambda$run$0(GeneralNonBlockingStepExecution.java:77)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Finished: FAILURE

    Anny suggestions what I can do?

    ReplyDelete
    Replies
    1. Is this issue solved? I am facing the same issue

      Delete
    2. You need to add Custom Tool "toolbelt" in Global Tool Configuration

      Delete
    3. Already added Custom Tool "toolbelt"in Global Tool Config. But this error continues.

      Delete
    4. Is it resolve? I am getting same error.

      Delete
  9. Hi Amit,

    Sub: toolbelt setup issue.

    I did below steps, but getting exception. I didn't find any field as u mentioned in ur previous comments as "Installation directory".

    Global Tool Configuration -> Custom tool -> Custom tool installations:
    Custom tool Name: toolbelt
    Exported paths: C:\Program Files\sfdx\bin


    [Pipeline] tool
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    java.net.MalformedURLException: no protocol:
    at java.net.URL.(Unknown Source)
    at java.net.URL.(Unknown Source)
    at java.net.URL.(Unknown Source)
    at hudson.tools.ZipExtractionInstaller.performInstallation(ZipExtractionInstaller.java:83)
    at hudson.tools.InstallerTranslator.getToolHome(InstallerTranslator.java:69)
    at hudson.tools.ToolLocationNodeProperty.getToolHome(ToolLocationNodeProperty.java:109)
    at hudson.tools.ToolInstallation.translateFor(ToolInstallation.java:206)

    Please advise.

    Thanks.

    ReplyDelete
    Replies
    1. Is this solved? What is the solution? I am facing same issue.

      Delete
    2. Is this Solved ? I'm also facing the same issue

      Delete
    3. Is this resolved? Getting same error.
      java.net.MalformedURLException: no protocol

      Delete
    4. is this issue resolved? i am facing error. i had setup toolbelt path(custom tool)
      java.net.MalformedURLException: no protocol:
      at java.net.URL.(Unknown Source)
      at java.net.URL.(Unknown Source)
      at java.net.URL.(Unknown Source)
      at hudson.tools.ZipExtractionInstaller.performInstallation(ZipExtractionInstaller.java:83)
      at hudson.tools.InstallerTranslator.getToolHome(InstallerTranslator.java:69)
      at hudson.tools.ToolLocationNodeProperty.getToolHome(ToolLocationNodeProperty.java:109)
      at hudson.tools.ToolInstallation.translateFor(ToolInstallation.java:206)
      at com.cloudbees.jenkins.plugins.customtools.CustomTool.forNode(CustomTool.java:163)
      at com.cloudbees.jenkins.plugins.customtools.CustomTool.forNode(CustomTool.java:65)
      at org.jenkinsci.plugins.workflow.steps.ToolStep$Execution.run(ToolStep.java:152)
      at org.jenkinsci.plugins.workflow.steps.ToolStep$Execution.run(ToolStep.java:133)
      at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
      at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
      at java.util.concurrent.FutureTask.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
      Finished: FAILURE

      Delete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Hi Amit,

    I am getting this error!
    C:\Program Files (x86)\Jenkins\workspace\DevOrg_>"" force:auth:jwt:grant --clientid 3MVG9G9pzCUSkzZvL0eg5JOEbMZU6ogOA5ZNMyfnMLAKMQEalxWTeEsi_tZr5rUhkrvNDWXKIdmexm9VhFFUO --username bitbucket-integration@concret.io --jwtkeyfile "****" --setdefaultdevhubusername --instanceurl https://login.salesforce.com
    '""' is not recognized as an internal or external command,
    operable program or batch file.
    [Pipeline] error
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // withCredentials
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    ERROR: hub org authorization failed
    Finished: FAILURE

    Can you please tell me that how to setup toolbelt, because I think I am getting this error because of toolbelt only?

    Thanks

    ReplyDelete
    Replies
    1. Hi vikas,
      I am getting same error .
      did you get any solution for that one?
      if you find any solution please let me know?

      Delete
    2. I'm also getting same issue.

      Delete
    3. I am also getting same error . Could you please let me know how it will resolve.

      Delete
    4. Did anyone got the solution for this?

      Delete
    5. I'm also getting the same error

      Delete
    6. /C:\Program Files (x86)\Jenkins\workspace\BasicAuto_>"" force:auth:jwt:grant --clientid 3MVG9Y6d_Btp4xp78sJezspGP9HAkWhrS9xWf06s3BjVOSWqIyZa8ESqha55VjkV8PooiKBIB2.1.iS0FO8b1 --username bhavesh@jogi.com --jwtkeyfile "****" --setdefaultdevhubusername --instanceurl https://login.salesforce.com
      '""' is not recognized as an internal or external command,
      operable program or batch file.
      [Pipeline] error
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] }
      [Pipeline] // withCredentials
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] End of Pipeline
      ERROR: hub org authorization failed
      Finished: FAILURE


      Please help it. How to solve ?

      Delete
  12. HI All,
    I am facing an error :'""' is not recognized as an internal or external command,
    operable program or batch file.

    Any help is much appreciated.

    Thanks

    ReplyDelete
  13. Hi Amit,

    Thank you for your post.

    While I am implementing this method, I have few questions in mind and appreciate any response from you !! :)

    1. Using this method , can we integrate/deploy code, config as well as lightning components?
    2. Do you find any disadvantages of this method so far? With DX , which one is a recommended approach?

    Thanks!

    Kind regards,
    Milli

    ReplyDelete
  14. Hey Amit,

    Can Jenkins do the integration with Jira.

    ReplyDelete
  15. Subhash Beniwal20 July 2019 at 13:45

    Amit Chaudhary

    I am facing following issue to setup Jenkins, Please help me to resolve this issue.

    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    java.net.MalformedURLException: no protocol:
    at java.net.URL.(Unknown Source)
    at java.net.URL.(Unknown Source)
    at java.net.URL.(Unknown Source)
    at hudson.tools.ZipExtractionInstaller.performInstallation(ZipExtractionInstaller.java:83)
    at hudson.tools.InstallerTranslator.getToolHome(InstallerTranslator.java:69)
    at hudson.tools.ToolLocationNodeProperty.getToolHome(ToolLocationNodeProperty.java:109)
    at hudson.tools.ToolInstallation.translateFor(ToolInstallation.java:206)
    at com.cloudbees.jenkins.plugins.customtools.CustomTool.forNode(CustomTool.java:163)
    at com.cloudbees.jenkins.plugins.customtools.CustomTool.forNode(CustomTool.java:65)
    at org.jenkinsci.plugins.workflow.steps.ToolStep$Execution.run(ToolStep.java:152)
    at org.jenkinsci.plugins.workflow.steps.ToolStep$Execution.run(ToolStep.java:133)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Finished: FAILURE

    Thanks,
    Subhash

    ReplyDelete
  16. Hi Amit, how do i define RunSpecifiedTests in the deploy command and how do i mention those specified classes in the DX folder structure, like the way we use to add in build.properties file while deploying using ANT. Any help would be appreciated.
    Thanks

    ReplyDelete
  17. Hi Amit,

    I am still facing issue for toolbelt. I set the path as you mentioned in the Custom tool for toolbelt.
    Any help in this will be much appreciated.
    java.net.MalformedURLException: no protocol:
    at java.net.URL.(Unknown Source)
    at java.net.URL.(Unknown Source)
    at java.net.URL.(Unknown Source)
    at hudson.tools.ZipExtractionInstaller.performInstallation(ZipExtractionInstaller.java:83)
    at hudson.tools.InstallerTranslator.getToolHome(InstallerTranslator.java:69)
    at hudson.tools.ToolLocationNodeProperty.getToolHome(ToolLocationNodeProperty.java:109)
    at hudson.tools.ToolInstallation.translateFor(ToolInstallation.java:206)
    at com.cloudbees.jenkins.plugins.customtools.CustomTool.forNode(CustomTool.java:163)
    at com.cloudbees.jenkins.plugins.customtools.CustomTool.forNode(CustomTool.java:65)
    at org.jenkinsci.plugins.workflow.steps.ToolStep$Execution.run(ToolStep.java:152)
    at org.jenkinsci.plugins.workflow.steps.ToolStep$Execution.run(ToolStep.java:133)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Finished: FAILURE

    Thanks!

    ReplyDelete
    Replies
    1. You need to add Custom Tool "toolbelt" in Global Tool Configuration

      Delete
    2. Remove the auto installer for Custom Tools and SFDX Path in extracted path.

      Delete
    3. Hi Amit/Samrat,
      Thanks for the post. Can you please elaborate the steps for removing Auto Installer. I'm facing the same issue.
      Would really appriciate your reply as it is urgnet for me.

      Delete
    4. This comment has been removed by the author.

      Delete
    5. In Custom Tool, remove the Auto Installer under "toolbelt" and add the SFDX CLI path like below.
      (Example: "C:\Program Files\Salesforce CLI\bin")

      Delete
  18. Hi Amit,

    Thanks for the detailed explanation for CI using SFDX.
    I was trying build(as part of a trailhead module challenge) CI pipeline using Gitlab, i have done everything,
    1- Setup DevHub , 2-Generated certificate using OpenSSL, created Connected app and gave proper profile , 4- Added certificate to Connected app.

    and then granted access _
    sfdx force:auth:jwt:grant --clientid 3MVG9GYWKbMgBvbxdetjB7QiPpcjZ.TcXna3q0e2EKVaVx8v.AtyFsvp0R_iqCOy17niCM.Iw04h8.KVIbTkX --jwtkeyfile ./JWT/server.key --username prakash.sfdc26@curious-bear-mp8v4r.com --setdefaultdevhubusername

    and got "Successfully authorized prakash.sfdc26@curious-bear-mp8v4r.com with org ID 00D4T000000G0vWUAS" message aswell.

    but my challange is failing with error "We could not confirm that you authenticated to your Dev Hub org using JWT-based auth flow with the 'DreamHouse_demo' Connected App".

    I am repeating the above steps from last 2 days, still no luck. I am sure i am missing something minor, If you have any suggestion/idea why its failing, very much appreciate that.

    Thanks, Waiting for you reply,
    Prakash

    ReplyDelete
  19. Hi Amit, That was a good knowledge sharing about jenkins.
    I had followed all of your steps and i'm getting the below Error as :
    ERROR running force:mdapi:deploy: The specified path [manifest/.] does not exist
    C:\Program Files (x86)\Jenkins\workspace\SEGRDHT_>"C:\Program Files\Salesforce CLI\bin\sfdx" force:mdapi:deploy -d manifest/. -u demogmail@gmail.com
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // withCredentials
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    ERROR: script returned exit code 1
    Finished: FAILURE


    Can you please help me out on this what can be the issue.

    ReplyDelete
    Replies
    1. I have the same error over and over again. Did you you get to solve it??

      I would eagerly thank a answer.

      Regards

      Delete
  20. Failed to connect to repository : Error performing command: git.exe ls-remote -h https://github.com/guptaaalokkumar/SFDXProject.git HEAD
    getting above error

    ReplyDelete
  21. So do i have to create a connected app in each environment? I mean how does this work, i'm confused

    ReplyDelete
  22. Hi amith,
    In jenkins groovy script
    @echo off
    del /q /f .\TEMp\classes\*.*
    del /q /f .\TEMp\pages\*.*
    del /q /f .\TEMp\triggers\*.*
    for /f "delims=" %%a in ('type package.xml ^|repl ".*(.*).*" "$1" a ') do (echo "%%a"

    xcopy .\classes\%%a.* .\TEMp\classes\
    xcopy .\pages\%%a.* .\TEMp\pages\
    xcopy .\pages\%%a.* .\TEMp\triggers\ ,,
    here my batch does not reading email template components.remaing all components will taken but email compoents does not taking.I have getting error like this.
    "GSD_Email_Templates/GSDCSCCaseCreation"
    Invalid switch - /GSDCSCCaseCreation.*
    Invalid switch - /GSDCSCCaseCreation.*
    Invalid switch - /GSDCSCCaseCreation.*
    "GSD_Email_Templates/GSDCSCCaseCreationForAlternateAndPrimary"
    Invalid switch - /GSDCSCCaseCreationForAlternateAndPrimary.*
    Invalid switch - /GSDCSCCaseCreationForAlternateAndPrimary.*
    Invalid switch - /GSDCSCCaseCreationForAlternateAndPrimary.*
    "GSD_Email_Templates/GSDCSCCaseCreationForAlternateContact"
    Invalid switch - /GSDCSCCaseCreationForAlternateContact.*
    Invalid switch - /GSDCSCCaseCreationForAlternateContact.*
    Invalid switch - /GSDCSCCaseCreationForAlternateContact.* ..
    can u pls help me on this issue.or else pls give me u r emailid

    ReplyDelete
    Replies
    1. Hi amith,
      In our organization classes,pages,triggers components developer directly checkin to github.remaing all components to retrive and deploy throgh sandbox.my above batch file is working for all components.when in package.xml email template components will get that batch does not retriveing.what can i add in script if email type also getten

      Delete
  23. Hello All,
    After providing the local file path of Salesforce CLI on "Installation directory" under Global Tool Configuration setting in jenkins, does anyone got the following error.
    "java.io.IOException: CreateProcess error=2, The system cannot find the file specified"

    I have provided all the information as suggested by couldn't resolve this.
    Could anyone give me a proper solution to resolve this issue.

    Waiting for suggestions
    Thanks In Advance.

    ReplyDelete
  24. Hi Amit,

    Everything is working fine but the deployment command is failing.


    ERROR:
    C:\Program Files (x86)\Jenkins\workspace\MyDevOrg_>"C:\Program Files\Salesforce CLI\bin\sfdx" force:auth:jwt:grant --clientid 3MVG9n_HvETGhr3Byx97bKgRrkDBI0CqGdtqPIJe8O.yJnXa1HaXNEo4SDB4T9iWT5w3kkrKK_IeuQgDv_dDq --username sfdctraining@popcornapps.com --jwtkeyfile "****" --setdefaultdevhubusername --instanceurl https://login.salesforce.com
    Successfully authorized sfdctraining@popcornapps.com with org ID 00D2w000001fLPsEAM
    [Pipeline] echo
    0
    [Pipeline] isUnix
    [Pipeline] bat
    ERROR running force:source:deploy: ENOENT: no such file or directory, open 'C:\Program Files (x86)\Jenkins\workspace\MyDevOrg_@tmp\secretFiles\8d12d706-94b4-4f1e-92d6-67731f4abb33\server.key'
    C:\Program Files (x86)\Jenkins\workspace\MyDevOrg_>"C:\Program Files\Salesforce CLI\bin\sfdx" force:source:deploy --manifest manifest/package.xml -u sfdctraining@popcornapps.com
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // withCredentials
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    ERROR: script returned exit code 1
    Finished: FAILURE
    Please suggest any better way to resolve my issue

    Thanks,
    Vamsi

    ReplyDelete
  25. Hey.
    I was working on jenkin authorization with salesforce. I use the same configuration as yours and I am encounterd with this error

    " The filename, directory name, or volume label syntax is incorrect. "

    What is the error. Here is my code

    #!groovy
    import groovy.json.JsonSlurperClassic
    node {

    def BUILD_NUMBER=env.BUILD_NUMBER
    def RUN_ARTIFACT_DIR="tests/${BUILD_NUMBER}"
    def SFDC_USERNAME

    def HUB_ORG=env.HUB_ORG_DH
    def SFDC_HOST = env.SFDC_HOST_DH
    def JWT_KEY_CRED_ID = env.JWT_CRED_ID_DH
    def CONNECTED_APP_CONSUMER_KEY=env.CONNECTED_APP_CONSUMER_KEY_DH

    println 'KEY IS'
    println JWT_KEY_CRED_ID
    println HUB_ORG
    println SFDC_HOST
    println CONNECTED_APP_CONSUMER_KEY
    def toolbelt = tool 'toolbelt'

    stage('checkout source') {
    // when running in multi-branch job, one must issue this command
    checkout scm
    }

    withCredentials([file(credentialsId: JWT_KEY_CRED_ID, variable: 'jwt_key_file')]) {
    stage('Deploye Code') {
    if (isUnix()) {
    rc = sh returnStatus: true, script: "${toolbelt} force:auth:jwt:grant --clientid ${CONNECTED_APP_CONSUMER_KEY} --username ${HUB_ORG} --jwtkeyfile ${jwt_key_file} --setdefaultdevhubusername --instanceurl ${SFDC_HOST}"
    }else{
    rc = bat returnStatus: true, script: "${toolbelt} force:auth:jwt:grant --clientid ${CONNECTED_APP_CONSUMER_KEY} --username ${HUB_ORG} --jwtkeyfile \"${jwt_key_file}\" --setdefaultdevhubusername --instanceurl ${SFDC_HOST}"
    }
    if (rc != 0) { error 'hub org authorization failed' }

    println rc

    // need to pull out assigned username
    if (isUnix()) {
    rmsg = sh returnStdout: true, script: "${toolbelt} force:mdapi:deploy -d manifest/. -u ${HUB_ORG}"
    }else{
    rmsg = bat returnStdout: true, script: "\"${toolbelt}\" force:mdapi:deploy -d manifest/. -u ${HUB_ORG}"
    }

    printf rmsg
    println('Hello from a Job DSL script!')
    println(rmsg)
    }
    }
    }

    ReplyDelete



  26. Hi Amit,

    I am getting the following issue. Deployment is failing. I am not sure why deployment command again looking for server.key.


    C:\Program Files (x86)\Jenkins\workspace\SFDX_Build_myfeaturebranch>"C:\Program Files\Salesforce CLI\bin\SFDX" force:auth:jwt:grant --clientid 3MVG9CEn_O3jvv0zQmQ6aqLjcVCgLrbkrSSqu.tbA.zZ4xAda4yMWtSmvxtqB5lllnQw2D3gvudUWSmGa.fUq --username chandan.agarwal1707@gmail.com --jwtkeyfile "****" --setdefaultdevhubusername --instanceurl https://login.salesforce.com
    Successfully authorized chandan.agarwal1707@gmail.com with org ID 00D6A000001h73jUAA
    [Pipeline] echo
    0
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (Deploy the code)
    [Pipeline] bat
    ERROR running force:mdapi:deploy: ENOENT: no such file or directory, open 'C:\Program Files (x86)\Jenkins\workspace\SFDX_Build_myfeaturebranch@tmp\secretFiles\ea031b28-4aad-49f5-9a61-606534022102\server.key'
    C:\Program Files (x86)\Jenkins\workspace\SFDX_Build_myfeaturebranch>"C:\Program Files\Salesforce CLI\bin\SFDX" force:mdapi:deploy -d manifest/. -u chandan.agarwal1707@gmail.com
    389 bytes written to C:\WINDOWS\TEMP\..zip using 622.125ms
    Deploying C:\WINDOWS\TEMP\..zip...
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // withCredentials
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    ERROR: script returned exit code 1
    Finished: FAILURE

    ReplyDelete
  27. https://github.com/forcedotcom/cli/issues/81 its issue with the current cli version. i tested with older version it worked.

    ReplyDelete
  28. Hi Amit,
    i am getting error on Step 4) Create Connected App for JWT-Based Flow.
    below is the command i a running
    sfdx force:auth:jwt:grant --clientid 3MVG9n_HvETGhr3BoWmrZLbLQQXbd0w.qsNhKzOQueWjRwMPBNjvprxLf.UjnNGZ7TJEqaNytZR99OGq5uWwW --jwtkeyfile server.key --username karim.kng@gmail.com --instanceurl https://curious-moose-wz4mnm-dev-ed.my.salesforce.com/ --setdefaultdevhubusername
    ERROR is
    ERROR running force:auth:jwt:grant: We encountered a JSON web token error, whic
    h is likely not an issue with Salesforce CLI. Here's the error: invalid assertio
    n
    please help me to resolve

    ReplyDelete
    Replies
    1. Hello I am also getting the same error. Were you able to resolve it.

      Delete
  29. Hi Amit,

    Thanks for the explanation in this article. I am following your article to authorize one of the sandbox through connected App for JWT-Based Flow in Jenkins installed in AWS but got following error
    ERROR running force:auth:jwt:grant: We encountered a JSON web token error, which is likely not an issue with Salesforce CLI. Here’s the error: audience is invalid

    All parameter values in force:auth:jwt:grant commands seems populated with correct values.

    Please help to resolve this error.

    ReplyDelete
    Replies
    1. I am able to resolve Invalid Audience error when authenticating a sandbox with JWT by setting up an environment variable
      SFDX_AUDIENCE_URL = https://test.salesforce.com

      Delete
  30. Hi, I am getting following error after setting salesforce cli as custom tool
    C:\Program Files (x86)\Jenkins\workspace\NEWPROJECT_>"C:\Program Files\Salesforce CLI\bin\sfdx" force:auth:jwt:grant --clientid 3MVG97quAmFZJfVzb5V8pnm_e_6An0Yi1aPqZuioDJTbiWldkeS9l3CcKFQ5pDWw7vNejANf6dl9Ian9rMTUp --username dvarsha@gmail.com --jwtkeyfile "****" --setdefaultdevhubusername --instanceurl https://login.salesforce.com
    openssl config failed: error:02001003:system library:fopen:No such process
    ERROR running force:auth:jwt:grant: error:2006D080:BIO routines:BIO_new_file:no such file
    [Pipeline] error

    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // withCredentials
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    ERROR: hub org authorization failed
    Finished: FAILURE

    ReplyDelete
  31. I am getting this error. Please solve this:


    C:\Program Files (x86)\Jenkins\workspace\BasicAuto_>"" force:auth:jwt:grant --clientid 3MVG9Y6d_Btp4xp78sJezspGP9HAkWhrS9xWf06s3BjVOSWqIyZa8ESqha55VjkV8PooiKBIB2.1.iS0FO8b1 --username bhavesh@jogi.com --jwtkeyfile "****" --setdefaultdevhubusername --instanceurl https://login.salesforce.com
    '""' is not recognized as an internal or external command,
    operable program or batch file.
    [Pipeline] error
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // withCredentials
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    ERROR: hub org authorization failed
    Finished: FAILURE

    ReplyDelete
    Replies
    1. you are facing issue due to hub org authorization . Try below command on your local laptop.

      sfdx force:auth:jwt:grant --clientid KeyHere --jwtkeyfile c:\openssl\bin\server.key --username bhavesh@jogi.com --instanceurl https://login.salesforce.com --setdefaultdevhubusername

      Delete
    2. Hello Amit,

      i was authorized the org on cmd prompt, the result is success but the issue is not resolved. may i know the reason for that.
      C:\openssl\bin>sfdx force:auth:jwt:grant --clientid 3MVG9n_HvETGhr3C0IBETj._Lt0S9lX9ZfK14tqXRH8SrawNRaIr6RDhFLcSqXa.E1YMj0H0aHLp8n5FU6QQn --jwtkeyfile server.key --username krishna@gmail.com.package --instanceurl https://login.salesforce.com --setdefaultdevhubusername
      Successfully authorized krishna@gmail.com.package with org ID 00D2w000007sFqLEAU

      Delete
  32. Is this code really work for any one? who is able to deployed successfully?

    ReplyDelete
  33. [Pipeline] tool (show)
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    java.net.MalformedURLException: no protocol:
    at java.net.URL.(Unknown Source)
    at java.net.URL.(Unknown Source)
    at java.net.URL.(Unknown Source)
    at hudson.tools.ZipExtractionInstaller.performInstallation(ZipExtractionInstaller.java:83)
    at hudson.tools.InstallerTranslator.getToolHome(InstallerTranslator.java:69)
    at hudson.tools.ToolLocationNodeProperty.getToolHome(ToolLocationNodeProperty.java:109)
    at hudson.tools.ToolInstallation.translateFor(ToolInstallation.java:206)
    at com.cloudbees.jenkins.plugins.customtools.CustomTool.forNode(CustomTool.java:163)
    at com.cloudbees.jenkins.plugins.customtools.CustomTool.forNode(CustomTool.java:65)
    at org.jenkinsci.plugins.workflow.steps.ToolStep$Execution.run(ToolStep.java:152)
    at org.jenkinsci.plugins.workflow.steps.ToolStep$Execution.run(ToolStep.java:133)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Finished: FAILURE

    ReplyDelete
  34. HI Amit,

    I am getting below error message when trying to build application -

    C:\Program Files\Git\cmd\git.exe checkout -f aac1ac93929020b816893c893db07cb9c215d6b2 # timeout=10
    ERROR: Unable to retrieve commit message
    org.eclipse.jgit.errors.MissingObjectException: Missing unknown aac1ac93929020b816893c893db07cb9c215d6b2
    at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:135)
    at org.eclipse.jgit.lib.ObjectReader.open(ObjectReader.java:203)
    at org.eclipse.jgit.revwalk.RevWalk.parseAny(RevWalk.java:917)
    at org.eclipse.jgit.revwalk.RevWalk.parseCommit(RevWalk.java:827)
    at hudson.plugins.git.util.RevCommitRepositoryCallback.invoke(RevCommitRepositoryCallback.java:25)
    at hudson.plugins.git.util.RevCommitRepositoryCallback.invoke(RevCommitRepositoryCallback.java:13)
    at org.jenkinsci.plugins.gitclient.AbstractGitAPIImpl.withRepository(AbstractGitAPIImpl.java:29)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.withRepository(CliGitAPIImpl.java:84)
    at hudson.plugins.git.GitSCM.printCommitMessageToLog(GitSCM.java:1266)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1235)
    at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:125)
    at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:155)
    at org.jenkinsci.plugins.workflow.multibranch.SCMBinder.create(SCMBinder.java:142)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:309)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:428)
    First time build. Skipping changelog.
    ERROR: C:\Windows\system32\config\systemprofile\AppData\Local\Jenkins.jenkins\workspace\TestBuild_@script\Jenkinsfile not found
    Finished: FAILURE

    I have check location and file is present there. Can you please provide some inputs on it.

    ReplyDelete
  35. HI Amit,

    Above issue is resolved. Can you please let me know how tomove triggers using above integration.

    Thanks,

    ReplyDelete
  36. I am getting this error.

    ERROR running force:auth:jwt:grant: We encountered a JSON web token error, which is likely not an issue with Salesforce CLI. Here’s the error: invalid client credentials


    Please anyone help me?

    ReplyDelete
  37. First of all . Thanks a lot for the wonderful detailed article .I have never executed Jenkins and sfdx in windows before (only in AWS) so some of this was new to me.

    I faced some problem with the toolbelt error in groovy (22nd Minute in the video) and wasn't able to configure sfdx to be used from custom tools plugin.

    WORKAROUND --> Since we are already installing sfdx on our local , once SFDX is configured in the PATH variable for windows , it can be invoked using the 'bat' command in groovy.
    Eg:

    stage('Deploy Code') {
    bat 'sfdx --version';
    bat "sfdx force:auth:jwt:grant --clientid ${CONNECTED_APP_CONSUMER_KEY} --username ${HUB_ORG} --jwtkeyfile ${jwt_key_file} --setdefaultdevhubusername --instanceurl ${SFDC_HOST}";




    ReplyDelete
    Replies
    1. Hi PJ, I have sfdx cli installed on my computer and I get the error "sfdx is not recognised".
      I just write bat 'sfdx --version'

      Could you why this?

      Delete
  38. I am getting error and I am unable figure out its coming along but its failing when authenticate to sandbox. Please respond when possible.

    ReplyDelete
  39. This comment has been removed by the author.

    ReplyDelete
  40. C:\Program Files (x86)\Jenkins\workspace\MySFDX_>"" force:auth:jwt:grant --clientid 3MVG9U_dUptXGpYLfoQ.nOJMrfS3DP5vBtbUe0K9jt4elPoV0G9HMR8m1s4s9PQQDa12fSHyGN8bd35I.2GsU --username rameshnc@gmail.com.ramesh --jwtkeyfile "****" --setdefaultdevhubusername --instanceurl https://test.salesforce.com
    '""' is not recognized as an internal or external command,
    operable program or batch file.
    [Pipeline] error
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // withCredentials
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    ERROR: hub org authorization failed
    Finished: FAILURE

    ReplyDelete
  41. I got resolved above ERROR: hub org authorization failed issue.

    === Status
    Status: Queued
    jobid: 0Af4T000009dDbXSAU

    The deploy request did not complete within the specified wait time [0 minutes].
    To check the status of this deployment, run "sfdx force:mdapi:deploy:report"

    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // withCredentials
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    [Checks API] No suitable checks publisher found.
    Finished: SUCCESS

    ReplyDelete
    Replies
    1. Can you please tell us , how you resolved this error "ERROR: hub org authorization failed" ?

      Delete
  42. May be a novice question. Do we need to self signed ssl certificate for this as we are installing in our local system only, or is it for added security.

    ReplyDelete
  43. @Ramesh Vemulapalli23 : Can you please tell us , how you resolved this error "ERROR: hub org authorization failed" ?

    ReplyDelete
  44. @Ramesh Vemulapalli23 : Can you please tell us , how you resolved this error :-
    '""' is not recognized as an internal or external command,
    operable program or batch file.


    "ERROR: hub org authorization failed"

    ReplyDelete
    Replies
    1. Under Custom tool configuration in the "Installation directory" text box give like this "\sfdx" ie "C:\Program Files\Salesforce CLI\bin\sfdx"

      Delete
  45. Hi Amit,
    I face the similar issue like others but could not find any solution above.
    When I run the build I get below error:
    Please help as soon as possible.

    3MVG9fe4g9fhX0E7aM1il19gyOJY_HeDL7dShGlMxSfJ604WWofN4vVVLR268cZj.N7qGr.1jxbo79USda_je
    [Pipeline] tool
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    java.net.MalformedURLException: no protocol:
    at java.net.URL.(URL.java:610)
    at java.net.URL.(URL.java:507)
    at java.net.URL.(URL.java:456)
    at hudson.tools.ZipExtractionInstaller.performInstallation(ZipExtractionInstaller.java:83)
    at hudson.tools.InstallerTranslator.getToolHome(InstallerTranslator.java:69)
    at hudson.tools.ToolLocationNodeProperty.getToolHome(ToolLocationNodeProperty.java:109)
    at hudson.tools.ToolInstallation.translateFor(ToolInstallation.java:217)
    at com.cloudbees.jenkins.plugins.customtools.CustomTool.forNode(CustomTool.java:164)
    at com.cloudbees.jenkins.plugins.customtools.CustomTool.forNode(CustomTool.java:66)
    at org.jenkinsci.plugins.workflow.steps.ToolStep$Execution.run(ToolStep.java:152)
    at org.jenkinsci.plugins.workflow.steps.ToolStep$Execution.run(ToolStep.java:133)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
    Finished: FAILURE

    ReplyDelete
    Replies
    1. Same error did you find any solution ?

      Delete
  46. I am getting server.csr: No such file or directory . Any one please help

    ReplyDelete
  47. I am getting error while generating SSL certificate " server.csr: No such file or directory". I used the given command

    ReplyDelete