Lädt...


🔧 Controlling Job Execution with Conditions in GitHub Actions


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to


cicube.io

Introduction

Conditional execution of jobs is one of those powerful features you find in GitHub Actions, underutilized at times. It enables you to block the execution of jobs unless certain conditions are met-something which is super useful if you want to control a workflow flow based on outputs or external factors. Whether for multiple environments, complex build pipelines, or job dependencies, conditions enable you to further define your automation.

Using Conditions to Control Job Execution

GitHub Actions similar to jobs.<job_id>.if syntax, you define conditions based on a wide range of inputs where a job would run. Amongst these are job outputs, GitHub contexts-like repository names or branches, environment variables amongst others. Adding a condition makes your workflow more efficient while skipping jobs by marking them as "success" if skipped.

Here's a simple example - we only want to run a deployment job if the repository is a production repository:

jobs:
  production-deploy:
    if: github.repository == 'my-org/prod-repo'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: echo "Deploying to production"

In this case, the production-deploy job will only execute if the workflow is triggered in the repository my-org/prod-repo, and be skipped for any other cases.

Combining Job Dependencies and Conditions

This is often the case when you want to make a job execute depending on the result of the execution of another job, deployment job after a build job. You can easily define job dependencies using the keyword needs in these cases, or you could combine the usage of needs with conditions to fine-tune this behavior.

Consider an example where we have the three jobs: build, test, and deploy. Here, we want deploy to run only if build succeeds and the test job either succeeds or is skipped.

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Building project"

  test:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - run: echo "Running tests"
      - id: set_test_output
        run: echo "::set-output name=should_deploy::yes"
    outputs:
      should_deploy: ${{ steps.set_test_output.outputs.should_deploy }}

  deploy:
    runs-on: ubuntu-latest
    needs: [build, test]
    if: needs.test.outputs.should_deploy == 'yes' && (needs.test.result == 'success' || needs.test.result == 'skipped')
    steps:
      - run: echo "Deploying project"

Overview: How It Works:

  1. In it, the job build is executed and upon success of this job, the test job is triggered.
  2. The test job outputs a should_deploy, which will be used in the deploy job to decide if it should be executed or not.
  3. Here, the deploy job is dependent on success of the build job and either success or skip of the test job. The deployment proceeds when the output of the test job is set to 'yes'.

This will be an elastic way of dealing with job dependencies, which come with complementary conditions.

Handling of Skipped Jobs and Conditional Logic

One common challenge within workflows, is how to deal with skipped jobs. By default a job may be "skipped", yet its status can still be reported out as "success", yet you may wish for other jobs to behave differently based on this status. That's where using conditions like success(), failure(), cancelled() or always() can help.

Suppose you then have a test job which, under some circumstances is going to skip. Since the following job, notify should-only execute if prior test job succeed or is skipped.

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Running tests"
      - id: set_skip_condition
        run: echo "::set-output name=should_skip::no"
    outputs:
      should_skip: ${{ steps.set_skip_condition.outputs.should_skip }}

  notify:
    runs-on: ubuntu-latest
    needs: test
    if: needs.test.result == 'success' || needs.test.result == 'skipped'
    steps:
      - run: echo "Sending notification"

Breakdown:

  1. The test job sets an output called should_skip.
  2. The notify job will only run if the test job succeeds or is skipped.
  3. This way, it would still generally be expected that the workflow proceeds, even if a job was skipped.

Monitoring GitHub Actions Workflows

CICube is a GitHub Actions monitoring tool that provides you with detailed insights into your workflows to further optimize your CI/CD pipeline. With CICube, you will be able to track your workflow runs, understand where the bottlenecks are, and tease out the best from your build times. Go to cicube.io now and create a free account to better optimize your GitHub Actions workflows!

CICube GitHub Actions Workflow Duration Monitoring

Lessons Learned: Always Be Careful with Conditional Logic

When I was investigating the feature of conditional execution of jobs, that is when all those unexpected behaviors kicked in. For example, I used some combined conditions with if. Sometimes, the workflow didn't behave the way it was expected.

Sometimes you might want to make sure a job runs under several conditions. You can use always() as a way of forcing an evaluation alongside other conditions. Here's such an example where we wish the finalize job to run even if previous jobs were skipped, provided another condition is met:

jobs:
  finalize:
    runs-on: ubuntu-latest
    needs: [build, test]
    if: always() && (needs.build.result == 'success' || needs.test.result == 'skipped')
    steps:
      - run: echo "Finalizing workflow"

This will ensure that finalize is definitely executed regardless of the result of all the other jobs, but only of course, if at least one of the jobs succeeded or was skipped.

Conclusion

Conditional execution of jobs in GitHub Actions allows you to get much more efficient and maintainable continuous integration/continuous deployment pipelines. Because you're going to have some context, like outputs or external factors determining whether it's worth running the job, you will save not just time but also resources. You would need to consider how you will handle skipped jobs. Know about status checks like success(), failure() or always().

You'll be able to make your workflows more intelligent and responsive with conditional logic so that the need for redundant jobs or manual intervention will be reduced when different scenarios pop up.

...

🔧 Controlling Job Execution with Conditions in GitHub Actions


📈 59.74 Punkte
🔧 Programmierung

🔧 Controlling Job Execution with Conditions in GitHub Actions


📈 59.74 Punkte
🔧 Programmierung

🎥 Conversational Actions overview - Actions Builder & Actions SDK


📈 30.31 Punkte
🎥 Videos

🎥 Actions Project - Actions Builder & Actions SDK


📈 30.31 Punkte
🎥 Videos

🐧 Learn how to write if conditions in bash and how conditions get tested using a command 'test'


📈 30.13 Punkte
🐧 Linux Tipps

🔧 Deploying a Vite app on GitHub Pages using GitHub Actions with GitHub Secrets


📈 28.68 Punkte
🔧 Programmierung

🔧 [Github actions][actions]


📈 26.4 Punkte
🔧 Programmierung

🔧 Cargo Actions: An efficient tool for managing and creating GitHub Actions workflow templates


📈 26.4 Punkte
🔧 Programmierung

🔧 actions-hottest🚀: GitHub Actions for Commenting on Golang Unit Test Results in Pull Requests


📈 26.4 Punkte
🔧 Programmierung

🐧 Gitea is working on a built-in CI/CD tool called Gitea Actions (compatible with GitHub Actions syntax)


📈 26.4 Punkte
🐧 Linux Tipps

🔧 Generating temporary access actions in policies using Terraform with DATE and TIME conditions in AWS.


📈 25.17 Punkte
🔧 Programmierung

🔧 Building Efficient Node.js Workflows in GitHub Actions: Leveraging Caching and Modular Job Structures


📈 23.62 Punkte
🔧 Programmierung

🔧 GitHub Actions: run a job only if a package has changed


📈 23.62 Punkte
🔧 Programmierung

🎥 Using GitHub Actions in your day job


📈 23.62 Punkte
🎥 Video | Youtube

🎥 Accelerating cloud-to-edge development flows with GitHub Copilot and GitHub Actions


📈 22.49 Punkte
🎥 Video | Youtube

🔧 How I Bulk Closed 1000+ GitHub Issues with GitHub Actions 🚀


📈 22.49 Punkte
🔧 Programmierung

🔧 Automate Your C# Library Deployment: Publishing to NuGet and GitHub Packages with GitHub Actions


📈 22.49 Punkte
🔧 Programmierung

🔧 Sync GitHub repo and Hugging Face Space Repo with GitHub Actions


📈 22.49 Punkte
🔧 Programmierung

🔧 Automate Your C# Library Deployment: Publishing to NuGet and GitHub Packages with GitHub Actions


📈 22.49 Punkte
🔧 Programmierung

📰 Build an end-to-end MLOps pipeline using Amazon SageMaker Pipelines, GitHub, and GitHub Actions


📈 22.49 Punkte
🔧 AI Nachrichten

🔧 GitHub Actions CI/CD for React app hosted in GitHub Pages


📈 22.49 Punkte
🔧 Programmierung

🔧 Secure Your GitHub Repos! Prevent Merging AWS, Azure, and GCP Sensitive Credentials with GitHub Actions


📈 22.49 Punkte
🔧 Programmierung

🔧 Use GitHub Actions to Make Your GitHub Profile Dynamic


📈 22.49 Punkte
🔧 Programmierung

🔧 Automating Flutter App Builds with GitHub Actions: Firebase App Distribution and GitHub Releases


📈 22.49 Punkte
🔧 Programmierung

🔧 How to Create a GitHub Profile Readme with GitHub Actions, Profile Trophy, and Custom Icons Badges


📈 22.49 Punkte
🔧 Programmierung

🔧 Optimizing GitHub Actions with GitHub GraphQL API


📈 22.49 Punkte
🔧 Programmierung

🔧 Automate Docker Image Builds and Push to GitHub Registry Using GitHub Actions 🐙


📈 22.49 Punkte
🔧 Programmierung

🔧 Upload Docker Images to GitHub: A Simple Guide with GitHub Actions


📈 22.49 Punkte
🔧 Programmierung

🔧 Deploy Nextjs app to github-pages with Github Actions


📈 22.49 Punkte
🔧 Programmierung

🔧 [GitHub Actions] Automate Build and Deployment of Your Python Package to PyPI and GitHub Releases 🐍📦


📈 22.49 Punkte
🔧 Programmierung

🔧 Bonus Tip: How to Use GitHub Actions to Test a GitHub Action Whose Output Must be Visually Inspected


📈 22.49 Punkte
🔧 Programmierung

🔧 Deploy React App to GitHub Pages using GitHub Actions


📈 22.49 Punkte
🔧 Programmierung

🔧 How to Test a GitHub Action with GitHub Actions


📈 22.49 Punkte
🔧 Programmierung

🔧 Publishing Packages to GitHub with GitHub Actions


📈 22.49 Punkte
🔧 Programmierung

🔧 Pushing container images to GitHub Container Registry with GitHub Actions


📈 22.49 Punkte
🔧 Programmierung

matomo