[SalesForce] How to push code to production with Visual Studio Code & DX

I am using Visual Studio Code to develop new functionality.

However, after completing the testing in the sandbox, I want to push the code to production. When I choose "SFDX: Deploy Code to org", it fails with errors –

INVALID_OPERATION: rollbackOnError option must be true on a production org

and

INVALID_OPERATION: testLevel of NoTestRun cannot be used in production organizations

The code is in the new "source format", but I'm not developing in scratch orgs (I started the project with "SFDX: Create Project with Manifest").

Question 1: Do I need to convert the source to Metadata API format before I can deploy to production? This is not how the instructions in release notes are written:

To deploy source to an org without source tracking (to an org that’s
not a scratch org), right-click a manifest, a source file, or a
directory in the Visual Studio Code explorer. Select SFDX: Deploy
Source to Org.

https://releasenotes.docs.salesforce.com/en-us/winter19/release-notes/rn_vscode_any_org.htm

However, the Salesforce DX Developer Guide seems to imply it:

Release Your App to Production

After you convert from source format to metadata format, and package metadata from one org, you can release your app in a different org.

https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_build_mdapi_production.htm

Question 2: If I don't need to convert the source format and can push to production directly, how do I set up Visual Studio Code / CLI to avoid the two errors?

Best Answer

You have two choices. First, you can use force:package commands to create an unlocked package. You can install this in production with force:package:install; it is similar to installing an AppExchange package. Otherwise, you would need to use force:source:convert to get a mdapi style format, then use force:mdapi:deploy with the relevant test level (e.g. RunLocalTests) and set rollbackOnError to true. Check out the Salesforce CLI Reference for specific details, or sfdx help force:<command> for command-level help direct from the console. I do not believe that you're currently allowed to create unlocked packages directly via the GUI, but you can open a terminal by using the Terminal > Create New Terminal option, which will open a sfdx environment by default.