|
| 1 | +const shell = require("shelljs") |
| 2 | +const { spawn } = require("child_process") |
| 3 | +const path = require("path") |
| 4 | +const GraphQLClient = require("graphql-request").GraphQLClient |
| 5 | +const queue = require(`async/queue`) |
| 6 | +const s3 = require("s3") |
| 7 | + |
| 8 | +const commitId = process.env.CODEBUILD_SOURCE_VERSION |
| 9 | + |
| 10 | +const s3Client = s3.createClient({ |
| 11 | + s3Options: { |
| 12 | + accessKeyId: process.env.accessKeyId, |
| 13 | + secretAccessKey: process.env.secretAccessKey, |
| 14 | + region: "us-east-1", |
| 15 | + endpoint: "s3.us-east-1.amazonaws.com", |
| 16 | + }, |
| 17 | +}) |
| 18 | + |
| 19 | +const client = new GraphQLClient( |
| 20 | + "https://api.graph.cool/simple/v1/cj8xuo77f0a3a0164y7jketkr", |
| 21 | + { |
| 22 | + headers: { |
| 23 | + Authorization: `Bearer ${process.env.GRAPHCOOL_TOKEN}`, |
| 24 | + }, |
| 25 | + } |
| 26 | +) |
| 27 | + |
| 28 | +function createBuild(sha, commitObjId, url) { |
| 29 | + return client.request(` |
| 30 | + mutation { |
| 31 | + createBuild(gitSha: "${sha}", result: BUILDING, url: "${url}", commitId: "${commitObjId}", logsIds: []) { |
| 32 | + id |
| 33 | + } |
| 34 | + } |
| 35 | + `) |
| 36 | +} |
| 37 | + |
| 38 | +function updateBuild(buildId, status) { |
| 39 | + return client.request(` |
| 40 | + mutation { |
| 41 | + updateBuild(id: "${buildId}", result: ${status}) { |
| 42 | + id |
| 43 | + } |
| 44 | + } |
| 45 | + `) |
| 46 | +} |
| 47 | + |
| 48 | +function createLogLine(logLine, buildId, stderr = false) { |
| 49 | + return client.request( |
| 50 | + ` |
| 51 | + mutation createLogLine($text: String!, $buildId: ID!, $stderr: Boolean) { |
| 52 | + createLogLine(text: $text, buildId: $buildId, stderr: $stderr) { |
| 53 | + id |
| 54 | + } |
| 55 | + } |
| 56 | + `, |
| 57 | + { text: logLine, buildId, stderr } |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +const getCommitObjectByHash = commitId => { |
| 62 | + return client |
| 63 | + .request( |
| 64 | + ` |
| 65 | + { |
| 66 | + allCommits( |
| 67 | + filter: { hash: "46f5ed9d8d05a9608bc9c98fcb5867a892149e94" } |
| 68 | + ) { |
| 69 | + id |
| 70 | + } |
| 71 | + } |
| 72 | + ` |
| 73 | + ) |
| 74 | + .then(result => result.allCommits[0].id) |
| 75 | +} |
| 76 | + |
| 77 | +console.log(process.env) |
| 78 | + |
| 79 | +var q = queue(function({ logLine, buildId, stderr }, callback) { |
| 80 | + createLogLine(logLine, buildId, stderr).then(callback) |
| 81 | +}, 1) |
| 82 | + |
| 83 | +const Main = async () => { |
| 84 | + if (!process.env.PATH_TO_SITE) { |
| 85 | + console.log("Missing required environment variable PATH_TO_SITE") |
| 86 | + process.exit(1) |
| 87 | + } |
| 88 | + |
| 89 | + console.log(process.cwd()) |
| 90 | + |
| 91 | + // Navigate to Gatsby directory |
| 92 | + shell.cd(`/gatsby/`) |
| 93 | + |
| 94 | + // Ensure we have the latest code |
| 95 | + shell.exec(`git fetch`) |
| 96 | + |
| 97 | + // Check if the commit exists (later we'll grab pull requests as well |
| 98 | + // but we're just building branches on gatsbyjs/gatsby while testing) |
| 99 | + const commitExists = shell.exec(`git cat-file -e ${commitId}`) |
| 100 | + console.log(`commitExists`, commitExists) |
| 101 | + if (commitExists.code !== 0) { |
| 102 | + process.exit(commitExists.code) |
| 103 | + } |
| 104 | + |
| 105 | + // It does! So let's checkout our commit and initialize the environment. |
| 106 | + shell.exec(`git checkout -b newbranch ${commitId}`) |
| 107 | + shell.exec(`yarn bootstrap`) |
| 108 | + |
| 109 | + // Now go to the site and install |
| 110 | + const pathToSite = path.join(`/gatsby/`, process.env.PATH_TO_SITE) |
| 111 | + shell.cd(pathToSite) |
| 112 | + shell.exec(`yarn`) |
| 113 | + shell.exec(`gatsby-dev --scan-once --quiet`) |
| 114 | + |
| 115 | + // Create the build |
| 116 | + const commitObjId = await getCommitObjectByHash(commitId) |
| 117 | + const url = `${process.env.PATH_TO_SITE}---${commitId}.gatsbydev.com` |
| 118 | + const result = await createBuild(commitId, commitObjId, url) |
| 119 | + const buildId = result.createBuild.id |
| 120 | + |
| 121 | + // Start building! |
| 122 | + const child = spawn(`gatsby`, [`build`]) |
| 123 | + child.on(`error`, err => console.log(`err:`, err)) |
| 124 | + child.stdout.pipe(process.stdout) |
| 125 | + child.stderr.pipe(process.stderr) |
| 126 | + child.stdout.on("data", data => { |
| 127 | + // Create new logline |
| 128 | + q.push({ logLine: data.toString("utf8"), buildId }) |
| 129 | + }) |
| 130 | + child.stderr.on("data", data => { |
| 131 | + // Create new logline |
| 132 | + q.push({ logLine: data.toString("utf8"), buildId, stderr: true }) |
| 133 | + }) |
| 134 | + |
| 135 | + // On end |
| 136 | + child.on("exit", (code, signal) => { |
| 137 | + console.log( |
| 138 | + "child process exited with " + `code ${code} and signal ${signal}` |
| 139 | + ) |
| 140 | + // Gatsby build failed |
| 141 | + if (code !== 0) { |
| 142 | + updateBuild(buildId, "FAILURE") |
| 143 | + process.exit(code) |
| 144 | + } |
| 145 | + const publicDir = `${pathToSite}/public` |
| 146 | + console.log(`uploading files from ${publicDir}`) |
| 147 | + |
| 148 | + // 1. Push built files to s3 bucket |
| 149 | + const upload = s3Client.uploadDir({ |
| 150 | + localDir: publicDir, |
| 151 | + s3Params: { |
| 152 | + Prefix: url, |
| 153 | + Bucket: `gatsby-js-builds`, |
| 154 | + }, |
| 155 | + }) |
| 156 | + upload.on(`fileUploadEnd`, () => |
| 157 | + console.log(`${upload.progressAmount / upload.progressTotal}`) |
| 158 | + ) |
| 159 | + upload.on(`error`, error => { |
| 160 | + console.error(error) |
| 161 | + updateBuild(buildId, "FAILURE").then(() => { |
| 162 | + process.exit(code) |
| 163 | + }) |
| 164 | + }) |
| 165 | + // 2. Write final status of build and exit. |
| 166 | + upload.on(`end`, () => { |
| 167 | + updateBuild(buildId, "SUCCESS").then(() => process.exit()) |
| 168 | + }) |
| 169 | + }) |
| 170 | +} |
| 171 | + |
| 172 | +Main() |
0 commit comments