blob: 08cbc91e1151ce60a5c14684cb1822efc40a4e33 [file] [log] [blame]
James E. King, III0ad20bd2017-09-30 15:44:16 -07001#!/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
31set -e
32
33SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
James E. King, IIIcd5be7b2017-10-21 10:16:19 -040034DOCKER_TAG=$DOCKER_REPO:$DISTRO
James E. King, III0ad20bd2017-09-30 15:44:16 -070035
36function 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 Liu40496a72022-04-22 11:37:03 +080046# push the result if the Dockerfile changed.
James E. King, III0ad20bd2017-09-30 15:44:16 -070047#
48
49if [[ "$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
53fi
54
55
56pushd ${SCRIPT_DIR}/$DISTRO
57if dockerfile_changed; then
58 echo Dockerfile has not changed. No need to rebuild.
59 exit 0
60else
61 echo Dockerfile has changed.
62fi
63popd
64
65#
James E. King, IIIcd5be7b2017-10-21 10:16:19 -040066# 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, III0ad20bd2017-09-30 15:44:16 -070070#
71
72echo Rebuilding docker image $DISTRO
Jiayu Liu40496a72022-04-22 11:37:03 +080073docker build --tag $DOCKER_TAG build/docker/$DISTRO
James E. King, III0ad20bd2017-09-30 15:44:16 -070074
Jiayu Liu40496a72022-04-22 11:37:03 +080075if [[ "$TRAVIS_BUILD_STAGE" == "docker" ]] && [[ ! -z "$DOCKER_USER" ]] && [[ ! -z "$DOCKER_PASS" ]]; then
James E. King, III0ad20bd2017-09-30 15:44:16 -070076 echo Pushing docker image $DOCKER_TAG
77 docker login -u $DOCKER_USER -p $DOCKER_PASS
78 docker push $DOCKER_TAG
James E. King, IIIcd5be7b2017-10-21 10:16:19 -040079else
80 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, III0ad20bd2017-09-30 15:44:16 -070081fi
82