Get success rate and average time from your Github Actions workflow runs and jobs
Tags: githubactions, github, go, cli
Published: 2024-03-17 14:22:16



![demo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mhdxun5a52p5q0enk00t.png)



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](https://github.com/fchimpan/gh-workflow-stats).


## 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:

```bash
$ gh extensions install fchimpan/gh-workflow-stats
```

## 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.

```bash
# 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
```

These commands will output the following result:

```bash
🏃 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
```

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

```bash
$ 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
          },
          ...

```

Full Output example can be found [here](https://github.com/fchimpan/gh-workflow-stats/blob/main/sample/json-output.json).

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](https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-workflow).


## 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](https://github.com/fchimpan/gh-workflow-stats) 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!