Showing posts with label SalesforceDx. Show all posts
Showing posts with label SalesforceDx. Show all posts

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