Can't access unsecured variable from pipelines in script

Viewed 273

I'm trying to use the technique of base64 encoding my .env file into a pipelines unsecured variable as described in the following places, but the file that is created is empty:

How can I run a env variables on bitbucket pipeline?

How to pass variables to credentials.json in bitbucket-pipelines?

image: node:16
definitions:

  caches:
    yarncustom: /usr/local/share/.cache/yarn
  steps:
    - step: &Build-step
        name: Build
        caches:
          - node
          - yarncustom
        script:
          - echo $ENV_FILE | base64 -d > .env.local
          - cat .env.local
          - yarn install
          - yarn build
        artifacts:
          - .next/**
          - node_modules/**
          - .env.local
    - step: &Deploy-step
        name: Deploy
        script:
          - pipe: atlassian/rsync-deploy:0.4.3
            variables:
              USER: $USER
              SERVER: $SERVER
              REMOTE_PATH: $REMOTE_PATH
              LOCAL_PATH: '.'
              EXTRA_ARGS: '--exclude-from=deployment-exclude-list.txt'
pipelines:
  branches:
    development:
      - step: *Build-step
      - step:
          <<: *Deploy-step
          deployment: test

I have tried running the following which works, which means that the variable is simply not there, although everything I have read suggests this should be working.

echo randomBase64String | base64 -d > .env.local

Screenshot of Bitbucket Piplelines to prove it exists and is unsecured:

enter image description here

What could I be missing?

2 Answers

You are not passing the correct deployment to the Build-step.

As can be seen on the screenshot, the ENV_FILE variable is defined in the Test deployment, so you'll need to change your pipeline to:

pipelines:
  branches:
    development:
      - step:
          <<: *Build-step
          deployment: Test # This is required to fix your problem
      - step:
          <<: *Deploy-step
          deployment: Other-Deployment # Don't attempt to use Test deployment twice as it's not allowed.

I found that you can't access deployment variables between steps. But you can access repository level variables in any step. So I created a new step to get the repo level variable and save it as an artefact for later steps. If anyone has any improvements on this I would love to hear it!

image: node:16
definitions:

  caches:
    yarncustom: /usr/local/share/.cache/yarn
  steps:
    - step: &Dev-step
        name: Env
        script:
          - echo $DEV_ENV | base64 -di > .env.local
        artifacts:
          - .env.local
    - step: &Build-step
        name: Build
        caches:
          - node
          - yarncustom
        script:
          - yarn install
          - yarn build
        artifacts:
          - .next/**
          - node_modules/**
    - step: &Deploy-step
        name: Deploy
        script:
          - pipe: atlassian/rsync-deploy:0.4.3
            variables:
              USER: $USER
              SERVER: $SERVER
              REMOTE_PATH: $REMOTE_PATH
              LOCAL_PATH: '.'
              EXTRA_ARGS: '--exclude-from=deployment-exclude-list.txt'
pipelines:
  branches:
    development:
      - step:
          <<: *Dev-step
      - step:
          <<: *Build-step
      - step:
          <<: *Deploy-step
          deployment: test
Related