DEV Community

Cover image for Get success rate and average time from your Github Actions workflow runs and jobs
r4mimu
r4mimu

Posted on

Get success rate and average time from your Github Actions workflow runs and jobs

demo

GitHub Actions is a powerful tool for automating your software development workflows. It can be used to build, test, and deploy your code. Many teams use GitHub Actions for testing and building. As development continues and the product grows, tests can become unstable, and build times can lengthen, potentially harming developer productivity. In GitHub Actions, it's not easy to check how often workflows fail or how long they take to execute. This makes it difficult to identify issues with workflows early on. By using the GitHub API, it's possible to retrieve the success rate and execution time of workflows. However, to use the API, you need to write code to send requests. While you can use the GitHub API with shell scripts, calculating statistical information requires complex processing.

Therefore, this article introduces an extension for GitHub CLI that allows you to easily obtain the success rate and execution time of GitHub Actions workflows. With GitHub CLI installed, you can easily get the success rate and execution time of your workflows.

If you want to know details about the extension, you can find the source code and the README on the GitHub repository.

Example use cases

  • Checking success rates
    • Check the success rate of workflows to identify problematic workflows.
    • Check the success rate of workflow jobs to identify problematic jobs.
  • Checking execution times
    • Check the execution time of workflows to identify workflows that take a long time to execute.
    • Check the execution time of workflow jobs to identify jobs that take a long time to execute.
  • Collecting information on failed workflows
    • Retrieve logs and HTML links for failed workflows.

Getting started

Prerequisites

  • GitHub CLI is installed.
  • You are logged in with GitHub CLI.
  • You have Read access permission to the GitHub Actions workflows you want to investigate.

Installation

You can install the extension by running the following command:

$ gh extensions install fchimpan/gh-workflow-stats
Enter fullscreen mode Exit fullscreen mode

Usage

The extension provides a command gh workflow-stats that you can use to get the success rate and average time of your GitHub Actions workflow runs.
jobs subcommand can be used to get the success rate and average time of your GitHub Actions workflow jobs.

# Get the success rate and average time of your GitHub Actions workflow runs
$ gh workflow-stats -o $OWNER -r $REPO -f $WORKFLOW_FILE_NAME
# Get the success rate and average time of your GitHub Actions workflow jobs and jobs
$ gh workflow-stats jobs -o $OWNER -r $REPO -f $WORKFLOW_FILE_NAME
Enter fullscreen mode Exit fullscreen mode

These commands will output the following result:

πŸƒ Total runs: 100
  βœ” Success: 79 (79.0%)
  βœ– Failure: 14 (14.0%)
  πŸ€”Others : 7 (7.0%)

⏰ Workflow run execution time stats
  Min: 365.0s
  Max: 1080.0s
  Avg: 505.5s
  Med: 464.0s
  Std: 120.8s

πŸ“ˆ Top 3 jobs with the highest failure counts (failure runs / total runs)
  build (macos-latest)
    └──Run tests: 6/93

  build (ubuntu-latest)
    └──Run tests: 6/93

  build (windows-latest)
    └──Run tests: 6/93


πŸ“Š Top 3 jobs with the longest execution average duration
  build (windows-latest): 480.86s
  build (macos-latest): 301.65s
  build (ubuntu-latest): 185.24s
Enter fullscreen mode Exit fullscreen mode

--json option can be used to get the result in JSON format.

$ gh workflow-stats -o $OWNER -r $REPO -f $WORKFLOW_FILE_NAME --json

# Outputs
{
  "workflow_runs_stats_summary": {
    "total_runs_count": 100,
    "name": "Tests",
    "rate": {
      "success_rate": 0.79,
      "failure_rate": 0.14,
      "others_rate": 0.06999999999999995
    },
    "execution_duration_stats": {
      "min": 365,
      "max": 1080,
      "avg": 505.54430379746833,
      "med": 464,
      "std": 120.8452088122228
    },
    "conclusions": {
      "failure": {
        "runs_count": 14,
        "workflow_runs": [
          {
            "id": 8178615249,
            "status": "completed",
            "conclusion": "failure",
            "actor": "xxx",
            "run_attempt": 1,
            "html_url": "https://github.com/cli/cli/actions/runs/8178615249",
            "run_started_at": "2024-03-06T20:53:43Z",
            "duration": 0
          },
            ...
        ]
...
 "workflow_jobs_stats_summary": [
    {
      "name": "build (ubuntu-latest)",
      "total_runs_count": 93,
      "rate": {
        "success_rate": 0.8494623655913979,
        "failure_rate": 0.15053763440860216,
        "others_rate": 0
      },
      "conclusions": {
        "failure": 14,
        "success": 79
      },
      "execution_duration_stats": {
        "min": 166,
        "max": 225,
        "avg": 185.2405063291139,
        "med": 178,
        "std": 15.9367541045597
      },
      "steps_summary": [
        {
          "name": "Set up job",
          "number": 1,
          "runs_count": 93,
          "conclusion": {
            "success": 93
          },
          "rate": {
            "success_rate": 1,
            "failure_rate": 0,
            "others_rate": 0
          },
          ...

Enter fullscreen mode Exit fullscreen mode

Full Output example can be found here.

This extension can use query parameters to filter the runs and jobs to be analyzed. For example, you can use --actor option to filter the runs and jobs by the actor who triggered the workflow. More query parameters can be found in the GitHub API documentation.

Conclusion

In this article, I introduced a GitHub cli extension that can be used to get the success rate and average time of your GitHub Actions workflow runs and jobs.

My team has been using this extension to monitor the health of our workflows and jobs. We have found it useful to identify the workflows and jobs that are failing frequently and taking a long time to execute. We have been able to take actions to fix the issues and improve the health of our workflows and jobs.

If your team has not test reports or monitoring tools for your tests and builds, this extension can be useful for your first step to monitor the health of your workflows and jobs.

This extension can be useful for teams that want to monitor the health of their workflows and jobs.
You can visit our GitHub repository to find the source code and the README. If you have any questions or feedback, feel free to open an issue on the repository and contributions are welcome!

Top comments (0)