|
| 1 | +def getStorybooksBucket() { |
| 2 | + return "ci-artifacts.kibana.dev/storybooks" |
| 3 | +} |
| 4 | + |
| 5 | +def getDestinationDir() { |
| 6 | + return env.ghprbPullId ? "pr-${env.ghprbPullId}" : buildState.get('checkoutInfo').branch.replace("/", "__") |
| 7 | +} |
| 8 | + |
| 9 | +def getUrl() { |
| 10 | + return "https://${getStorybooksBucket()}/${getDestinationDir()}" |
| 11 | +} |
| 12 | + |
| 13 | +def getUrlLatest() { |
| 14 | + return "${getUrl()}/latest" |
| 15 | +} |
| 16 | + |
| 17 | +def getUrlForCommit() { |
| 18 | + return "${getUrl()}/${buildState.get('checkoutInfo').commit}" |
| 19 | +} |
| 20 | + |
| 21 | +def upload() { |
| 22 | + dir("built_assets/storybook") { |
| 23 | + sh "mv ci_composite composite" |
| 24 | + |
| 25 | + def storybooks = sh( |
| 26 | + script: 'ls -1d */', |
| 27 | + returnStdout: true |
| 28 | + ).trim() |
| 29 | + .split('\n') |
| 30 | + .collect { it.replace('/', '') } |
| 31 | + .findAll { it != 'composite' } |
| 32 | + |
| 33 | + def listHtml = storybooks.collect { """<li><a href="${getUrlForCommit()}/${it}">${it}</a></li>""" }.join("\n") |
| 34 | + |
| 35 | + def html = """ |
| 36 | + <html> |
| 37 | + <body> |
| 38 | + <h1>Storybooks</h1> |
| 39 | + <p><a href="${getUrlForCommit()}/composite">Composite Storybook</a></p> |
| 40 | + <h2>All</h2> |
| 41 | + <ul> |
| 42 | + ${listHtml} |
| 43 | + </ul> |
| 44 | + </body> |
| 45 | + </html> |
| 46 | + """ |
| 47 | + |
| 48 | + writeFile(file: 'index.html', text: html) |
| 49 | + |
| 50 | + withGcpServiceAccount.fromVaultSecret('secret/kibana-issues/dev/ci-artifacts-key', 'value') { |
| 51 | + kibanaPipeline.bash(""" |
| 52 | + gsutil -q -m cp -r -z js,css,html,json,map,txt,svg '*' 'gs://${getStorybooksBucket()}/${getDestinationDir()}/${buildState.get('checkoutInfo').commit}/' |
| 53 | + gsutil -h "Cache-Control:no-cache, max-age=0, no-transform" cp -z html 'index.html' 'gs://${getStorybooksBucket()}/${getDestinationDir()}/latest/' |
| 54 | + """, "Upload Storybooks to GCS") |
| 55 | + } |
| 56 | + |
| 57 | + buildState.set('storybooksUrl', getUrlForCommit()) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +def build() { |
| 62 | + withEnv(["STORYBOOK_BASE_URL=${getUrlForCommit()}"]) { |
| 63 | + kibanaPipeline.bash('test/scripts/jenkins_storybook.sh', 'Build Storybooks') |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +def buildAndUpload() { |
| 68 | + def sha = buildState.get('checkoutInfo').commit |
| 69 | + def context = 'Build and Publish Storybooks' |
| 70 | + |
| 71 | + githubCommitStatus.create(sha, 'pending', 'Building Storybooks', context) |
| 72 | + |
| 73 | + try { |
| 74 | + build() |
| 75 | + upload() |
| 76 | + githubCommitStatus.create(sha, 'success', 'Storybooks built', context, getUrlForCommit()) |
| 77 | + } catch(ex) { |
| 78 | + githubCommitStatus.create(sha, 'error', 'Building Storybooks failed', context) |
| 79 | + throw ex |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +return this |
0 commit comments