Skip to content

Commit

Permalink
Use docker to deterministically build gaia binary on release
Browse files Browse the repository at this point in the history
  • Loading branch information
faboweb committed Jan 8, 2018
1 parent f1c441a commit f6384f2
Show file tree
Hide file tree
Showing 2 changed files with 258 additions and 107 deletions.
61 changes: 36 additions & 25 deletions tasks/release.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { exec } = require('child_process')
const path = require('path')
const { join } = require('path')
const fs = require('fs-extra')
const builder = require('electron-builder')

Expand Down Expand Up @@ -67,7 +67,7 @@ function build (platform = process.platform, arch = process.arch) {
config.afterPack = ({outDir, appOutDir}) => {
binaryPath
? copyBinary('gaia', binaryPath, platform)({outDir, appOutDir})
: goBuild(`github.com/cosmos/gaia/cmd/gaia`, platform, arch)({outDir, appOutDir})
: buildGaiaBinary(platform, arch)({outDir, appOutDir})
copyNetworks(appOutDir)
}

Expand All @@ -82,7 +82,7 @@ function build (platform = process.platform, arch = process.arch) {
console.log('\n\x1b[34mDONE\n\x1b[0m')
})
.catch((error) => {
console.error('\x1b[31mError from `electron-packager` when building app...\x1b[0m')
console.error('\x1b[31mError from `electron-builder` when building app...\x1b[0m')
console.error(error)
})
}
Expand All @@ -94,29 +94,40 @@ const GOARCH = {
'x64': 'amd64',
'ia32': '386'
}
function goBuild (pkg, platform, arch) {

function buildGaiaBinary (platform, arch) {
return function ({outDir, appOutDir}) {
if (platform === 'win32') platform = 'windows'
if (platform === 'mas') platform = 'darwin'
if (GOARCH[arch]) arch = GOARCH[arch]

console.log(`\x1b[34mBuilding gaia binary (${platform}/${arch})...\n\x1b[0m`)

let binaryDir = path.join(appOutDir, 'resources/bin')
fs.ensureDirSync(binaryDir)
let binPath = path.join(binaryDir, name)
if (platform === 'windows') {
binPath = binPath + '.exe'
}
let cmd = `cross-env GOOS=${platform} GOARCH=${arch} go build -o ${binPath} ${pkg}`
console.log(`> ${cmd}\n`)
let go = exec(cmd)

go.stdout.on('data', (data) => process.stdout.write(data))
go.stderr.on('data', (data) => process.stderr.write(data))
let output = 'gaia'
if (platform === 'windows') output += '.exe'

let cmd = `
docker run -v "/tmp:/mnt" golang bash -c "
go get github.com/cosmos/gaia;
cd /go/src/github.com/cosmos/gaia && \
git checkout develop && \
make get_vendor_deps && \
GOOS=darwin GOARCH=amd64 go build \
-o /mnt/${output} \
-ldflags '-s -w' \
./cmd/gaia
"
`
let docker = exec(cmd)
docker.stdout.on('data', (data) => process.stdout.write(data))
docker.stderr.on('data', (data) => process.stderr.write(data))
return new Promise((resolve, reject) => {
go.once('exit', (code) => {
docker.once('exit', (code) => {
if (code !== 0) return reject(Error('Build failed'))

let binPath = join(outDir, 'bin')
fs.ensureDirSync(binPath)
fs.copySync(join('/tmp', output), join(binPath, output))
resolve()
})
})
Expand All @@ -128,7 +139,7 @@ function goBuild (pkg, platform, arch) {
*/
function copyBinary (name, binaryLocation, platform) {
return function ({outDir, appOutDir}) {
let binPath = path.join(appOutDir, 'resources/bin', name)
let binPath = join(appOutDir, 'resources/bin', name)
if (platform === 'win32') {
binPath = binPath + '.exe'
}
Expand All @@ -141,18 +152,18 @@ function copyBinary (name, binaryLocation, platform) {
*/
function copyIcons () {
console.log('Copying icons to builds/resources')
let iconsPath = path.join(__dirname, '../app/icons')
let distPath = path.join(__dirname, '../builds/resources')
let iconsPath = join(__dirname, '../app/icons')
let distPath = join(__dirname, '../builds/resources')
fs.ensureDirSync(distPath)
fs.copyFileSync(path.join(iconsPath, 'icon.icns'), path.join(distPath, 'icon.icns'))
fs.copyFileSync(path.join(iconsPath, 'icon.ico'), path.join(distPath, 'icon.ico'))
fs.copySync(path.join(iconsPath, 'png'), path.join(distPath, 'png'))
fs.copyFileSync(join(iconsPath, 'icon.icns'), join(distPath, 'icon.icns'))
fs.copyFileSync(join(iconsPath, 'icon.ico'), join(distPath, 'icon.ico'))
fs.copySync(join(iconsPath, 'png'), join(distPath, 'png'))
}

function copyNetworks (appOutDir) {
console.log('Copying networks to folder')
let networksPath = path.join(__dirname, '../app/networks')
let networksOutPath = path.join(appOutDir, 'resources/networks')
let networksPath = join(__dirname, '../app/networks')
let networksOutPath = join(appOutDir, 'resources/networks')
fs.ensureDirSync(networksOutPath)
fs.copySync(networksPath, networksOutPath)
}
Loading

0 comments on commit f6384f2

Please sign in to comment.