Skip to content

Instantly share code, notes, and snippets.

@harrytwright
Last active August 5, 2020 15:06
Show Gist options
  • Save harrytwright/1117dcc3b3ab84943e1ce74f123e262b to your computer and use it in GitHub Desktop.
Save harrytwright/1117dcc3b3ab84943e1ce74f123e262b to your computer and use it in GitHub Desktop.
const os = require('os')
const fs = require('fs')
const path = require('path')
const { exec } = require('child_process')
const tmpdir = os.tmpdir()
const directory = path.join(tmpdir, './docker-hook')
const avatar = "https://www.docker.com/sites/default/files/d8/2019-07/Moby-logo.png"
const config = {
uri: 'https://discordapp.com/api/webhooks/740538358945546260/BGePe7yOcQTQhGXREPCFq8Lt4Mcr3WpTrlUCNP1QfVG4L1wIGvlTS9T8u73XwCr0l_SM'
}
hook(true, {
tags: ['react'],
build: 'geo-search',
uri: 'https://hub.docker.com/layers',
username: 'harrytwright',
sha: '068335bdf6da4ee2e1efaddc181876b3403771eb1de0bf4691eb4f4b41319488'
})
function hook(passed, { tags, build, uri, username, sha }) {
console.log("docker: Sending webhook to discord")
const status = passed ? "Success" : "Failure"
const colour = passed ? 3066993 : 15158332
const data = {
username: "",
avatar_url: avatar,
embeds: [{
color: colour,
author: {
name: `push: ${status} (${username}/${build})`,
url: uri,
icon_url: avatar
},
title: "docker push",
url: uri,
description: `push: ${status} (${username}/${build})\n${username} built & pushed`,
fields: tags.map((tag) => ({
name: tag,
value: `[\`${tag}\`](${uri}/${username}/${build}/${tag}/images/sha256-${sha})`,
inline: true
}))
}],
timestamp: new Date()
}
if (fs.existsSync(directory)) fs.rmdirSync(directory, { recursive: true })
fs.mkdirSync(directory)
fs.writeFileSync(path.join(directory, './curl.json'), JSON.stringify(data))
process.on('exit', () => {
fs.rmdirSync(directory, { recursive: true })
process.exit(1)
})
try {
const curl = exec(`curl --fail --progress-bar -A "Docker-Webhook" -H Content-Type:application/json -H X-Author:harrytwright -d "@${path.join(directory, './curl.json')}" ${config.uri}`)
curl.stdout?.pipe(process.stdout)
curl.stderr?.pipe(process.stderr)
} catch (error) {
console.error(error)
process.exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment