|
| 1 | +#! /usr/bin/env node |
| 2 | +/* eslint-disable camelcase */ |
| 3 | + |
| 4 | +const path = require('path'); |
| 5 | +const fs = require('fs'); |
| 6 | + |
| 7 | +const github = require('octonode'); |
| 8 | +const travisAfterAll = require('travis-after-all'); |
| 9 | +const urlRegex = require('url-regex'); |
| 10 | +const normalizeUrl = require('normalize-url'); |
| 11 | +const axios = require('axios'); |
| 12 | + |
| 13 | +const argv = require('yargs') |
| 14 | + .option('debug', { |
| 15 | + alias: 'd', |
| 16 | + description: 'Show debug info', |
| 17 | + type: Boolean, |
| 18 | + }) |
| 19 | + .option('public', { |
| 20 | + alias: 'p', |
| 21 | + description: 'Deployment is public (`/_src` is exposed)', |
| 22 | + type: Boolean, |
| 23 | + }) |
| 24 | + .option('team', { |
| 25 | + alias: 'T', |
| 26 | + description: 'Set a custom team scope', |
| 27 | + type: String, |
| 28 | + }) |
| 29 | + .option('folder', { |
| 30 | + alias: 'F', |
| 31 | + description: 'Set a folder to deploy', |
| 32 | + type: String, |
| 33 | + }) |
| 34 | + .option('comment', { |
| 35 | + alias: 'c', |
| 36 | + description: |
| 37 | + 'Post a comment to the PR issue summarizing the now deployment results', |
| 38 | + default: true, |
| 39 | + type: Boolean, |
| 40 | + }) |
| 41 | + .help() |
| 42 | + .alias('help', 'h').argv; |
| 43 | + |
| 44 | +const { runNow, runNowAlias } = require('./now'); |
| 45 | + |
| 46 | +if (!process.env.CI || !process.env.TRAVIS) { |
| 47 | + throw new Error('Could not detect Travis CI environment'); |
| 48 | +} |
| 49 | + |
| 50 | +const githubToken = process.env.GH_TOKEN; |
| 51 | +const nowToken = process.env.NOW_TOKEN; |
| 52 | +const discordHook = process.env.DISCORD_HOOK; |
| 53 | +const prSha = process.env.TRAVIS_PULL_REQUEST_SHA; |
| 54 | +const commitSha = process.env.TRAVIS_COMMIT; |
| 55 | +const repoSlug = process.env.TRAVIS_REPO_SLUG; |
| 56 | +const aliasUrl = process.env.NOW_ALIAS; |
| 57 | + |
| 58 | +if (!githubToken) { |
| 59 | + throw new Error('Missing required environment variable GH_TOKEN'); |
| 60 | +} |
| 61 | + |
| 62 | +if (!nowToken) { |
| 63 | + throw new Error('Missing required environment variable NOW_TOKEN'); |
| 64 | +} |
| 65 | + |
| 66 | +const ghClient = github.client(githubToken); |
| 67 | +const ghRepo = ghClient.repo(repoSlug); |
| 68 | +const ghIssue = ghClient.issue(repoSlug, process.env.TRAVIS_PULL_REQUEST); |
| 69 | + |
| 70 | +function getUrl(content) { |
| 71 | + const urls = content.match(urlRegex()) || []; |
| 72 | + return urls.map(url => normalizeUrl(url.trim().replace(/\.+$/, '')))[0]; |
| 73 | +} |
| 74 | + |
| 75 | +const baseArgs = ['--token', nowToken]; |
| 76 | + |
| 77 | +const nowArgs = ['--no-clipboard']; |
| 78 | + |
| 79 | +if (argv.debug || argv.d) { |
| 80 | + baseArgs.push('--debug'); |
| 81 | +} |
| 82 | + |
| 83 | +if (argv.team || argv.T) { |
| 84 | + baseArgs.push('--team'); |
| 85 | + baseArgs.push(argv.team || argv.T); |
| 86 | +} |
| 87 | + |
| 88 | +if (argv.public || argv.p) { |
| 89 | + nowArgs.push('--public'); |
| 90 | +} |
| 91 | + |
| 92 | +if (argv.folder || argv.F) { |
| 93 | + const deployPath = path.resolve(argv.folder); |
| 94 | + if (fs.statSync(deployPath).isDirectory()) { |
| 95 | + nowArgs.push('--name', repoSlug.replace('/', '-')); |
| 96 | + nowArgs.push(deployPath); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +function notifyInDiscord(err, res) { |
| 101 | + if (err) { |
| 102 | + return axios |
| 103 | + .post(discordHook, { |
| 104 | + username: `${repoSlug.replace('/', '-')}-BOT`, |
| 105 | + content: `Deploymet failed check travis logs here https://travis-ci.org/coderplex/coderplex/builds/${process |
| 106 | + .env.TRAVIS_BUILD_ID}`, |
| 107 | + }) |
| 108 | + .then(() => { |
| 109 | + console.log(`Error posted to discord`); |
| 110 | + }) |
| 111 | + .catch(console.log.bind(console)); |
| 112 | + } |
| 113 | + return axios |
| 114 | + .post(discordHook, { |
| 115 | + username: `${repoSlug.replace('/', '-')}-BOT`, |
| 116 | + content: buildComment(res.context, res.url, 'https://coderplex.org'), |
| 117 | + }) |
| 118 | + .then(() => { |
| 119 | + console.log(`Error posted to discord`); |
| 120 | + }) |
| 121 | + .catch(console.log.bind(console)); |
| 122 | +} |
| 123 | + |
| 124 | +function buildComment(context, url, aliasUrl) { |
| 125 | + return `### New Δ Now ${context} deployment complete\n- ✅ **Build Passed**\n- 🚀 **URL** : ${aliasUrl |
| 126 | + ? aliasUrl |
| 127 | + : url}\n---\nNote: **This is autogenerated through travis-ci build**`; |
| 128 | +} |
| 129 | + |
| 130 | +function deploy(context, sha) { |
| 131 | + // Send error status to github PR |
| 132 | + ghRepo.status( |
| 133 | + sha, |
| 134 | + { |
| 135 | + context, |
| 136 | + state: 'pending', |
| 137 | + description: `Δ Now ${context} deployment pending`, |
| 138 | + }, |
| 139 | + console.log.bind(console), |
| 140 | + ); |
| 141 | + // Initiate deployment process |
| 142 | + runNow([...baseArgs, ...nowArgs], (code, res) => { |
| 143 | + // Remember, process code: 0 means success else failure in unix/linux |
| 144 | + if (code) { |
| 145 | + // Send error status to github PR |
| 146 | + ghRepo.status( |
| 147 | + sha, |
| 148 | + { |
| 149 | + context, |
| 150 | + state: 'error', |
| 151 | + description: `Δ Now ${context} deployment failed`, |
| 152 | + }, |
| 153 | + console.log.bind(console), |
| 154 | + ); |
| 155 | + // Notify in discord |
| 156 | + notifyInDiscord(true); |
| 157 | + return console.log(`now process exited with code ${code}`); |
| 158 | + } |
| 159 | + |
| 160 | + // Retrieve now.sh unique url from stdOut |
| 161 | + const deployedUrl = getUrl(res); |
| 162 | + |
| 163 | + if (context === 'staging') { |
| 164 | + // Send success status to github PR |
| 165 | + ghRepo.status( |
| 166 | + sha, |
| 167 | + { |
| 168 | + context, |
| 169 | + target_url: deployedUrl, |
| 170 | + state: 'success', |
| 171 | + description: `Δ Now ${context} deployment complete`, |
| 172 | + }, |
| 173 | + console.log.bind(console), |
| 174 | + ); |
| 175 | + // Check and create comment on github PR abot deployment results |
| 176 | + if (argv.comment) { |
| 177 | + ghIssue.createComment( |
| 178 | + { |
| 179 | + body: buildComment(context, deployedUrl), |
| 180 | + }, |
| 181 | + console.log.bind(console), |
| 182 | + ); |
| 183 | + } |
| 184 | + return; |
| 185 | + } |
| 186 | + // In production alias deployment to specified alias url from now.json file or from env variable |
| 187 | + if (context === 'production') { |
| 188 | + runNowAlias(baseArgs, { deployedUrl, aliasUrl }, code => { |
| 189 | + if (code) { |
| 190 | + // Notify failure in discord. |
| 191 | + notifyInDiscord(true); |
| 192 | + return console.log(`now process exited with code ${code}`); |
| 193 | + } |
| 194 | + // Notify success in discord |
| 195 | + notifyInDiscord(false, { context, url: deployedUrl, aliasUrl }); |
| 196 | + console.log('🎉 Done'); |
| 197 | + }); |
| 198 | + } |
| 199 | + }); |
| 200 | +} |
| 201 | + |
| 202 | +travisAfterAll((code, err) => { |
| 203 | + if (err || code) return; |
| 204 | + switch (process.env.TRAVIS_EVENT_TYPE) { |
| 205 | + case 'pull_request': |
| 206 | + return deploy('staging', prSha); |
| 207 | + case 'push': |
| 208 | + return deploy('production', commitSha); |
| 209 | + default: |
| 210 | + break; |
| 211 | + } |
| 212 | +}); |
0 commit comments