Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cpdeploy

Package Overview
Dependencies
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cpdeploy - npm Package Compare versions

Comparing version 1.2.0 to 1.2.1

102

index.js
#!/usr/bin/env node
'use strict';
const get = require('lodash/get')
const execSync = require('child_process').execSync
const readFileSync = require('fs').readFileSync
const pjson = JSON.parse(readFileSync('package.json'))
const config = pjson.config || {}
const tagName = pjson.version
const argv = require('yargs')
.option('pre-tag', {
alias: 'ot',
describe: 'Commands to execute before building the docker image',
type: 'array',
default: [],
}).argv
function authConfig() {
['ecrRepoUrl', 'ecrRepoName'].forEach(key => {
if (! (key in config)) {
console.error(`Missing required key config.${key}`)
process.exit(1)
}
})
function readPackageKey(key) {
const value = get(pjson, key)
if (value) {
console.log(`- ${key}: ${value}`)
return value
} else {
console.error(`Missing \`${key}\` key in \`package.json\` `)
process.exit(1)
}
}
console.log('Deployment configuration:')
const repoName = readPackageKey('name')
const tagName = readPackageKey('version')
const ecrRepoName = readPackageKey('config.ecrRepoName')
const ecrRepoUrl = readPackageKey('config.ecrRepoUrl')
// if pushing a tag
// check if deps are met, exit with non-zero if not
// do the docker-login stuff
// build the docker image
// tag the docker image with the same tag that is being pushed
// push the docker image
// if it fails, exit non-zero. the output of the command will appear so hopefully that's enough of an error message.
// If success, exit 0
function exec(cmd, opts={}) {

@@ -46,50 +37,17 @@ opts.stdio = 'stdio' in opts ? opts.stdio : [0, 1, 2]

function buildAndPushDockerImage() {
const remoteTag = `${config.ecrRepoUrl}/${config.ecrRepoName}:${tagName}`
const localTag = `${config.ecrRepoName}:${tagName}`
exec('`aws ecr get-login --region eu-west-1`')
exec(`docker build -t ${localTag} .`)
exec(`docker tag ${localTag} ${remoteTag}`)
exec(`docker push ${remoteTag}`)
function buildAndPushDockerImageRemote() {
const remoteTag = `${ecrRepoUrl}/${ecrRepoName}:${tagName}`
const localTag = `${ecrRepoName}:${tagName}`
let sshcmd = [
`cd ${repoName}`,
'git fetch',
`git checkout v${tagName}`,
'eval $(aws ecr get-login --region eu-west-1)',
`docker build -t ${localTag} .`,
`docker tag ${localTag} ${remoteTag}`,
`docker push ${remoteTag}`,
].join(' && ')
exec(`ssh docker-build "${sshcmd}"`)
}
function startDockerIfNeeded() {
if (process.platform == 'darwin') {
try {
execSync('docker ps')
} catch(e) {
exec('docker-machine restart')
exec('docker-machine env', {stdio: undefined}).toString().split('\n')
.filter(c => c.startsWith('export'))
.map(c => c.split('='))
.map(([key, val]) => [key.replace('export ', ''), val.slice(1, -1)])
.map(([key, val]) => process.env[key] = val)
}
}
}
// Checks that the 'docker' and 'aws' commands exist, if they do not, will
// exit in error
function checkDeps() {
const opts = {stdio: undefined}
exec('which docker', opts)
exec('which aws', opts)
}
function runPreArgs() {
argv['pre-tag'].forEach(cmd => {
exec(cmd)
})
}
process.stdin.once('data', data => {
const commitLine = data.toString().trim()
if (commitLine.startsWith('refs/tags/')) {
console.log('Detected tag push, attempting to build docker image')
authConfig()
checkDeps()
runPreArgs()
startDockerIfNeeded()
buildAndPushDockerImage()
}
})
buildAndPushDockerImageRemote()
{
"name": "cpdeploy",
"version": "1.2.0",
"version": "1.2.1",
"description": "",

@@ -24,5 +24,4 @@ "main": "index.js",

"dependencies": {
"husky": "~0.11.4",
"yargs": "~4.7.1"
"lodash": "~4.15.0"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc