From 3a8b0afabba76c3be4d466e8fdae5f08c1d9ba1a Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Wed, 25 Oct 2017 14:52:14 +0000 Subject: [PATCH] Use @cypress/deploy-bits (#171) * use code from deploy-bits to publish to S3, close #170 * remove obsolete code * remove this branch from deploy job --- cy_scripts/deploy.js | 146 +++++-------------------------------------- package.json | 3 +- 2 files changed, 16 insertions(+), 133 deletions(-) diff --git a/cy_scripts/deploy.js b/cy_scripts/deploy.js index 6225cd9337..70828b7057 100644 --- a/cy_scripts/deploy.js +++ b/cy_scripts/deploy.js @@ -1,25 +1,25 @@ /* eslint-disable no-console */ -const _ = require('lodash') const path = require('path') const gift = require('gift') -const gulp = require('gulp') const chalk = require('chalk') -const human = require('human-interval') const Promise = require('bluebird') const inquirer = require('inquirer') -const awspublish = require('gulp-awspublish') -const parallelize = require('concurrent-transform') const minimist = require('minimist') const debug = require('debug')('deploy') const questionsRemain = require('@cypress/questions-remain') const scrape = require('./scrape') const shouldDeploy = require('./should-deploy') -const { configFromEnvOrJsonFile } = require('@cypress/env-or-json-file') const R = require('ramda') const la = require('lazy-ass') const is = require('check-more-types') const git = require('ggit') +const { + warnIfNotCI, + getDeployEnvironment, + checkBranchEnvFolder, + uploadToS3, +} = require('@cypress/deploy-bits') const distDir = path.resolve('public') const isValidEnvironment = is.oneOf(['production', 'staging']) @@ -27,35 +27,10 @@ const isValidEnvironment = is.oneOf(['production', 'staging']) // initialize on existing repo const repo = Promise.promisifyAll(gift(path.resolve('..'))) -function getS3Credentials () { - const key = path.join('support', '.aws-credentials.json') - const config = configFromEnvOrJsonFile(key) - if (!config) { - console.error('⛔️ Cannot find AWS credentials') - console.error('Using @cypress/env-or-json-file module') - console.error('and key', key) - throw new Error('AWS config not found') - } - return config -} - function getCurrentBranch () { return git.branchName() } -function promptForDeployEnvironment () { - return prompt({ - type: 'list', - name: 'strategy', - message: 'Which environment are you deploying?', - choices: [ - { name: 'Staging', value: 'staging' }, - { name: 'Production', value: 'production' }, - ], - }) - .get('strategy') -} - function cliOrAsk (property, ask, minimistOptions) { // for now isolate the CLI/question logic const askRemaining = questionsRemain({ @@ -65,83 +40,6 @@ function cliOrAsk (property, ask, minimistOptions) { return askRemaining(options).then(R.prop(property)) } -const getDeployEnvironment = R.partial(cliOrAsk, - ['environment', promptForDeployEnvironment]) - -function ensureCleanWorkingDirectory () { - return repo.statusAsync() - .catch((e) => { - console.error('Could not get Git status') - console.error(e) - console.error('assuming clean status') - return { clean: true } - }) - .then((status) => { - if (!status.clean) { - console.log(chalk.red('\nUncommited files:')) - - _.each(status.files, (props, file) => { - console.log(chalk.red(`- ${file}`)) - }) - - console.log('') - - throw new Error('Cannot deploy master to production. You must first commit these above files.') - } - }) -} - -function getPublisher (bucket, key, secret) { - return awspublish.create({ - httpOptions: { - timeout: human('10 minutes'), - }, - params: { - Bucket: bucket, - }, - accessKeyId: key, - secretAccessKey: secret, - }) -} - -function publishToS3 (publisher) { - const headers = {} - - return new Promise((resolve, reject) => { - const files = path.join(distDir, '**', '*') - - return gulp.src(files) - .pipe(parallelize(publisher.publish(headers), 100)) - - // we dont need to gzip here because cloudflare - // will automatically gzip the content for us - // after its cached at their edge location - // but we should probably gzip the index.html? - // .pipe(awspublish.gzip({ext: '.gz'})) - - .pipe(awspublish.reporter()) - .on('error', reject) - .on('end', resolve) - }) -} - -function uploadToS3 (env) { - la(isValidEnvironment(env), 'invalid environment', env) - const bucketName = `bucket-${env}` - return Promise.resolve() - .then(getS3Credentials) - .then((json) => { - la(is.object(json), 'missing S3 credentials object for environment', env) - const bucket = json[bucketName] - la(is.unemptyString(bucket), 'Could not find a bucket for environment', env) - - console.log('\n', 'Deploying to:', chalk.green(bucket), '\n') - const publisher = getPublisher(bucket, json.key, json.secret) - return publisher - }) - .then(publishToS3) -} - function prompt (questions) { return Promise.resolve(inquirer.prompt(questions)) } @@ -213,25 +111,6 @@ function deployEnvironmentBranch (env, branch) { la(is.unemptyString(branch), 'missing branch to deploy', branch) la(isValidEnvironment(env), 'invalid deploy environment', env) - const cleanup = () => { - console.log('Target environment:', chalk.green(env)) - console.log('On branch:', chalk.green(branch), '\n') - if (env === 'staging') { - return env - } - - if (env === 'production') { - if (branch !== 'master') { - throw new Error('Cannot deploy master to production. You are not on the \'master\' branch.') - } - - return ensureCleanWorkingDirectory() - } else { - throw new Error(`Unknown environment: ${env}`) - } - } - - const uploadEnvToS3 = _.partial(uploadToS3, env) const maybeCommit = () => commitMessage(env, branch) .catch((err) => { @@ -240,9 +119,9 @@ function deployEnvironmentBranch (env, branch) { console.error(err.message) }) - return Promise.resolve() - .then(cleanup) - .then(uploadEnvToS3) + checkBranchEnvFolder(branch)(env) + + uploadToS3(distDir, env) .then(maybeCommit) .then(() => scrapeDocs(env, branch)) .then(() => { @@ -257,7 +136,8 @@ function doDeploy (env) { debug('getting current branch') return getCurrentBranch() .then((branch) => { - console.log('deploying branch %s to %s', branch, env) + console.log('deploying branch %s to environment %s', + chalk.green(branch), chalk.blue(env)) la(is.unemptyString(branch), 'invalid branch name', branch) return deployEnvironmentBranch(env, branch) }) @@ -265,9 +145,11 @@ function doDeploy (env) { function deploy () { console.log() - console.log(chalk.yellow('Cypress Docs Deployinator')) + console.log(chalk.yellow('Cypress Docs Deploy')) console.log(chalk.yellow('==============================\n')) + warnIfNotCI() + return getDeployEnvironment() .then((env) => { return shouldDeploy(env) diff --git a/package.json b/package.json index 6a87906d7c..7ac2ae2c5d 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "deps": "deps-ok && dependency-check . --no-default-entries --entry cy_scripts/deploy.js", "prebuild": "npm run clean && npm run deps && gulp pre:build", "build": "node --stack-size=8192 ./index.js generate", - "postbuild": "gulp post:build && git-last -f public/build.json", + "postbuild": "gulp post:build && git-last -m -f public/build.json", "clean": "hexo clean", "clean-deps": "rm -rf node_modules", "predeploy": "NODE_ENV=production npm run build", @@ -37,6 +37,7 @@ "postlint": "eslint --fix *.js cy_scripts/*.js scripts/*.js" }, "devDependencies": { + "@cypress/deploy-bits": "1.6.0", "@cypress/env-or-json-file": "^1.0.0", "@cypress/questions-remain": "^1.0.1", "beeper": "^1.1.1",