Some reusable shell scripts for creating and publishing badges based on artifacts from your builds. It helps avoid duplicating the code for report-parsing, badge-generating, and uploading across repositories.
The conventions and badge generator of shields.io are used.
Create your own task script which generates and publishes badges from your job...
$ cat > ci/tasks/publish-badges.sh
#!/bin/sh -eu
init-refs @repo/.git/HEAD $repo_branch
log=artifacts/logs/coverage/clover.xml \
( clover-coverage && clover-lines )
log=artifacts/logs/junit/results.xml \
junit-results
publish-s3
And add your task plan, including the inputs and publish parameters you'll need...
$ cat > ci/tasks/publish-badges.yml
---
platform: "linux"
image: "docker:///dpb587/ci-badges#master"
inputs:
- name: "repo"
- name: "artifacts"
run:
path: "repo/ci/tasks/publish-badges.sh"
params:
repo_branch: ~
publish_region: ~
publish_bucket: ~
publish_prefix: ~
publish_access_key: ~
publish_secret_key: ~
Then include it in your pipeline...
task: publish-badges
file: repo/ci/tasks/publish-badges.yml
params:
repo_branch: {{repo_branch}}
publish_bucket: my-bucket-name-us-east-1
publish_prefix: owner-name/repo-name/
publish_access_key: {{badge_publish_access_key}}
publish_secret_key: {{badge_publish_secret_key}}
And badges can be referenced from the bucket...
[![build](https://s3.amazonaws.com/my-bucket-name-us-east-1/owner-name/repo-name/master/build.svg)](https://ci.example.com/pipelines/owner-name:repo-name:master/)
![coverage](https://s3.amazonaws.com/my-bucket-name-us-east-1/owner-name/repo-name/master/coverage.svg)
![loc](https://s3.amazonaws.com/my-bucket-name-us-east-1/owner-name/repo-name/master/loc.svg)
Generators will create a badge entry according to the variables you specify.
badges_dir
- path to generate badges in (default:/tmp/badges
)
Generate a custom badge.
name
- a badge namestatus
- the status of the badge (shown on the right with colored background)subject
- the subject/label of the badge (shown on the left)color
- the color of the status (default:lightgrey
)
Generate a green/yellow/red percentage based on a Clover Code Coverage report.
log
- path to a Clover Coverage XML filebadge_name
- badge name to use (default:coverage
)badge_subject
- badge subject to use (default:$badge_name
)percent_green
- threshold percentage for green (default:80
)percent_yellow
- threshold percentage for yellow (default:50
)
Generate an informational badge showing the number of lines of code.
log
- path to a Clover Coverage XML filebadge_color
- badge color to use (default:yellowgreen
)badge_name
- badge name to use (default:loc
)badge_subject
- badge subject to use (default:$badge_name
)
Generate a passing/failing badge based on a JUnit report.
log
- path to a JUnit XML filebadge_name
- badge name to use (default:build
)badge_subject
- badge subject to use (default:$badge_name
)
Publishers will publish a badge entry by uploading the shields.io-generated badge to your destination. Publisher-specific variable names should be publish_
-prefixed.
badges_dir
- path to publish badges from (default:/tmp/badges
)
Uploads badges to a bucket with a key in the format of {prefix}?{ref}/{badge_name}.svg
.
publish_bucket
- bucket namepublish_access_key
- access keypublish_prefix
- prefix for ref and badge filespublish_region
- region the bucket is located (default:us-east-1
)publish_secret_key
- secret key
You might want to create a restricted IAM User for uploading to bucket...
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::my-bucket-name-us-east-1/*"
]
}
]
}
And you might want to make the bucket publicly readable so badges can show up in GitHub and other frontends...
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-bucket-name-us-east-1/*"
}
]
}