GitHub Actions provides powerful capabilities for automating workflows, including the ability to control job execution based on specific conditions. By using if conditions in workflow files, you can create dynamic workflows that execute certain steps or jobs only when certain conditions are met. In this tutorial, we’ll explore how to use if conditions in GitHub Actions workflows, along with basic examples and variations to demonstrate their usage.
Understanding If Conditions:
If conditions in GitHub Actions allow you to specify conditional logic based on predefined variables, outputs from previous steps, or custom expressions. You can use if conditions to control when a job or step should execute based on the result of evaluating a condition.
Basic Syntax:
The basic syntax for using if conditions in GitHub Actions is as follows:
jobs: job_name: if: condition steps: - name: Step 1 run: echo "Step 1 executed"
Before checking some examples, we recommend looking at this article if you are new to GitHub Actions: Getting started with GitHub Actions
GitHub Actions Conditional Execution examples
Example 1: Conditional Execution Based on Branch: In this example, we’ll execute a job only when changes are pushed to the main
branch.
jobs: build: if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest steps: - name: Checkout Repository uses: actions/checkout@v2 - name: Build Project run: npm run build
Example 2: Conditional Execution Based on Event Type: Here, we’ll execute a job only when the trigger event is a pull request.
jobs: test: if: github.event_name == 'pull_request' runs-on: ubuntu-latest steps: - name: Checkout Repository uses: actions/checkout@v2 - name: Run Tests run: npm test
Example 3: Using Custom Outputs: We’ll use outputs from previous steps to conditionally execute a job.
jobs: deploy: if: steps.build.outputs.success == 'true' runs-on: ubuntu-latest steps: - name: Checkout Repository uses: actions/checkout@v2 - name: Build Project id: build run: | # Build project and set output echo "::set-output name=success::true" - name: Deploy Project run: | # Deploy project if build was successful echo "Deploying project"
Advanced Variations:
Example 4: Combining Conditions: You can combine multiple conditions using logical operators (&&
, ||
, !
) to create more complex expressions.
jobs: deploy: if: github.event_name == 'push' && github.ref == 'refs/heads/main' runs-on: ubuntu-latest steps: - name: Checkout Repository uses: actions/checkout@v2 - name: Deploy Project run: | # Deploy project only on push to main branch echo "Deploying project to main branch"
Example 5: Using Environment Variables: Environment variables can be used in if conditions to evaluate dynamic values.
jobs: test: if: env.TEST_ENV == 'true' runs-on: ubuntu-latest steps: - name: Run Tests run: npm test
Conclusion: Using if conditions in GitHub Actions workflows allows you to create flexible and dynamic automation pipelines. By controlling job execution based on conditions such as branch names, event types, outputs from previous steps, or environment variables, you can tailor workflows to meet specific requirements and optimize resource utilization. Experiment with different conditions and expressions to customize your workflows according to your needs.