├── .gitignore ├── Procfile ├── test.html ├── Dockerfile ├── package.json ├── shields.js ├── app.json ├── License.md ├── index.js └── Readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | npm-debug.log -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: npm start -------------------------------------------------------------------------------- /test.html: -------------------------------------------------------------------------------- 1 |

2 |

3 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mhart/alpine-node:4 2 | MAINTAINER Tim Lucas 3 | 4 | ADD . /usr/src/app/ 5 | 6 | WORKDIR /usr/src/app 7 | 8 | RUN npm install \ 9 | && npm prune --production 10 | 11 | EXPOSE 8080 12 | 13 | CMD ["npm", "start"] 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "build-shields", 3 | "version": "1.0.0", 4 | "description": "Dynamic shield.io badges for your Buildkite builds", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node index.js" 8 | }, 9 | "author": "Tim Lucas ", 10 | "license": "MIT" 11 | } 12 | -------------------------------------------------------------------------------- /shields.js: -------------------------------------------------------------------------------- 1 | var querystring = require('querystring'); 2 | 3 | function paramStr(str) { 4 | // Escapes the special shields.io characters 5 | return encodeURIComponent(str.replace(/\-/g,'--').replace(/_/g,'__')); 6 | } 7 | 8 | function url(label, value, color, urlOptions) { 9 | var base = 'https://img.shields.io/badge/' + paramStr(label) + '-' + paramStr(value) + '-' + color + '.svg'; 10 | 11 | if (!urlOptions) { 12 | return base; 13 | } else { 14 | return base + '?' + querystring.stringify(urlOptions); 15 | } 16 | } 17 | 18 | module.exports = { 19 | url: url 20 | }; 21 | -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Dynamic Build Badges", 3 | "description": "Dynamic shields.io badges for your Buildkite builds", 4 | "keywords": [ 5 | "ci", 6 | "badges", 7 | "shields", 8 | "nodejs", 9 | "buildkite" 10 | ], 11 | "website": "https://buildkite.com/", 12 | "repository": "https://github.com/buildkite/dynamic-build-badges", 13 | "logo": "https://dl.dropboxusercontent.com/u/376613/buildkite/buildkite-lifx-build-light-logo.svg", 14 | "env": { 15 | "BUILDKITE_API_KEY": { 16 | "description": "A Buildkite API key with read_builds access" 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /License.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Buildkite Pty Ltd 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var buildkiteApiKey = process.env.BUILDKITE_API_KEY; 2 | if (!buildkiteApiKey) { 3 | throw new Error("No BUILDKITE_API_KEY env present"); 4 | } 5 | 6 | var http = require('http'); 7 | var url = require('url'); 8 | var shields = require('./shields'); 9 | var buildkiteApi = require('./buildkite-api')(buildkiteApiKey); 10 | 11 | var server = http.createServer(function(request, response){ 12 | if (request.url == '/') { 13 | response.end("Use the following markdown in your Readme:\n" + 14 | '![](//' + request.headers.host + '/my-org/my-pipeline/my-meta-data)'); 15 | } else { 16 | badgeUrl(request, function(url) { 17 | response.writeHead(302, { 'Location': url }); 18 | response.end(''); 19 | }) 20 | } 21 | }); 22 | 23 | server.listen(process.env.PORT || 8080, function() { 24 | console.log("Server listening on: http://localhost:%s", this.address().port); 25 | }); 26 | 27 | function badgeUrl(request, callback) { 28 | var parsedUrl = url.parse(request.url, true) 29 | var pathMatch = parsedUrl.pathname.match(/^\/(.+)\/(.+)\/(.+)$/); 30 | 31 | if (pathMatch) { 32 | fetchBadgeUrl(pathMatch[1], pathMatch[2], pathMatch[3], parsedUrl.query, callback); 33 | } else { 34 | callback(shields.url('Badge syntax error', 'Path invalid', 'red')); 35 | } 36 | } 37 | 38 | function fetchBadgeUrl(orgSlug, pipelineSlug, metadataField, options, callback) { 39 | var label = options.label || metadataField; 40 | var branch = options.branch || 'master'; 41 | var state = options.state || 'passed'; 42 | 43 | var badgeUrlOptions = {}; 44 | if (options.style) badgeUrlOptions.style = options.style; 45 | if (options.logo) badgeUrlOptions.logo = options.logo; 46 | if (options.logoWidth) badgeUrlOptions.logoWidth = options.logoWidth; 47 | 48 | buildkiteApi.fetchBuild(orgSlug, pipelineSlug, branch, state, function(err, build) { 49 | if (err) { 50 | callback(shields.url(label, String(err), 'red', badgeUrlOptions)); 51 | } else { 52 | var metadataValue = build.meta_data[metadataField] || "missing"; 53 | callback(shields.url(label, metadataValue, colorForBuild(build, options), badgeUrlOptions)); 54 | } 55 | }); 56 | } 57 | 58 | function colorForBuild(build, options) { 59 | // TODO: Add smarts to this function to calculate red -> green if a specific 60 | // option is passed in 61 | if (options.color) { 62 | return options.color; 63 | } else { 64 | return 'green'; 65 | } 66 | } -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Deprecated 2 | 3 | > Buildkite no longer uses dynamic build badges. This repository is deprecated and is no longer maintained. 4 | 5 | # Dynamic Build Badges ![](https://img.shields.io/badge/Woot-100%-green.svg?style=flat-square) 6 | 7 | A dynamic build badge server that uses the [Buildkite Builds API](https://buildkite.com/docs/api/builds) and [shields.io](http://shields.io/) to turn any of your [build meta-data values](https://buildkite.com/docs/guides/build-meta-data) into a readme badge. Embed your code coverage, build timings, latest commit running on production, or anything you like! 8 | 9 | For example, say you had the following code snippet somewhere in your `my-org/my-pipeline` build pipeline: 10 | 11 | ```bash 12 | buildkite-agent meta-data set coverage '95%' 13 | ``` 14 | 15 | To show the value from the last passing master build, you’d: 16 | 17 | ```html 18 | Coverage 19 | ``` 20 | 21 | which would look like: 22 | 23 | Screenshot showing the badge 24 | 25 | Available parameters: 26 | 27 | * `branch` - the branch to find the latest build from. Default is `master`. 28 | * `state` - the required state of the latest build on the branch. Default is `passed`. 29 | * `label` - the label for the badge. Default is meta-data key name. 30 | * `color` - any valid SVG named color or hex value (e.g. `red` or `ff0033`). Default is `green`. 31 | * `style` - [shields.io style](http://shields.io/#styles) (e.g. `flat-square`) 32 | * `logo` - [shields.io logo](http://shields.io/#styles) (e.g. `data:image/png;base64,…`) 33 | * `logoWidth` - [shields.io logoWidth](http://shields.io/#styles) (e.g. `40`) 34 | 35 | ## Usage 36 | 37 | 1. **Create an API token**
Create a [Buildkite API Token](https://buildkite.com/user/api-access-tokens) with `read_builds` access so it can fetch the latest build and grab its meta-data. Copy the API key and paste it into the next step… 38 | 39 | 1. [![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy) 40 | 41 | 4. **Embeddinate your badges** :tada: 42 | 43 | ## Docker 44 | 45 | You can also run this via Docker using [`buildkite/dynamic-build-badges`](https://hub.docker.com/r/buildkite/dynamic-build-badges/): 46 | 47 | ``` 48 | docker run -p 8080:8080 -e BUILDKITE_API_KEY=xyz buildkite/dynamic-build-badges 49 | ``` 50 | 51 | ## Roadmap 52 | 53 | * Ability to specify a `range` parameter, and have the color change from red → green depending on the value 54 | * Speed up badge display by removing 302 and serving it directly with [shields](https://github.com/badges/shields) 55 | 56 | ## Development 57 | 58 | * `git clone` 59 | * `env BUILDKITE_API_KEY=xxx npm run web` 60 | 61 | ## Contributing 62 | 63 | Pull requests welcome! 64 | 65 | ## License 66 | 67 | See the [License](License.md) file for license rights and limitations (MIT). 68 | --------------------------------------------------------------------------------