Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

clone-for-build.js 4.5 KB

You have to be logged in to leave a comment. Sign In
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  1. #!/usr/bin/env node
  2. // [start-readme]
  3. //
  4. // This script is run as a postbuild script during staging and deployments on Heroku. It clones a branch
  5. // in the early-access repo that matches the current branch in the docs repo; if one can't be found, it
  6. // clones the `main` branch.
  7. //
  8. // [end-readme]
  9. require('dotenv').config()
  10. const {
  11. DOCUBOT_REPO_PAT,
  12. HEROKU_PRODUCTION_APP,
  13. GIT_BRANCH // Set by Actions and/or the deployer with the name of the docs-internal branch
  14. } = process.env
  15. // Exit if PAT is not found
  16. if (!DOCUBOT_REPO_PAT) {
  17. console.log('Skipping early access, not authorized')
  18. process.exit(0)
  19. }
  20. const { execSync } = require('child_process')
  21. const rimraf = require('rimraf').sync
  22. const fs = require('fs')
  23. const path = require('path')
  24. const os = require('os')
  25. const EA_PRODUCTION_BRANCH = 'main'
  26. // If a branch name is not provided in the environment, attempt to get
  27. // the local branch name; or default to 'main'
  28. let currentBranch = (GIT_BRANCH || '').replace(/^refs\/heads\//, '')
  29. if (!currentBranch) {
  30. try {
  31. currentBranch = execSync('git branch --show-current').toString()
  32. } catch (err) {
  33. // Ignore but log
  34. console.warn('Error checking for local branch:', err.message)
  35. }
  36. }
  37. if (!currentBranch) {
  38. currentBranch = EA_PRODUCTION_BRANCH
  39. }
  40. // Early Access details
  41. const earlyAccessOwner = 'github'
  42. const earlyAccessRepoName = 'docs-early-access'
  43. const earlyAccessDirName = 'early-access'
  44. const earlyAccessFullRepo = `https://${DOCUBOT_REPO_PAT}@github.com/${earlyAccessOwner}/${earlyAccessRepoName}`
  45. // On our Azure self-hosted runners, os.tmpdir() doesn't work reliably. On Heroku, os.homedir doesn't work reliably.
  46. const earlyAccessCloningParentDir = process.env.CI ? os.homedir() : os.tmpdir()
  47. const earlyAccessCloningDir = path.join(earlyAccessCloningParentDir, earlyAccessRepoName)
  48. const destinationDirNames = ['content', 'data', 'assets/images']
  49. const destinationDirsMap = destinationDirNames
  50. .reduce(
  51. (map, dirName) => {
  52. map[dirName] = path.join(process.cwd(), dirName, earlyAccessDirName)
  53. return map
  54. },
  55. {}
  56. )
  57. // Production vs. staging environment
  58. // TODO test that this works as expected
  59. const environment = HEROKU_PRODUCTION_APP ? 'production' : 'staging'
  60. // Early access branch to clone
  61. let earlyAccessBranch = HEROKU_PRODUCTION_APP ? EA_PRODUCTION_BRANCH : currentBranch
  62. // Confirm that the branch exists in the remote
  63. let branchExists = execSync(`git ls-remote --heads ${earlyAccessFullRepo} ${earlyAccessBranch}`).toString()
  64. // If the branch did NOT exist, try checking for the default branch instead
  65. if (!branchExists && earlyAccessBranch !== EA_PRODUCTION_BRANCH) {
  66. console.warn(`The branch '${earlyAccessBranch}' was not found in ${earlyAccessOwner}/${earlyAccessRepoName}!`)
  67. console.warn(`Attempting the default branch ${EA_PRODUCTION_BRANCH} instead...`)
  68. earlyAccessBranch = EA_PRODUCTION_BRANCH
  69. branchExists = execSync(`git ls-remote --heads ${earlyAccessFullRepo} ${earlyAccessBranch}`).toString()
  70. }
  71. // If no suitable branch was found, bail out now
  72. if (!branchExists) {
  73. console.error(`The branch '${earlyAccessBranch}' was not found in ${earlyAccessOwner}/${earlyAccessRepoName}!`)
  74. console.error('Exiting!')
  75. process.exit(1)
  76. }
  77. // Remove any previously cloned copies of the early access repo
  78. rimraf(earlyAccessCloningDir)
  79. // Clone the repo
  80. console.log(`Setting up: ${earlyAccessCloningDir}`)
  81. execSync(
  82. `git clone --single-branch --branch ${earlyAccessBranch} ${earlyAccessFullRepo} ${earlyAccessRepoName}`,
  83. {
  84. cwd: earlyAccessCloningParentDir
  85. }
  86. )
  87. console.log(`Using early-access ${environment} branch: '${earlyAccessBranch}'`)
  88. // Remove all existing early access directories from this repo
  89. destinationDirNames.forEach(key => rimraf(destinationDirsMap[key]))
  90. // Move the latest early access source directories into this repo
  91. destinationDirNames.forEach((dirName) => {
  92. const sourceDir = path.join(earlyAccessCloningDir, dirName)
  93. const destDir = destinationDirsMap[dirName]
  94. // If the source directory doesn't exist, skip it
  95. if (!fs.existsSync(sourceDir)) {
  96. console.warn(`Early access directory '${dirName}' does not exist. Skipping...`)
  97. return
  98. }
  99. // Move the directory from the cloned source to the destination
  100. fs.renameSync(sourceDir, destDir)
  101. // Confirm the newly moved directory exist
  102. if (fs.existsSync(destDir)) {
  103. console.log(`Successfully moved early access directory '${dirName}' into this repo`)
  104. } else {
  105. throw new Error(`Failed to move early access directory '${dirName}'!`)
  106. }
  107. })
  108. // Remove the source content again for good hygiene
  109. rimraf(earlyAccessCloningDir)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...