2

I'm trying to set up sfdx on Bitbucket Pipelines.

It's going well but I'm facing a weird issue when using the steps feature.

Here's a snippet of my build file:

- step:
            name: Auth. with DevHub
            script:
                - sfdx force:auth:jwt:grant -i $CLIENT_ID -f jwt/server.key -u $DEVHUB_USERNAME --setdefaultdevhubusername --setalias DevHub
                - sfdx force:org:list
        - step:
            name: Create Scratch Org.
            script:
                - sfdx force:org:list
                - sfdx force:org:create -f environments/scratchorg1-scratch-def.json -v DevHub -a bitbucket_build_$BITBUCKET_BUILD_NUMBER

Here's what the first force:org:list outputs:

+ sfdx force:org:list
=== Orgs
     ALIAS   USERNAME                            ORG ID              CONNECTED STATUS
───  ──────  ──────────────────────────────────  ──────────────────  ────────────────
(D)  DevHub  davin.casey+sfdx-trial@pexlify.com  00D1r000002cUfcEAE  Connected
No active scratch orgs found. Specify --all to see all scratch orgs

and the second:

+ sfdx force:org:list
ERROR:  No orgs can be found. 
Try this:
Use one of the commands in force:auth or force:org:create to add or create new scratch orgs.

When you read the documentation for Pipelines, that appears to be expected behaviour:

Each step in your pipeline will start a separate Docker container to run the commands configured in the script. Each step can be configured to:

Use a different Docker image. Configure a custom max-time. Use specific caches and services. Produce artifacts that subsequent steps can consume.

So it's spinning up a fresh image for each step, but we can pass artifacts to subsequent steps. Fine.

So I updated my build file to look like:

- step:
                name: Auth. with DevHub
                script:
                    - sfdx force:auth:jwt:grant -i $CLIENT_ID -f jwt/server.key -u $DEVHUB_USERNAME --setdefaultdevhubusername --setalias DevHub
                    - sfdx force:org:list
                artifacts:
                    - .sfdx/**
                    - config/**
                    - environments/**
                    - sfdx-project.json
                    - jwt/**
            - step:
                name: Create Scratch Org.
                script:
                    - sfdx force:org:list
                    - sfdx force:org:create -f environments/scratchorg1-scratch-def.json -v DevHub -a bitbucket_build_$BITBUCKET_BUILD_NUMBER
                artifacts:
                    - .sfdx/**
                    - config/**
                    - environments/**
                    - sfdx-project.json
                    - jwt/**

Now, I can see the artifacts being prepared & used in each step.

'Auth. with DevHub teardown':

Build teardown
10s
Searching for artifacts matching .sfdx/**
Compressing artifact /opt/atlassian/pipelines/agent/build/.sfdx/orgs/test-oitz1ujqhgka@example.com/metadataTypeInfos.json
Compressing artifact /opt/atlassian/pipelines/agent/build/.sfdx/orgs/test-oitz1ujqhgka@example.com/sourcePathInfos.json.bak
Compressing artifact /opt/atlassian/pipelines/agent/build/.sfdx/orgs/test-oitz1ujqhgka@example.com/sourcePathInfos.json
Compressing artifact /opt/atlassian/pipelines/agent/build/.sfdx/sfdx-config.json
Compressed artifact in 0 seconds
Uploading artifact of 10 KB
Successfully uploaded artifact in 1 seconds
Searching for artifacts matching config/**
Compressing artifact /opt/atlassian/pipelines/agent/build/config/testorg11384827171-scratch-def.json
Compressing artifact /opt/atlassian/pipelines/agent/build/config/scratchorg1-scratch-def.json
Compressing artifact /opt/atlassian/pipelines/agent/build/config/project-scratch-def.json
Compressing artifact /opt/atlassian/pipelines/agent/build/config/sfdx-trial-scratch-def.json
Compressed artifact in 0 seconds
Uploading artifact of 739 bytes
Successfully uploaded artifact in 1 seconds
Searching for artifacts matching environments/**
Compressing artifact /opt/atlassian/pipelines/agent/build/environments/project-scratch-def.json
Compressed artifact in 0 seconds
Uploading artifact of 241 bytes
Successfully uploaded artifact in 1 seconds
Searching for artifacts matching sfdx-project.json
Compressing artifact /opt/atlassian/pipelines/agent/build/sfdx-project.json
Compressed artifact in 0 seconds
Uploading artifact of 278 bytes
Successfully uploaded artifact in 1 seconds
Searching for artifacts matching jwt/**
Compressing artifact /opt/atlassian/pipelines/agent/build/jwt/server.csr
Compressing artifact /opt/atlassian/pipelines/agent/build/jwt/server.crt
Compressing artifact /opt/atlassian/pipelines/agent/build/jwt/server.key
Compressed artifact in 0 seconds
Uploading artifact of 2 KB
Successfully uploaded artifact in 1 seconds

'Create Scratch Org. build setup':

Artifact ".sfdx/**": Downloading
Artifact ".sfdx/**": Downloaded 10 KB in 0 seconds
Artifact ".sfdx/**": Extracting
Artifact ".sfdx/**": Extracted in 0 seconds
Artifact "config/**": Downloading
Artifact "config/**": Downloaded 739 bytes in 0 seconds
Artifact "config/**": Extracting
Artifact "config/**": Extracted in 0 seconds
Artifact "environments/**": Downloading
Artifact "environments/**": Downloaded 239 bytes in 0 seconds
Artifact "environments/**": Extracting
Artifact "environments/**": Extracted in 0 seconds
Artifact "sfdx-project.json": Downloading
Artifact "sfdx-project.json": Downloaded 278 bytes in 0 seconds
Artifact "sfdx-project.json": Extracting
Artifact "sfdx-project.json": Extracted in 0 seconds
Artifact "jwt/**": Downloading
Artifact "jwt/**": Downloaded 2 KB in 0 seconds
Artifact "jwt/**": Extracting
Artifact "jwt/**": Extracted in 0 seconds
Artifact ".sfdx/**": Downloading
Artifact ".sfdx/**": Downloaded 10 KB in 0 seconds
Artifact ".sfdx/**": Extracting
Artifact ".sfdx/**": Extracted in 0 seconds
Artifact "config/**": Downloading
Artifact "config/**": Downloaded 739 bytes in 0 seconds
Artifact "config/**": Extracting
Artifact "config/**": Extracted in 0 seconds
Artifact "environments/**": Downloading
Artifact "environments/**": Downloaded 241 bytes in 1 seconds
Artifact "environments/**": Extracting
Artifact "environments/**": Extracted in 0 seconds
Artifact "sfdx-project.json": Downloading
Artifact "sfdx-project.json": Downloaded 278 bytes in 0 seconds
Artifact "sfdx-project.json": Extracting
Artifact "sfdx-project.json": Extracted in 0 seconds
Artifact "jwt/**": Downloading
Artifact "jwt/**": Downloaded 2 KB in 0 seconds
Artifact "jwt/**": Extracting
Artifact "jwt/**": Extracted in 0 seconds
Artifact ".sfdx/**": Downloading
Artifact ".sfdx/**": Downloaded 10 KB in 0 seconds
Artifact ".sfdx/**": Extracting
Artifact ".sfdx/**": Extracted in 0 seconds
Artifact "config/**": Downloading
Artifact "config/**": Downloaded 739 bytes in 0 seconds
Artifact "config/**": Extracting
Artifact "config/**": Extracted in 0 seconds
Artifact "environments/**": Downloading
Artifact "environments/**": Downloaded 241 bytes in 0 seconds
Artifact "environments/**": Extracting
Artifact "environments/**": Extracted in 0 seconds
Artifact "sfdx-project.json": Downloading
Artifact "sfdx-project.json": Downloaded 278 bytes in 0 seconds
Artifact "sfdx-project.json": Extracting
Artifact "sfdx-project.json": Extracted in 0 seconds
Artifact "jwt/**": Downloading
Artifact "jwt/**": Downloaded 2 KB in 0 seconds
Artifact "jwt/**": Extracting
Artifact "jwt/**": Extracted in 0 seconds

So they appear to be working correctly, however the orgs are STILL not carrying across to subsequent steps.

Does anyone know why this might be? I assume the org. list is being stored in a local file somewhere, but I've added everything I can see as an artifact so I've no idea where else to look. Thanks!

4

After you login, can you run something like sfdx force:org:display --verbose -u DevHub --json | jq -r .result.sfdxAuthUrl to get the auth URL, store the value securely somewhere, then run sfdx force:auth:sfdxurl:store -f <secure location> -a DevHub -d in each step?

This is how I solve a similar challenge across build/release phases for the Salesforce Buildpack in Heroku Pipelines.

Edit: I just remembered that we don't generate the sfdxAuthUrl if the org authenticated through jwt:grant because the flow doesn't return a refresh token. I avoid this issue in the buildpack by asking people to manually auth to their dev hub locally then generating the sfdxAuthUrl. Perhaps you can do that and then store it in some kind of config var on Bitbucket?

4
  • Thanks for the reply Wade, this worked perfectly. I've no problem generating the file manually, it only needs to be done once anyway. – Davin Casey May 30 '18 at 13:45
  • Glad it helped! – Wade Wegner May 30 '18 at 13:55
  • 1
    Actually I'm getting a follow on problem. In one step I auth using the json file method and then create a scratch org, that works perfectly. Next step I reauth using the same file (successfully) but then force:org:list returns no scratch orgs (only the DevHub) so my next step (push to scratch org) fails. Any idea why that might be? – Davin Casey May 30 '18 at 14:33
  • 2
    Yes: wadewegner.com/2018/01/jwt-oauth-with-scratch-orgs You might need to stick with JWT. Two thoughts: 1) use only one step or 2) re-auth with JWT at the beginning of each step. – Wade Wegner May 30 '18 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.