Salesforce CI/CD Pipeline Gitlab

 

Salesforce CI/CD Pipeline | Gitlab

In this article, we will be setting up GitlabCI/CD Pipeline for Salesforce.


Prerequisites



Setting up Gitlab for Salesforce Project

  1. Create a Gitlab Account. You can use this link for signing up

  2. Login to the Gitlab Account.

  3. Create a gitlab Repository with Name sfdx demo.

  4. Now clone the repository in the VS Code.  To do that go to your repository and click on the clone button.

  1. Now click on the Clone in VS Code to clone this Repository in the Vs Code.



  Or alternatively, you can use git clone repository url   in the terminal.

  1. In my case, a folder name SFDX-DEMO is created. Now open the parent folder of git repository folder in the terminal and use the following command.
    sfdx force:project:create -n sfdx-demo -x
    Please Make sure to use the correct repository folder name. In my case the repository folder name is sfdx-demo. This will convert your git repo folder into a Salesforce Project Folder

  2. Now open the git Repository Folder in the VS Code. In my case Folder Name is sfdx-demo.

  3. Now You can authorize the git repository folder with the salesforce org.

  4. Now you can use the following commands to make your first push to the repository.
    git add .
    git commit -m ‘First Salesforce Project Commit’
    git push

  5. That’s all, Now you have Successfully Setup Gitlab for the Salesforce Project.


Create a Self-Signed SSL Certificate and Private Key

  1. Create a folder on your PC to store the generated files, Open terminal and use command :
    mkdir certificates

  2. Generate an RSA private key, Use Command :
    openssl genrsa -des3 -passout pass:<password> -out server.pass.key 2048

  3. Create a key file from the server.pass.key file using the same password from before, use Command :
    openssl rsa -passin pass:<password> -in server.pass.key -out server.key

  4. Delete the server.pass.key, Command for Mac/Linux rm server.pass.key. Command for windows del server.pass.key

  5. Request and generate the certificate, when prompted for the challenge password press enter to skip the step, Use Command :
    openssl req -new -key server.key -out server.csr

  6. Generate the SSL certificate, Use Command :
    openssl x509 -req -sha256 -days 365 -in server.csr -signkey server.key -out server.crt



Connected App Creation/Configuration

1. Open Setup, In the Quick Find Box, Enter App Manager and select App Manager.

2. Now click on New Connected App.

 


3. Now Enter the connected name app as sfdx-demo.

4. Enter your Contact email as your email address and Select Enable OAuth Settings.

5. Enter the callback URL :  http://localhost:1717/OauthRedirect

6.  Select Use digital signatures.  To upload your server.crt file, click Choose File.


7. Now add the following permission for the oauth settings.

Manage user data via APIs (api)

Manage user data via Web browsers (web)

Perform requests at any time (refresh_token, offline_access).

8. Now click on save.



Edit Connected app policies

Now we will be editing the connected app policies to let user login without authorization.

Steps for the same are: 

1. Go to Setup => Manage Connected Apps. Click on the App sfdx demo

2. Click Edit Policies.

3. In the OAuth policies section, for Permitted Users select Admin approved users are pre-authorized, then click OK.

4. Click Save.


Create a Permission Set for Connected App

Lastly, create a permission set and assign pre-authorized users for this connected app.


1. From Setup, enter Permission in the Quick Find box, then select Permission Sets.

2. Click New.

3. For the Label, enter: sfdx demo

4. Click Save.

5. Go to Manage Assignments | Add Assignments.

6. Select the checkbox next to your Dev Hub username, then click Assign | Done.

7. Go back to your connected app.

  1. From Setup, enter App Manager in the Quick Find box, then select App Manager.

  2. Next to sfdx demo, click the list item drop-down arrow (), then select Manage.

  3. In the Permission Sets section, click Manage Permission Sets.

  4. Select the checkbox next to sfdx sfdx demo, then click Save.



Setting up VS Code for the CI/CD

In the above steps we have successfully setup Salesforce , Gitlab for CI CD.

Here in this section we are going to setup VS code for the CI CD.


In the above steps we have successfully created a Salesforce Project with the name sfdx demo.  Open the sfdx demo Project in the VS Code and follow the steps below to create a Salesforce Project.


  1. Create a keys folder in the sfdx-demo Project.  

  2. In the above steps we created server.crt, server.key files. We used server.crt file for the connected app. Here we are going to use server.key file. Place server.key file in the keys folder.


Testing the JWT Auth Flow

Enter the following  command in the terminal to test the Authentication flow.


sfdx force:auth:jwt:grant –clientid “<connected app consumer key>” --username “<devhub username>” --jwtkeyfile “<Path to the server.key file>” --setdefaultdevhubusername --setalias sfdx-demo --instanceurl "<Instance URL>"


Command Description :

"<connected app consumer key>" => Place holder for Connected App Consumer Key

"<devhub username>"  => Placeholder for username of user to whom we have assigned sfdx demo permission set

“<Path to the server.key file>” => Placeholder for server.key file Path Relative Path of the server.key file. It should be like “keys/server.key” 

"<Instance URL>"  => Placeholder for Salesforce Instance URL For Developer/Production Org the value is https://login.salesforce.com/, For Sandbox org the value is https://test.salesforce.com/.


Note : With above command you can log in to the Org in the terminal. Now you should be able to access sfdx commands to deploy/retrieve. The access is granted via Connected App. So the access will be limited to what access we are providing via the connected App.


Setting Up Gitlab Variables

Go to Repository => Settings => CICD. Now add the following Repository Variables.  For more information on the CI/CD gitlab Variables click this link 


  1. Set Up CONSUMER_KEY,  value is Consumer Key of the Connected App.

  2. Set Up HUB_USERNAME,  value is username of the dev Hub User. (User to which sfdx-demo permission set is assigned).

  3. Set Up INSTANCE_URL,  the URL for the salesforce Instance. For Developer/Production Org the value is https://login.salesforce.com/, For Sandbox org the value is https://test.salesforce.com/.


This is all for the Gitlab Variables Setup


Configuring Gitlab YML File

Create a .gitlab-ci.yml file in the project folder. Content of .gitlab-ci.yml File can be copied from here.


Making First Commit, CI CD in Action

Now that we have successfully Setup Gitlab CI/CD. Let’s start with the first deployment.

  1. We need some files in the project to be deployed. For that go to package.xml file. Right click and Select SFDX: Retrieve Source from Org

  2. Now use the following git commands to commit and Push.

git add .

git commit -m 'Commit Message’

git push

  1. Once the data is pushed to the Gitlab . The CI/CD pipeline will start to run. To check the CI CD Pipeline. Go to the Repository =>CI CD => Pipeline.


Example Screenshot :



Congratulations, Now you have successfully Setup Git CI/CD Pipeline. Hurray !!!!


Comments

Popular posts from this blog

Salesforce CI/CD Pipeline | Bitbucket

Salesforce FLS Exporter