aboutsummaryrefslogtreecommitdiffstats
path: root/deploy-ui-gcs/index.js
blob: 1701f6e8e4388d105bd542143864bbaea189f621 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const { Storage } = require("@google-cloud/storage")
const PubSub = require("@google-cloud/pubsub")
const JSZip = require("jszip")

const storage = new Storage()
// const projectId = process.env.GCP_PROJECT || process.env.GCLOUD_PROJECT
const artifactBucketName = "gs://slam-lang-gh-artifacts"
const uiDeployBucketName = "gs://slam-lang-ui"

const pubsub = new PubSub();
const resultTopicName = "deploy-ui-gcs-result"
/**
 * Triggered from a message on a Cloud Pub/Sub topic.
 *
 * @param {!Object} event Event payload.
 * @param {!Object} context Metadata for the event.
 */
exports.deployPubSub = async (event, context) => {
  console.log("Received event:", event)

  const json = JSON.parse(Buffer.from(event.data, "base64").toString("utf8"))
  const revision = json.revision
  console.log("Parsed revision from event:", revision)
  const resultPublisher = pubsub.topic(resultTopicName).publisher()

  try {
    const archiveName = `ui-${revision}.zip`

    const artifactBucket = storage.bucket(artifactBucketName)
    const archiveFile = artifactBucket.file(archiveName)

    console.log("Downloading zip archive", archiveName)
    const zipData = await archiveFile.download()
    if (zipData.length !== 1) {
      throw new Error("zipData.length !== 1")
    }

    const zipContent = await JSZip.loadAsync(zipData[0])
    const zippedPaths = Object.keys(zipContent.files)

    console.log("Zipped content to deploy:", zippedPaths)

    const uiDeployBucket = storage.bucket(uiDeployBucketName)

    const prevFiles = await uiDeployBucket.getFiles()
    if (prevFiles.length !== 1) {
      throw new Error("prevFiles.length !== 1")
    }
    const prevFilePaths = prevFiles[0].map(f => f.name)
    console.log("Existing files:", prevFilePaths)

    const newFilePaths = []
    zippedPaths.forEach(filePath => {
      const zipFile = zipContent.files[filePath]
      if (zipFile.dir) return

      const deployFilePath = filePath.replace(/^dist\//, "")
      newFilePaths.push(deployFilePath)

      const newBucketFile = uiDeployBucket.file(deployFilePath)

      const nodeStream = zipFile.nodeStream()
      nodeStream.pipe(
        newBucketFile.createWriteStream({
          resumable: false,
          validation: false,
          contentType: "auto",
          metadata: {
            "Cache-Control": "no-store",
          },
        })
      )
    })
    console.log("Deployed new files:", newFilePaths)

    const filePathsToDelete = prevFilePaths
      .filter(prevFilePath => !newFilePaths.includes(prevFilePath))

    console.log("Obsolete files to delete:", filePathsToDelete)

    await Promise.all(
      filePathsToDelete.map(filePath =>
        uiDeployBucket.file(filePath).delete()
      )
    )

    console.log("Done!");

    await resultPublisher.publish(Buffer.from(JSON.stringify({
      revision,
      ok: true
    })))
  } catch (err) {
    console.error(err)
    await resultPublisher.publish(Buffer.from(JSON.stringify({
      revision,
      ok: false
    })))
    throw err
  }
};