乐闻世界logo
搜索文章和话题

How to pass environment variable received from GitHub actions

1个答案

1

GitHub Actions is GitHub's continuous integration and continuous deployment (CI/CD) tool, helping developers automate testing, deployment, and other processes in software development. Environment variables are a critical component in this automation process, used to manage sensitive data (such as keys, API credentials) or control script execution conditions.

In GitHub Actions, environment variables can be received through multiple methods:

1. Define directly in the workflow file

Environment variables can be defined in the workflow .yml file using the env keyword. These variables can be used across the entire workflow, individual jobs, or specific steps.

yaml
name: Example workflow on: [push] jobs: example_job: runs-on: ubuntu-latest env: LOCAL_ENV_VAR: value steps: - name: Checkout code uses: actions/checkout@v2 - name: Use environment variable run: echo $LOCAL_ENV_VAR

In this example, LOCAL_ENV_VAR is defined in example_job and used in subsequent steps.

2. Use GitHub Secrets

To securely handle sensitive information, GitHub Secrets can be used to store environment variables in the repository settings, then referenced in the workflow.

First, add a secret in GitHub repository Settings -> Secrets. Then reference it in the .yml file using the secrets context:

yaml
name: Example workflow on: [push] jobs: example_job: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Use GitHub secret run: echo ${{ secrets.MY_SECRET }}

In this example, MY_SECRET is defined in the repository's Secrets settings, avoiding hardcoding sensitive data in the code.

3. Load environment variables from a file

For multiple environment variables, they can be stored in a file and loaded during workflow execution.

yaml
name: Example workflow on: [push] jobs: example_job: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Load environment variables run: export $(cat .env | xargs)

Here, the .env file contains the necessary environment variable definitions.

By utilizing these methods, GitHub Actions can effectively receive and manage environment variables, facilitating automated build, test, and deployment processes while ensuring the security of sensitive information. GitHub Actions supports receiving environment variables through various methods for workflow use. These environment variables can be set at different levels, such as workflow (workflow), job (job), or step (step) level. Below are common methods to receive and use environment variables:

1. Define directly in the workflow file

Environment variables can be defined in the workflow .yml file using the env keyword. For example:

yaml
name: Example Workflow on: [push] jobs: example_job: runs-on: ubuntu-latest env: MY_ENV_VAR: 'My Value' steps: - name: Checkout code uses: actions/checkout@v2 - name: Use environment variable run: echo "The value of MY_ENV_VAR is $MY_ENV_VAR"

In this example, MY_ENV_VAR is defined at the job level and used in a step.

2. Use GitHub Secrets

For sensitive information such as API keys, GitHub Secrets are recommended. Secrets can be set at the repository or organization level and referenced in workflows.

yaml
name: Example Workflow with Secrets on: [push] jobs: example_job: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Use secret run: echo "The secret is ${{ secrets.MY_SECRET }}"

In this example, MY_SECRET is defined in the repository's Secrets settings.

3. Pass environment variables dynamically

Environment variables can also be set dynamically in workflows using runtime values.

yaml
name: Dynamic Environment Variables on: [push] jobs: example_job: runs-on: ubuntu-latest steps: - name: Set dynamic environment variable run: echo "DYNAMIC_VAR=$(date)" >> $GITHUB_ENV - name: Use dynamic environment variable run: echo "The dynamic var is $DYNAMIC_VAR"

In this example, a dynamic environment variable is generated using the date command and used in subsequent steps.

By utilizing these methods, GitHub Actions provides flexible ways to handle environment variables, from simple value passing to handling sensitive information to dynamically generated data. This enables more secure and efficient automation and CI/CD processes.

2024年6月29日 12:07 回复

你的答案