lfarci / github-actions

Preparation resources for the GitHub Actions certification
0 stars 0 forks source link

Demonstrate the correct syntax for passing custom environment variables in a workflow step #40

Closed lfarci closed 3 months ago

lfarci commented 3 months ago

In GitHub Actions, you can define custom environment variables for a specific step using the env keyword. Here's an example:

name: Demo workflow
on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Check out code
        uses: actions/checkout@v2

      - name: Run a step with custom environment variables
        run: |
          echo "Custom Variable 1: $CUSTOM_VAR_1"
          echo "Custom Variable 2: $CUSTOM_VAR_2"
        env:
          CUSTOM_VAR_1: This is the first custom variable
          CUSTOM_VAR_2: This is the second custom variable

In this workflow, the "Run a step with custom environment variables" step has two custom environment variables: CUSTOM_VAR_1 and CUSTOM_VAR_2. These variables are defined using the env keyword, and their values are set to "This is the first custom variable" and "This is the second custom variable", respectively.

The run command is a multi-line command. Each line is a separate command that prints the name and value of a custom environment variable. The $ symbol is used to reference the value of an environment variable.

This workflow will run whenever a push or pull request event occurs. The output of the "Run a step with custom environment variables" step can be viewed in the logs for the workflow run.