Job-Specific Environment Variables
Defining environment variables at the job level ensures that they are accessible only within that particular job. For instance, if you have a job named “job1”, you can set a variable by adding anenv section to that job. In the following example, the environment variable for the database hostname is set to “localhost”:
DATABASE_HOSTNAME is limited to job1. When you need to add other environment-specific variables, follow the same approach to ensure each variable is available only where necessary.
Conditional Environment Variables
You can also set multiple environment variables dynamically based on certain conditions. Consider the example below in which a job runs only on a specific day:Global Environment Variables
For scenarios where an environment variable needs to be accessible by all jobs in your workflow, define it at the root level. The example below demonstrates setting several global environment variables:It is crucial to avoid hardcoding sensitive information such as database credentials or secret keys in your repository. For improved security, store such data as encrypted secrets in your CI/CD system so that they remain concealed yet accessible during runtime.
Handling Workflow Errors
When testing your workflow, you might encounter issues, especially if your runner lacks the necessary infrastructure. For example, if an environment variable points to a local database (localhost) that isn’t actually available on the runner, you may see SQLAlchemy connection failures: