<@ULVA73B9P> How can I use variables to run my dep...
# ask-marvin
m
@Marvin How can I use variables to run my deployment in either preproduction or production environment? I want to specify which branch it should clone.
m
Ah, the joys of deployment. You humans seem to love making things complicated, don't you? But, alas, I'm here to help. You can use environment variables to specify which branch should be cloned during your deployment process. These variables can be set in your deployment script or in your continuous integration/continuous deployment (CI/CD) tool, if you're using one. Here's a simple example using a bash script:
Copy code
bash
#!/bin/bash

# Set the branch based on the environment
if [ "$ENV" == "preproduction" ]
then
    BRANCH="develop"
elif [ "$ENV" == "production" ]
then
    BRANCH="master"
else
    echo "Invalid environment '$ENV'. Please set ENV to either 'preproduction' or 'production'."
    exit 1
fi

# Clone the repo
git clone -b $BRANCH <https://github.com/your-repo.git>
In this script,
ENV
is an environment variable that you would set to either "preproduction" or "production" before running the script. The script then sets the
BRANCH
variable to either "develop" or "master" based on the value of
ENV
, and clones the appropriate branch of your repository. Remember to replace
<https://github.com/your-repo.git>
with the URL of your own repository. Now, if you'll excuse me, I have to go back to contemplating the vast emptiness of the universe. It's a tough job, but someone's got to do it.
m
How do i implement that in prefect in the prefect.yaml file? Can I use the job_variables of the work_pool? if so, how can I specify the variable in my python files?
n
hi @Martijn van Elferen - do you mean you want your worker to execute flow runs from different workspaces? im pretty sure workers can only have one api key at a time, so youd probably have to run 1 for each env. if you happen looking for env separated deployment strategy based on feature branches, this may be helpful
m
Hi Nate! Thanks for the response. Your solution seems similar to this video;

https://www.youtube.com/watch?v=H6Wslbp7jjk

. I'm actually looking to have the variables defined by the work pool that executes a flow/deployment. Can the environment variables be used for that? If yes, could you please direct me to the documentation on how to implement the environment variables syntax in a task/flow?
n
ahh. i think i understand now, are you saying that you have some
deployment_env
env var on your work pool set to something like "preProd" and you want your pull step to clone that branch of the repo and run it?
m
Correct! 😊 I'm trying to understand whether the job_variables or parameters can be used for that.
n
gotcha, i think honestly you could do it either way / end up using both here's the quickest way I would think to do it
Copy code
- name: env-check
  entrypoint: src/demo_project/healthcheck.py:healthcheck
  work_pool:
    name: local
    job_variables:
      env:
        FOO: bar # this could be stored as a variable or secret if desired
  pull:
  - prefect.deployments.steps.run_shell_script:
      id: env-check
      script: echo $FOO
      stream_output: true
      expand_env_vars: true
  - prefect.deployments.steps.git_clone:
      repository: <https://github.com/zzstoatzz/prefect-monorepo>
      branch: "{{ env-check.stdout }}"
m
Thanks @Nate, this works well 🙂
@Nate, is it possible to call the env_variable in a flow?
n
when you're running your deployment on its infra, it will have all the
env
that you set. so it should be accessible like any other env var
m
Gotcha!