Giter Club home page Giter Club logo

Comments (12)

Schneegans avatar Schneegans commented on September 13, 2024 1

Great article! Hopefully it helps someone discover this project! Thank you!

from dynamic-badges-action.

Schneegans avatar Schneegans commented on September 13, 2024

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.

TheJaredWilcurt avatar TheJaredWilcurt commented on September 13, 2024

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.

Schneegans avatar Schneegans commented on September 13, 2024

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.

TheJaredWilcurt avatar TheJaredWilcurt commented on September 13, 2024

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.

TheJaredWilcurt avatar TheJaredWilcurt commented on September 13, 2024

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.

Schneegans avatar Schneegans commented on September 13, 2024

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?

Reference: https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#environment-files

Edit: I updated the example in the README accordingly.

from dynamic-badges-action.

TheJaredWilcurt avatar TheJaredWilcurt commented on September 13, 2024

I couldn't get it to work:

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.

Schneegans avatar Schneegans commented on September 13, 2024

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.

TheJaredWilcurt avatar TheJaredWilcurt commented on September 13, 2024

Ayyyyyyyyyy! It woooooooooooorks!

from dynamic-badges-action.

Schneegans avatar Schneegans commented on September 13, 2024

😄

from dynamic-badges-action.

TheJaredWilcurt avatar TheJaredWilcurt commented on September 13, 2024

@Schneegans Update: I wrote an article on this to help others:

from dynamic-badges-action.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.