4

I have a GitHub action workflow that outputs a number and I want to display that in a badge.

Using https://github.com/username/reponame/actions/workflows/myaction.yml/badge.svg I get a red or green failing/success badge but I want to display the number of failures instead, which the workflow outputs into the "errors" output variable.

How can I access that variable in a badge?

Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
  • By “outputs”, do you mean logs to console? An action may have multiple jobs in it, and each execution of the action has a new id. You can link to an action, but not to the output of some job of an action from some execution, that doesn’t make sense. – Abhijit Sarkar Mar 11 '22 at 18:00
  • @AbhijitSarkar I mean output parameter such as `echo "::set-output name=action_fruit::strawberry`. – Konrad Höffner Mar 12 '22 at 12:04
  • `echo` is precisely logging to console, my comment stands. – Abhijit Sarkar Mar 13 '22 at 20:46
  • 1
    I don't think there's a straightforward way to do this. But, you may be able to use a webhook or a 3rd party service to listen to this event and then generate the badge. – ThisaruG Mar 14 '22 at 11:26
  • Yeah. No way to easily access a variable in the workflow after it has completed. Your workflow could have an action that generates the badge and stores it somewhere or updates a value in some kind of blob/file storage provider. – jessehouwing Mar 14 '22 at 11:29
  • @jessehouwing: That is strange, because I thought an output is exactly something that comes out of a process and that is accessible somehow. – Konrad Höffner Mar 14 '22 at 11:48
  • In a subsequent job in the same workflow, yes. But once the workflow has finished, no. – jessehouwing Mar 14 '22 at 11:50
  • @ThisaruG: Is that the standard way to do this? Because there are actions with linters which show a number and I wonder how they do it. – Konrad Höffner Mar 16 '22 at 16:03

3 Answers3

4

There are few options in Github actions marketplace

Bring Your Own Badge - https://github.com/marketplace/actions/bring-your-own-badge

BYOB is a GitHub Action to create badges dynamically based off of GitHub Actions' results, allowing for extremely versatile and easily-maintainable badges.

If you want to use https://shields.io/, consider Dynamic Badges - https://github.com/marketplace/actions/dynamic-badges

This action allows you to create badges for your README.md with shields.io which may change with every commit. To do this, this action does not need to push anything to your repository!

Nitin Bhojwani
  • 702
  • 1
  • 5
  • 14
  • Hey, I see you try to compare to my answer. So I will add my point of view too. Personally I like to have as least dependencies as possible, I recommend to check e.g. [Life is too short to depend on unstable software](https://blog.sidebits.tech/life-is-too-short-to-depend-on-unstable-software/). Your second solution does not push anything to the repository nevertheless it create a new gist file. So from now on the project depends on the external file, new dependency. Your solutions are still nice. It is not a 0/1 decision what to choose as sb have to weight their own pros and cons. – polkas Mar 21 '22 at 07:37
  • Dynamic Badges look great but I don't think I can use it because it would require the user of the action to create a GitHub gist first and supply the id as a parameter and that could be too much hassle for them. BYOB on the other hand will create an extra branch which I also don't want to bother the users with just for a simple badge. I will accept this answer as it seems there is no better way right now, but I will just keep my badge as success/failed for now. – Konrad Höffner Mar 21 '22 at 09:30
1

In a subsequent job (step) in the same workflow you could. I think you want to use your own badge using e.g. https://img.shields.io. I image you will update e.g. the README file every time the actions are finished, the updating step will be part of the workflow. The transfer of output could be done like here Using output from a previous job in a new one in a GitHub Action.

You will append e.g. the README with a proper svg [![](https://img.shields.io/badge/TEXT-NUMBER-COLOR?style=flat)](some url).

polkas
  • 3,797
  • 1
  • 12
  • 25
  • I think this could be a good method for someones own workflow, however I think as a reusable action on the marketplace it would be too intrusive or not worth it to the users. Unfortunately it seems like there is no perfect way to do this right now with GitHub actions. – Konrad Höffner Mar 21 '22 at 09:41
0

I created an action to generate a badge from a workflow:

Build-A-Badge - https://github.com/marketplace/actions/build-a-badge

As other users have pointed out, I didn't want any external dependencies or to create new branches on the main repo. So the workaround I used is to store the badge data in the Wiki, which is a separate repository.

peterrhodesdev
  • 241
  • 4
  • 14