James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Licensed to the Apache Software Foundation (ASF) under one |
| 4 | # or more contributor license agreements. See the NOTICE file |
| 5 | # distributed with this work for additional information |
| 6 | # regarding copyright ownership. The ASF licenses this file |
| 7 | # to you under the Apache License, Version 2.0 (the |
| 8 | # "License"); you may not use this file except in compliance |
| 9 | # with the License. You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, |
| 14 | # software distributed under the License is distributed on an |
| 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | # KIND, either express or implied. See the License for the |
| 17 | # specific language governing permissions and limitations |
| 18 | # under the License. |
| 19 | # |
| 20 | |
| 21 | # |
| 22 | # The build has two stages: "docker" and "test" |
| 23 | # The "docker" stage is meant to rebuild the docker images |
| 24 | # if needed. If we cannot push that result however then |
| 25 | # there is no reason to do anything. |
| 26 | # The "test" stage is an actual test job. Even if the docker |
| 27 | # image doesn't match what's in the repo, we still build |
| 28 | # the image so the build job can run properly. |
| 29 | # |
| 30 | |
| 31 | set -e |
| 32 | |
| 33 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
James E. King, III | cd5be7b | 2017-10-21 10:16:19 -0400 | [diff] [blame] | 34 | DOCKER_TAG=$DOCKER_REPO:$DISTRO |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 35 | |
| 36 | function dockerfile_changed { |
| 37 | # image may not exist yet, so we have to let it fail silently: |
| 38 | docker pull $DOCKER_TAG || true |
| 39 | docker run $DOCKER_TAG bash -c 'cd .. && sha512sum Dockerfile' > .Dockerfile.sha512 |
| 40 | sha512sum -c .Dockerfile.sha512 |
| 41 | } |
| 42 | |
| 43 | # |
| 44 | # If this build has no DOCKER_PASS and it is in the docker stage |
| 45 | # then there's no reason to do any processing because we cannot |
Jiayu Liu | 9042cc2 | 2022-04-22 11:32:08 +0800 | [diff] [blame^] | 46 | # push the result if the Dockerfile changed. |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 47 | # |
| 48 | |
| 49 | if [[ "$TRAVIS_BUILD_STAGE" == "docker" ]] && [[ -z "$DOCKER_PASS" ]]; then |
| 50 | echo Detected docker stage build and no defined DOCKER_PASS, this build job will be skipped. |
| 51 | echo Subsequent jobs in the test stage may each rebuild the docker image. |
| 52 | exit 0 |
| 53 | fi |
| 54 | |
| 55 | |
| 56 | pushd ${SCRIPT_DIR}/$DISTRO |
| 57 | if dockerfile_changed; then |
| 58 | echo Dockerfile has not changed. No need to rebuild. |
| 59 | exit 0 |
| 60 | else |
| 61 | echo Dockerfile has changed. |
| 62 | fi |
| 63 | popd |
| 64 | |
| 65 | # |
James E. King, III | cd5be7b | 2017-10-21 10:16:19 -0400 | [diff] [blame] | 66 | # Dockerfile has changed - rebuild it for the current build job. |
| 67 | # If it is a "docker" stage build then we want to push it back |
| 68 | # to the DOCKER_REPO. If it is a "test" stage build then we do |
| 69 | # not. If nobody defined a DOCKER_PASS then it doesn't matter. |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 70 | # |
| 71 | |
| 72 | echo Rebuilding docker image $DISTRO |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 73 | |
Jiayu Liu | 9042cc2 | 2022-04-22 11:32:08 +0800 | [diff] [blame^] | 74 | # https://docs.travis-ci.com/user/common-build-problems/#build-times-out-because-no-output-was-received |
| 75 | # adding `travis_wait` because kerl building in the docker file takes >10 min for building erlang |
| 76 | # without printing to stdout, resulting in build failures |
| 77 | travis_wait 45 docker build --tag $DOCKER_TAG build/docker/$DISTRO |
| 78 | |
| 79 | if [[ "$TRAVIS_BUILD_STAGE" == "docker" ]] && [[ ! -z "$DOCKER_USER" ]] && [[ ! -z "$DOCKER_PASS" ]]; then |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 80 | echo Pushing docker image $DOCKER_TAG |
| 81 | docker login -u $DOCKER_USER -p $DOCKER_PASS |
| 82 | docker push $DOCKER_TAG |
James E. King, III | cd5be7b | 2017-10-21 10:16:19 -0400 | [diff] [blame] | 83 | else |
| 84 | echo Not pushing docker image: either not a docker stage build job, or one of DOCKER_USER or DOCKER_PASS is undefined. |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 85 | fi |
| 86 | |