Question

How to Use App Platform Environment Variable on Run Command

I’ve tried looking through all of the available documentation on using the environment variables and can’t find anything about using the environment variables in the Run Command for your App/Component. I want to store some encrypted values in the environment variables and pass those in arguments with there run command after deploying an App. I am deploying a Worker, using a Docker image and my run command looks like this:

imapsync --user1 $USER1 --password1 $PASSWORD1 --user2 $USER2 --password2 $PASSWORD2 --subfolder2 $USER1 --gmail1 --gmail2

I want all of those variables to be environment variables that are passed in when run.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This is what I ultimately ended up with creating: https://github.com/timnolte/imapsync-worker

And my App Spec looks like this:

alerts:
- rule: DEPLOYMENT_FAILED
- rule: DOMAIN_FAILED
name: imapsync-worker
region: nyc
workers:
- dockerfile_path: Dockerfile
  envs:
  - key: USER1
    scope: RUN_TIME
    value: <omitted>
  - key: PASSWORD1
    scope: RUN_TIME
    type: SECRET
    value: <omitted>
  - key: USER2
    scope: RUN_TIME
    value: <omitted>
  - key: PASSWORD2
    scope: RUN_TIME
    type: SECRET
    value: <omitted>
  - key: OPTS
    scope: RUN_TIME
  github:
    branch: main
    deploy_on_push: true
    repo: timnolte/imapsync-worker
  instance_count: 1
  instance_size_slug: basic-xxs
  log_destinations:
  - logtail:
      token: <omitted>
    name: imapsync-worker
  name: imapsync-worker
  run_command: emailsync gmail2gmail
  source_dir: /

Hello,

I have just tested this and it seems to be working.

Are you defining the scope of your environment variables as per the documentation here:

https://docs.digitalocean.com/products/app-platform/how-to/use-environment-variables/#define-build-time-environment-variables

If so, you need to make sure that you’ve also defined the variables to have a RUN_TIME scope so that you could use them for your run commands:

    envs:
    - key: RUNTIME_ONLY_VAR
      scope: RUN_TIME
      value: i'm a little teapot

Or alternatively, if you need the environment variables for both the run and the build times, you could use RUN_AND_BUILD_TIME.

I tested this with the following simple deployment:

alerts:
- rule: DEPLOYMENT_FAILED
name: testapp
region: fra
services:
- environment_slug: php
  envs:
  - key: TEST
    scope: RUN_AND_BUILD_TIME
    type: SECRET
    value: EV[1:jKc4tNWFtX6EE12356DESA9IkSX4W1L:tnxT09rsY+q7aDrUu7P3Z7y5J9E=]
  github:
    branch: main
    repo: myrepo/demo
  http_port: 8080
  instance_count: 1
  testapp: basic-xs
  name: guild
  routes:
  - path: /
  run_command: 'echo "test: ${TEST}" > test.txt ; heroku-php-apache2 public/'
  source_dir: /

Best,

Bobby