You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

sfdx-predeploy-hook-org-env

Package Overview
Dependencies
1
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

sfdx-predeploy-hook-org-env

sfdx predeploy hook to export target org details as environment variables


Version published
Weekly downloads
182
decreased by-12.92%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

sfdx-predeploy-hook-org-env

sfdx predeploy hook to export target org details as environment variables

[!IMPORTANT]

This plugin currently only works with sfdx force:source:push and sfdx force:source:deploy.

The sf project deploy start command does not (yet) emit the predeploy hook.

Use Case

Example: When deploying Metadata of type Portal you'll need to provide an <admin> which is a username in the org. The Metadata String Replacements feature already supports replacing strings with values stored in a file or in an environment variable.

sfdx-project.json

{
  ...
  "replacements": [
    {
      "filename": "force-app/main/default/portals/Customer Portal.portal-meta.xml",
      "stringToReplace": "admin@example.com",
      "replaceWithEnv": "ORG_USERNAME"
    }
  ]
}

Let's assume that for a Scratch Org we create, we simply want to make the default scratch org user the admin of the portal.

So in this example, we'll set the environment variable ORG_USERNAME, but first we need to get the default scratch org username from the org:

export ORG_USERNAME="$(node -pe 'JSON.parse(fs.readFileSync(0, "utf8")).result.username' < <(sfdx force:org:display -u my-target-org --json))"
sfdx force source deploy -p "force-app/main/default/portals/Customer Portal.portal-meta.xml" -u my-target-org

Although this works just fine, we need to remember to set this environment variable before deploying or pushing.

Note

For common things like username, email address, org id, it would be handy to have these environment variables with target specific values built-in.

And this is exactly what this sfdx plugin does!

Installation

sfdx plugins install sfdx-predeploy-hook-org-env

Usage

Once you've installed this sfdx plugin, you can immediately use the following environment variables mentioned for the Metadata String Replacements:

Environment VariableDescriptionExample
SFDX_TARGET_ORG_USERNAMEThe username of the target org userjohn.doe@example.com
SFDX_TARGET_ORG_IDThe id of the target org00D7g0000006RKmEAM
SFDX_TARGET_ORG_USER_EMAILThe email of the target org userjohn.doe@gmail.com
SFDX_TARGET_ORG_USER_FIRSTNAMEThe first name of the target org userJohn
SFDX_TARGET_ORG_USER_LASTNAMEThe last name of the target org userDoe
SFDX_TARGET_ORG_USER_DISPLAYNAMEThe display name of the target org userJohn Doe
SFDX_TARGET_ORG_USER_IDThe id of the target org user0058F000002RfcKQAS

Note

Do you have ideas for other target specific values which could be useful?

Please get in touch by creating an issue.

Example:

Make sure your sfdx-project.json contains some replacements using one of the environment variables listed above.

sfdx-project.json:

{
  "packageDirectories": [
    {
      "path": "force-app",
      "default": true
    }
  ],
  "replacements": [
    {
      "filename": "force-app/main/default/portals/Customer Portal.portal-meta.xml",
      "stringToReplace": "admin@example.com",
      "replaceWithEnv": "SFDX_TARGET_ORG_USERNAME"
    },
    {
      "filename": "force-app/main/default/portals/Customer Portal.portal-meta.xml",
      "stringToReplace": "admin@gmail.com",
      "replaceWithEnv": "SFDX_TARGET_ORG_USER_EMAIL"
    }
  ],
  "sourceApiVersion": "57.0"
}

Now you can run sfdx force source push or sfdx force source deploy and the Metadata replacement will automatically use the dynamically generated environment variables for the given target org:

sfdx force source deploy -p "Portal:Customer Portal" -u my-target-org1
sfdx force source deploy -p "Portal:Customer Portal" -u my-target-org2
sfdx force source deploy -p "Portal:Customer Portal" -u my-target-org3

Debugging

To preview the environment variables, set the environment variable DEBUG to *:sfdx-predeploy-hook-org-env:* and perform a validation deployment to the target org.

Example

MacOS/Linux:

$ DEBUG="*:sfdx-predeploy-hook-org-env:*" sfdx force source deploy --checkonly -u mytargetorg -p force-app

Windows PowerShell:

$env:DEBUG="*:sfdx-predeploy-hook-org-env:*"
sfdx force source deploy --checkonly -u mytargetorg -p force-app

This will output something like:

...
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy Setting environment variables for target org +2s
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_ID="00D7g0000006RKmEAM"
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USERNAME="john.doe@example.com"
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_ID="0058F000002RfcKQAS"
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_EMAIL="john.doe@gmail.com"
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_FIRSTNAME="John"
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_LASTNAME="Doe"
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_DISPLAYNAME="John Doe" +0ms
...

Keywords

FAQs

Package last updated on 23 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc