# salesforce-components-gda

We are using Salesforce CLI to retrieve and deploy components. For Jenkins deployments, we use `sfdx` inside of a Dockerfile which runs on Jenkins. See deploy.sh and Dockerfile in this repository for more information. Locally you have the option to use `sf` or `sfdx`.

### Requirements
* sfdx must be installed. Installation instructions here: https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_setup_install_cli.htm
* Installation provides you with both sf and sfdx to use for local development. More details here: https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_setup_sf_intro.htm

### Usage

#### Authorization

Authorize a Salesforce instance, in this case `qaorchard` using sfdx

Note: you can do similar authorization via `sf login org`. More details here: https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_setup_sf_sfdx_command_mapping.htm

Run the following command which will prompt you to login to the `qaorchard` sandbox via browser
```
$ sfdx auth:web:login --setalias qaorchard
```

Primarily, we're interested in retrieving components from a Saleforce instance or deploying components to a destination Salesforce instance. The package.xml file is a manifest which specifies the components of the package.

`$ cd sfdx-project`

Using sf to retrieve from a sandbox (e.g. `qaorchard` sandbox) based on manifest (package.xml file)
```
$ sf retrieve metadata --manifest manifest/package.xml -o qaorchard
```

Using sf to retrieve from sandbox based on directory structure
```
$ sf retrieve metadata --source-dir gda-components/ -o qaorchard
```

Using sf to deploy to a destination (e.g. `qaorchard` sandbox) based on manifest (package.xml file)
```
$ sf deploy metadata --manifest manifest/package.xml -o qaorchard
```

### Deploy script
Our deploy script uses Oauth tokens and sfdx. To replicate, you must first get an access token.

Get secrets from AWS Secrets Manager for the destination environment, e.g. `qaorchard/salesforce/sf.consumer_key`

```
curl --location --request POST 'https://test.salesforce.com/services/oauth2/token' \
--data-urlencode 'grant_type=refresh_token' \
--data-urlencode 'client_id=XXXX' \
--data-urlencode 'refresh_token=XXXX' \
--data-urlencode 'client_secret=XXXXX'
```

Grab the returned access token and use it to interact with SFDX.

Using sfdx to retrieve from sandbox based on package.xml
```
$ sfdx force:source:retrieve --manifest manifest/package.xml --targetusername replace_with_access_token
```

Using sfdx to deploy to a destination (e.g. `qaorchard` sandbox) based on manifest (package.xml file) and run tests

```
sfdx force:source:deploy --manifest manifest/package.xml --targetusername replace_with_access_token -l RunLocalTests
```



