|
19 | 19 | // Define common lifecycle tasks and artifact types
|
20 | 20 | apply plugin: "base"
|
21 | 21 |
|
22 |
| -task cleanWebsite(type: Delete) { |
23 |
| - delete '.sass-cache', 'vendor', '.testcontent', 'content' |
| 22 | +def dockerImageTag = 'beam-website' |
| 23 | +def dockerWorkDir = "/repo" |
| 24 | +def buildDir = "$project.rootDir/build/website" |
| 25 | + |
| 26 | +task buildDockerImage(type: Exec) { |
| 27 | + inputs.files 'Gemfile', 'Gemfile.lock' |
| 28 | + commandLine 'docker', 'build', '-t', dockerImageTag, '.' |
24 | 29 | }
|
25 |
| -clean.dependsOn cleanWebsite |
26 | 30 |
|
27 |
| -task installBundler(type: Exec) { |
28 |
| - commandLine './install_bundler.sh' |
| 31 | +task createDockerContainer(type: Exec) { |
| 32 | + dependsOn buildDockerImage |
| 33 | + standardOutput = new ByteArrayOutputStream() |
| 34 | + ext.containerId = { |
| 35 | + return standardOutput.toString().trim() |
| 36 | + } |
| 37 | + commandLine '/bin/bash', '-c', |
| 38 | + "docker create -v $project.rootDir:$dockerWorkDir -u \$(id -u):\$(id -g) $dockerImageTag" |
29 | 39 | }
|
30 | 40 |
|
31 |
| -task installGems(type: Exec) { |
32 |
| - mustRunAfter cleanWebsite, installBundler |
33 |
| - inputs.file 'Gemfile.lock' |
34 |
| - outputs.dir 'vendor' |
35 |
| - commandLine 'bundle', 'install', '--deployment' |
| 41 | +task startDockerContainer(type: Exec) { |
| 42 | + dependsOn createDockerContainer |
| 43 | + ext.containerId = { |
| 44 | + return createDockerContainer.containerId() |
| 45 | + } |
| 46 | + commandLine 'docker', 'start', |
| 47 | + "${->createDockerContainer.containerId()}" // Lazily evaluate containerId. |
36 | 48 | }
|
37 | 49 |
|
38 |
| -// TODO(BEAM-4696): Remove this once we can run Jenkins jobs within Docker |
39 |
| -task setupJenkins { |
40 |
| - dependsOn installBundler, installGems |
| 50 | +task stopAndRemoveDockerContainer(type: Exec) { |
| 51 | + commandLine 'docker', 'rm', '-f', "${->createDockerContainer.containerId()}" |
41 | 52 | }
|
42 | 53 |
|
| 54 | +task setupBuildDir(type: Copy) { |
| 55 | + from('.') { |
| 56 | + include 'Gemfile*' |
| 57 | + include 'Rakefile' |
| 58 | + } |
| 59 | + into buildDir |
| 60 | +} |
| 61 | + |
| 62 | +task cleanWebsite(type: Delete) { |
| 63 | + delete buildDir |
| 64 | +} |
| 65 | +clean.dependsOn cleanWebsite |
| 66 | + |
43 | 67 | task buildWebsite(type: Exec) {
|
44 |
| - dependsOn installGems |
45 |
| - mustRunAfter cleanWebsite, setupJenkins |
| 68 | + dependsOn startDockerContainer, setupBuildDir |
| 69 | + finalizedBy stopAndRemoveDockerContainer |
46 | 70 | inputs.files 'Gemfile.lock', '_config.yml'
|
47 | 71 | inputs.dir 'src'
|
48 |
| - outputs.dir '.sass-cache' |
49 |
| - outputs.dir 'content' |
50 |
| - outputs.dir '.testcontent' |
51 |
| - commandLine 'bundle', 'exec', 'jekyll', 'build', |
52 |
| - '--config', '_config.yml', |
53 |
| - '--incremental' |
| 72 | + outputs.dir "$buildDir/.sass-cache" |
| 73 | + outputs.dir "$buildDir/content" |
| 74 | + commandLine 'docker', 'exec', |
| 75 | + "${->startDockerContainer.containerId()}", '/bin/bash', '-c', |
| 76 | + """cd $dockerWorkDir/build/website && \ |
| 77 | + bundle exec jekyll build \ |
| 78 | + --config $dockerWorkDir/website/_config.yml \ |
| 79 | + --incremental \ |
| 80 | + --source $dockerWorkDir/website/src |
| 81 | + """ |
54 | 82 | }
|
55 | 83 | build.dependsOn buildWebsite
|
56 | 84 |
|
57 | 85 | task testWebsite(type: Exec) {
|
58 |
| - dependsOn buildWebsite |
59 |
| - inputs.files 'Gemfile.lock', 'Rakefile' |
60 |
| - inputs.dir '.testcontent' |
61 |
| - commandLine 'bundle', 'exec', 'rake', 'test' |
| 86 | + dependsOn startDockerContainer, buildWebsite |
| 87 | + finalizedBy stopAndRemoveDockerContainer |
| 88 | + |
| 89 | + inputs.files "$buildDir/Rakefile" |
| 90 | + inputs.dir "$buildDir/content" |
| 91 | + commandLine 'docker', 'exec', |
| 92 | + "${->startDockerContainer.containerId()}", '/bin/bash', '-c', |
| 93 | + """cd $dockerWorkDir/build/website && \ |
| 94 | + bundle exec rake test""" |
62 | 95 | }
|
| 96 | + |
63 | 97 | check.dependsOn testWebsite
|
64 | 98 |
|
65 | 99 | task preCommit {
|
|
0 commit comments