Comments (12)
Great article! Hopefully it helps someone discover this project! Thank you!
from dynamic-badges-action.
Yes, this is definitely possible! Everything you can store in an environment variable can be shown in the badges. I do not use Jest, but reading it's documentation, I think it could work along the following lines. I haven't tested it - please tell me whether you get this working, then I can add an example to the README.
The idea is to extract the coverage percentage from jest's output and store it in an environment variable.
#!/bin/bash
# Get the last-but-one line of the text summary. This contains the line coverage
# percentage. It may look something like that:
# Lines : 100% ( 161/161 )
SUMMARY="$(jest --coverageReporters='text-summary' | tail -2 | head -1)"
# Split the line by white space.
TOKENS=($SUMMARY)
# The third token is our 100% above.
echo ${TOKENS[2]}
If this works, you could set an environment variable in your workflow file like this:
echo "::set-env name=COVERAGE::$(echo ${TOKENS[2]})"
And use this later in the action's configuration:
- name: Create the Badge
uses: schneegans/[email protected]
with:
auth: ${{ secrets.GIST_SECRET }}
gistID: <gist-ID>
filename: coverage.json
label: Coverage
message: ${{ env.COVERAGE }}
color: green
from dynamic-badges-action.
Thanks!
I was hoping I could keep all the logic in Node, so it would be cross-platform and easier to manage. But it looks like when node creates/modifies an environment variable, it doesn't effect the CI globally. So I couldn't get my Node script to communicate with the action.
This is a clever hack, but it feels like a lot of work to set up each badge, especially since I wanted each branch to have its own coverage badge. But now I don't even know how to just make the main
branch have its own that isn't effected by other branches.
I really hope GitHub releases a built in way of handling this, like every other CI does.
from dynamic-badges-action.
Yep, it's kind of verbose. But if you use bash, it's not too complex, I think. Maybe you can add one file for each branch to your gist with filename: ${{ github.ref }}.json
. Then you could add a table to your README with a badge for each relevant branch.
Not tested:
- run: |
SUMMARY="$(npm test -- --coverageReporters='text-summary' | tail -2 | head -1)"
TOKENS=($SUMMARY)
echo "::set-env name=COVERAGE::$(echo ${TOKENS[2]})"
- name: Create the Badge
uses: schneegans/[email protected]
with:
auth: ${{ secrets.GIST_SECRET }}
gistID: 9c5d16fe3fa8f8ef414fe8b0eff17f7f
filename: ${{ github.ref }}.json
label: Coverage
message: ${{ env.COVERAGE }}
color: green
from dynamic-badges-action.
Sadly GitHub's Actions API doesn't have a way of getting the branch name, so I used the closest thing.
That seems to work pretty well.
from dynamic-badges-action.
Though now I'm getting this error message:
The
set-env
command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands
from dynamic-badges-action.
It seems like you have to replace echo "::set-env name=COVERAGE::$(echo ${TOKENS[2]})"
with something like echo "COVERAGE=$(echo ${TOKENS[2]})" >> $GITHUB_ENV
. Does this work?
Edit: I updated the example in the README accordingly.
from dynamic-badges-action.
I couldn't get it to work:
- https://github.com/red-perfume/red-perfume/pull/35/files
- https://github.com/red-perfume/red-perfume/runs/1309030354?check_suite_focus=true
The closest thing I could find related to the error, but not sure what to do about it
Thanks for the help
from dynamic-badges-action.
Have you missed the leading echo
in the command I posted above? It should be something like (now without a line break)
echo "COVERAGE=$(echo ${TOKENS[2]})" >> $GITHUB_ENV
from dynamic-badges-action.
Ayyyyyyyyyy! It woooooooooooorks!
from dynamic-badges-action.
😄
from dynamic-badges-action.
@Schneegans Update: I wrote an article on this to help others:
from dynamic-badges-action.
Related Issues (20)
- Fetching gist through github API fetches gist history HOT 4
- When using selft-hosted runner which set proxy, it will throw erros HOT 2
- How to use it with organizations accounts ? HOT 33
- [BUG] Unauthorized HOT 8
- [FEATURE] Update gist only if changed HOT 1
- upgrade for node 16 HOT 2
- Workflow triggered by Dependabot fails HOT 4
- How to use it with secret gists? HOT 4
- Write SVG files to gist
- Thank you! HOT 1
- Cannot setup job on self hosted runner HOT 7
- The message contain: message: files] files] files] files] files] files] files] files] 21.4% HOT 4
- [Feature] Make action verbose when `ACTIONS_RUNNER_DEBUG` is `True`
- Improve the instructions in READEME file HOT 3
- Occasional "Gist not found 401" is the weakest link
- Bulk badge upload
- "Bad credentials" bug HOT 3
- Action sometimes not outputting any results HOT 5
- Shield.io display domain is blocked HOT 3
- Storing json in wiki ? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dynamic-badges-action.