SFDX - Create and Deploy Project to Dev Hub using VS Code

In this blog post we are going to create a sample project in Scratch Org and deploy it to Dev Hub using VisualStudio Code using SFDX commands

Create DevHub Account: In order to create a Scratch Org we need to have a DevHub Account which can be created using this Link. This DevHub is a free account and is valid only for 30 days to use!

Create Scratch Org: Once we have our DevHub Account created and now we can go ahead and create our Scratch Org using below commands Also make sure you have CLI installed for Windows/Mac using Install the Salesforce CLI steps

Now lets go ahead and authorize DevHub using "sfdx force:auth:web:login -d -a MyDevHub" from terminal and this will prompt you to allow access screen and once authentication is successful you will see a message saying 'You may now close the browser' like below

Now that we have authenticated our DevHub, let's check logging into it using alias name 'MyDevHub' which we gave in above step. From terminal use this "sfdx force:org:open -u MyDevHub" command

Create Project: We will need to create a Project to work with. Lets go ahead and use this command "sfdx force:project:create -n MySfdxProject" to create Project. Here 'MySfdxProject' is my folder path for creating project files
The above will create below list for files under MySfdxProject folder
a. Readme.md
b. force-app
c. config
d. sfdx-project.json

Create Scratch Org: We have DevHub and Project created and now we are ready to create our Scratch Org. To do this we first have to navigate to our Project folder, in our case it is 'MySfdxProject' and then use this command "sfdx force:org:create -s -f config/project-scratch-def.json -a MyScratchOrg1" to create a Scratch Org

Deploy Project to DevHub using VS Code: We are going to use VisualStudio Code to create some metadata and push those to Scratch Org and DevHub.
From VS Code open your project folder (in this case it is 'MySfdxProject') and that will open all our project file that we created earlier

For our demo on how to deploy code to DevHub we are going to create an Apex Class and Lightning Component and get those deployed to DevHub

From VS Code Terminal window we will execute below commands
To Create Apex Class: "sfdx force:apex:class:create -n MyTestClass"

To Create Lightning Component: "sfdx force:lightning:component:create -n MyTestComp". Here make sure you are in 'main' folder before executing this command else you might get an exception

Now we have two now components created in our local so lets go ahead and push those changes to our Scratch Org using below command
To Push changes to Scratch Org: "sfdx force:source:push"

We are now ready to move our source to DevHub and in order to do that we are first going to convert our project to Metadata Api format and then use it for deploy. Use below commands to convert and deploy!
To convert: "force:source:convert". This will create a folder that we can use to do our deployment to DevHub

To deploy: "sfdx force:mdapi:deploy -d deploy -u reddydev2018devhub1@gmail.com". Finally we are ready to deploy our source to DevHub and we are going to use this command process this deploy.

Note: Here I have renamed my 'metadataPackage_1519167275001' folder to 'deploy'

To check status: "sfdx force:mdapi:deploy:report" We can check the deployment status using this command

Thank you!


Popular posts from this blog

Displaying Toast Message from Modal in Lightning Components

Lightning RecordForm - An enhanced Lightning Data Service

Lightning Component to display dynamic sObject data