Skip to content

Commit 47a978c

Browse files
committed
Add scripts for rebuilding/pushing docker images
Taken from: pmem/dev-utils-kit@30794c3
1 parent 2561f45 commit 47a978c

File tree

5 files changed

+418
-0
lines changed

5 files changed

+418
-0
lines changed

docker/build.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
# Copyright 2022, Intel Corporation
4+
5+
#
6+
# build.sh - runs a Docker container from a Docker image with environment
7+
# prepared for running CacheLib builds and tests. It uses Docker image
8+
# tagged as described in ./images/build-image.sh.
9+
#
10+
# Notes:
11+
# - set env var 'HOST_WORKDIR' to where the root of this project is on the host machine,
12+
# - set env var 'OS' and 'OS_VER' properly to a system/Docker you want to build this
13+
# repo on (for proper values take a look at the list of Dockerfiles at the
14+
# utils/docker/images directory in this repo), e.g. OS=ubuntu, OS_VER=20.04,
15+
# - set env var 'CONTAINER_REG' to container registry address
16+
# [and possibly user/org name, and package name], e.g. "<CR_addr>/pmem/CacheLib",
17+
# - set env var 'DNS_SERVER' if you use one,
18+
# - set env var 'COMMAND' to execute specific command within Docker container or
19+
# env var 'TYPE' to pick command based on one of the predefined types of build (see below).
20+
#
21+
22+
set -e
23+
24+
source $(dirname ${0})/set-ci-vars.sh
25+
IMG_VER=${IMG_VER:-devel}
26+
TAG="${OS}-${OS_VER}-${IMG_VER}"
27+
IMAGE_NAME=${CONTAINER_REG}:${TAG}
28+
CONTAINER_NAME=CacheLib-${OS}-${OS_VER}
29+
WORKDIR=/CacheLib # working dir within Docker container
30+
SCRIPTSDIR=${WORKDIR}/utils/docker
31+
32+
if [[ -z "${OS}" || -z "${OS_VER}" ]]; then
33+
echo "ERROR: The variables OS and OS_VER have to be set " \
34+
"(e.g. OS=fedora, OS_VER=32)."
35+
exit 1
36+
fi
37+
38+
if [[ -z "${HOST_WORKDIR}" ]]; then
39+
echo "ERROR: The variable HOST_WORKDIR has to contain a path to " \
40+
"the root of this project on the host machine."
41+
exit 1
42+
fi
43+
44+
if [[ -z "${CONTAINER_REG}" ]]; then
45+
echo "ERROR: CONTAINER_REG environment variable is not set " \
46+
"(e.g. \"<registry_addr>/<org_name>/<package_name>\")."
47+
exit 1
48+
fi
49+
50+
# Set command to execute in the Docker container
51+
COMMAND="./run-build.sh";
52+
echo "COMMAND to execute within Docker container: ${COMMAND}"
53+
54+
if [ -n "${DNS_SERVER}" ]; then DOCKER_OPTS="${DOCKER_OPTS} --dns=${DNS_SERVER}"; fi
55+
56+
# Check if we are running on a CI (Travis or GitHub Actions)
57+
[ -n "${GITHUB_ACTIONS}" -o -n "${TRAVIS}" ] && CI_RUN="YES" || CI_RUN="NO"
58+
59+
# Do not allocate a pseudo-TTY if we are running on GitHub Actions
60+
[ ! "${GITHUB_ACTIONS}" ] && DOCKER_OPTS="${DOCKER_OPTS} --tty=true"
61+
62+
63+
echo "Running build using Docker image: ${IMAGE_NAME}"
64+
65+
# Run a container with
66+
# - environment variables set (--env)
67+
# - host directory containing source mounted (-v)
68+
# - working directory set (-w)
69+
docker run --privileged=true --name=${CONTAINER_NAME} -i \
70+
${DOCKER_OPTS} \
71+
--env http_proxy=${http_proxy} \
72+
--env https_proxy=${https_proxy} \
73+
--env TERM=xterm-256color \
74+
--env WORKDIR=${WORKDIR} \
75+
--env SCRIPTSDIR=${SCRIPTSDIR} \
76+
--env GITHUB_REPO=${GITHUB_REPO} \
77+
--env CI_RUN=${CI_RUN} \
78+
--env TRAVIS=${TRAVIS} \
79+
--env GITHUB_ACTIONS=${GITHUB_ACTIONS} \
80+
--env CI_COMMIT=${CI_COMMIT} \
81+
--env CI_COMMIT_RANGE=${CI_COMMIT_RANGE} \
82+
--env CI_BRANCH=${CI_BRANCH} \
83+
--env CI_EVENT_TYPE=${CI_EVENT_TYPE} \
84+
--env CI_REPO_SLUG=${CI_REPO_SLUG} \
85+
--env DOC_UPDATE_GITHUB_TOKEN=${DOC_UPDATE_GITHUB_TOKEN} \
86+
--env DOC_UPDATE_BOT_NAME=${DOC_UPDATE_BOT_NAME} \
87+
--env DOC_REPO_OWNER=${DOC_REPO_OWNER} \
88+
--env COVERITY_SCAN_TOKEN=${COVERITY_SCAN_TOKEN} \
89+
--env COVERITY_SCAN_NOTIFICATION_EMAIL=${COVERITY_SCAN_NOTIFICATION_EMAIL} \
90+
--env TEST_TIMEOUT=${TEST_TIMEOUT} \
91+
--env TZ='Europe/Warsaw' \
92+
--shm-size=4G \
93+
-v ${HOST_WORKDIR}:${WORKDIR} \
94+
-v /etc/localtime:/etc/localtime \
95+
-w ${SCRIPTSDIR} \
96+
${IMAGE_NAME} ${COMMAND}

docker/images/build-image.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
# Copyright 2016-2021, Intel Corporation
4+
#
5+
# build-image.sh - prepares a Docker image with <OS>-based environment for
6+
# testing (or dev) purpose, tagged with ${CONTAINER_REG}:${OS}-${OS_VER}-${IMG_VER},
7+
# according to the ${OS}-${OS_VER}.Dockerfile file located in the same directory.
8+
# IMG_VER is a version of Docker image (it usually relates to project's release tag)
9+
# and it defaults to "devel".
10+
#
11+
12+
set -e
13+
IMG_VER=${IMG_VER:-devel}
14+
TAG="${OS}-${OS_VER}-${IMG_VER}"
15+
16+
if [[ -z "${OS}" || -z "${OS_VER}" ]]; then
17+
echo "ERROR: The variables OS and OS_VER have to be set " \
18+
"(e.g. OS=fedora, OS_VER=34)."
19+
exit 1
20+
fi
21+
22+
if [[ -z "${CONTAINER_REG}" ]]; then
23+
echo "ERROR: CONTAINER_REG environment variable is not set " \
24+
"(e.g. \"<registry_addr>/<org_name>/<package_name>\")."
25+
exit 1
26+
fi
27+
28+
echo "Check if the file ${OS}-${OS_VER}.Dockerfile exists"
29+
if [[ ! -f "${OS}-${OS_VER}.Dockerfile" ]]; then
30+
echo "Error: ${OS}-${OS_VER}.Dockerfile does not exist."
31+
exit 1
32+
fi
33+
34+
echo "Build a Docker image tagged with: ${CONTAINER_REG}:${TAG}"
35+
docker build -t ${CONTAINER_REG}:${TAG} \
36+
--build-arg http_proxy=$http_proxy \
37+
--build-arg https_proxy=$https_proxy \
38+
-f ${OS}-${OS_VER}.Dockerfile .

docker/images/push-image.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
# Copyright 2016-2021, Intel Corporation
4+
5+
#
6+
# push-image.sh - pushes the Docker image tagged as described in
7+
# ./build-image.sh, to the ${CONTAINER_REG}.
8+
#
9+
# The script utilizes ${CONTAINER_REG_USER} and ${CONTAINER_REG_PASS} variables to
10+
# log in to the ${CONTAINER_REG}. The variables can be set in the CI's configuration
11+
# for automated builds.
12+
#
13+
14+
set -e
15+
IMG_VER=${IMG_VER:-devel}
16+
TAG="${OS}-${OS_VER}-${IMG_VER}"
17+
18+
if [[ -z "${OS}" || -z "${OS_VER}" ]]; then
19+
echo "ERROR: The variables OS and OS_VER have to be set " \
20+
"(e.g. OS=fedora, OS_VER=34)."
21+
exit 1
22+
fi
23+
24+
if [[ -z "${CONTAINER_REG}" ]]; then
25+
echo "ERROR: CONTAINER_REG environment variable is not set " \
26+
"(e.g. \"<registry_addr>/<org_name>/<package_name>\")."
27+
exit 1
28+
fi
29+
30+
if [[ -z "${CONTAINER_REG_USER}" || -z "${CONTAINER_REG_PASS}" ]]; then
31+
echo "ERROR: variables CONTAINER_REG_USER=\"${CONTAINER_REG_USER}\" and " \
32+
"CONTAINER_REG_PASS=\"${CONTAINER_REG_PASS}\"" \
33+
"have to be set properly to allow login to the Container Registry."
34+
exit 1
35+
fi
36+
37+
# Check if the image tagged with ${CONTAINER_REG}:${TAG} exists locally
38+
if [[ ! $(docker images -a | awk -v pattern="^${CONTAINER_REG}:${TAG}\$" \
39+
'$1":"$2 ~ pattern') ]]
40+
then
41+
echo "ERROR: Docker image tagged ${CONTAINER_REG}:${TAG} does not exist locally."
42+
exit 1
43+
fi
44+
45+
echo "Log in to the Container Registry: ${CONTAINER_REG}"
46+
echo "${CONTAINER_REG_PASS}" | docker login ghcr.io -u="${CONTAINER_REG_USER}" --password-stdin
47+
48+
echo "Push the image to the Container Registry"
49+
docker push ${CONTAINER_REG}:${TAG}

docker/pull-or-rebuild-image.sh

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
# Copyright 2016-2021, Intel Corporation
4+
5+
#
6+
# pull-or-rebuild-image.sh - rebuilds the Docker image used in the
7+
# current build (if necessary) or pulls it from the Container Registry.
8+
# Docker image is tagged as described in docker/build-image.sh,
9+
# but IMG_VER defaults in this script to "latest" (just in case it's
10+
# used locally without building any images).
11+
#
12+
# If Docker was rebuilt and all requirements are fulfilled (more details in
13+
# push_image function below) image will be pushed to the ${CONTAINER_REG}.
14+
#
15+
# The script rebuilds the Docker image if:
16+
# 1. the Dockerfile for the current OS version (${OS}-${OS_VER}.Dockerfile)
17+
# or any .sh script in the Dockerfiles directory were modified and committed, or
18+
# 2. "rebuild" param was passed as a first argument to this script.
19+
#
20+
# The script pulls the Docker image if:
21+
# 1. it does not have to be rebuilt (based on committed changes), or
22+
# 2. "pull" param was passed as a first argument to this script.
23+
#
24+
25+
set -e
26+
27+
source $(dirname ${0})/set-ci-vars.sh
28+
IMG_VER=${IMG_VER:-latest}
29+
TAG="${OS}-${OS_VER}-${IMG_VER}"
30+
IMAGES_DIR_NAME=images
31+
BASE_DIR=docker/${IMAGES_DIR_NAME}
32+
33+
if [[ -z "${OS}" || -z "${OS_VER}" ]]; then
34+
echo "ERROR: The variables OS and OS_VER have to be set properly " \
35+
"(eg. OS=fedora, OS_VER=34)."
36+
exit 1
37+
fi
38+
39+
if [[ -z "${CONTAINER_REG}" ]]; then
40+
echo "ERROR: CONTAINER_REG environment variable is not set " \
41+
"(e.g. \"<registry_addr>/<org_name>/<package_name>\")."
42+
exit 1
43+
fi
44+
45+
function build_image() {
46+
echo "Building the Docker image for the ${OS}-${OS_VER}.Dockerfile"
47+
pushd ${IMAGES_DIR_NAME}
48+
./build-image.sh
49+
popd
50+
}
51+
52+
function pull_image() {
53+
echo "Pull the image '${CONTAINER_REG}:${TAG}' from the Container Registry."
54+
docker pull ${CONTAINER_REG}:${TAG}
55+
}
56+
57+
function push_image {
58+
# Check if the image has to be pushed to the Container Registry:
59+
# - only upstream (not forked) repository,
60+
# - stable-* or master branch,
61+
# - not a pull_request event,
62+
# - and PUSH_IMAGE flag was set for current build.
63+
if [[ "${CI_REPO_SLUG}" == "${GITHUB_REPO}" \
64+
&& (${CI_BRANCH} == stable-* || ${CI_BRANCH} == master) \
65+
&& ${CI_EVENT_TYPE} != "pull_request" \
66+
&& ${PUSH_IMAGE} == "1" ]]
67+
then
68+
echo "The image will be pushed to the Container Registry: ${CONTAINER_REG}"
69+
pushd ${IMAGES_DIR_NAME}
70+
./push-image.sh
71+
popd
72+
else
73+
echo "Skip pushing the image to the Container Registry."
74+
fi
75+
}
76+
77+
# If "rebuild" or "pull" are passed to the script as param, force rebuild/pull.
78+
if [[ "${1}" == "rebuild" ]]; then
79+
build_image
80+
push_image
81+
exit 0
82+
elif [[ "${1}" == "pull" ]]; then
83+
pull_image
84+
exit 0
85+
fi
86+
87+
# Determine if we need to rebuild the image or just pull it from
88+
# the Container Registry, based on committed changes.
89+
if [ -n "${CI_COMMIT_RANGE}" ]; then
90+
commits=$(git rev-list ${CI_COMMIT_RANGE})
91+
else
92+
commits=${CI_COMMIT}
93+
fi
94+
95+
if [[ -z "${commits}" ]]; then
96+
echo "'commits' variable is empty. Docker image will be pulled."
97+
fi
98+
99+
echo "Commits in the commit range:"
100+
for commit in ${commits}; do echo ${commit}; done
101+
102+
echo "Files modified within the commit range:"
103+
files=$(for commit in ${commits}; do git diff-tree --no-commit-id --name-only \
104+
-r ${commit}; done | sort -u)
105+
for file in ${files}; do echo ${file}; done
106+
107+
# Check if committed file modifications require the Docker image to be rebuilt
108+
for file in ${files}; do
109+
# Check if modified files are relevant to the current build
110+
if [[ ${file} =~ ^(${BASE_DIR})\/(${OS})-(${OS_VER})\.Dockerfile$ ]] \
111+
|| [[ ${file} =~ ^(${BASE_DIR})\/.*\.sh$ ]]
112+
then
113+
build_image
114+
push_image
115+
exit 0
116+
fi
117+
done
118+
119+
# Getting here means rebuilding the Docker image isn't required (based on changed files).
120+
# Pull the image from the Container Registry or rebuild anyway, if pull fails.
121+
if ! pull_image; then
122+
build_image
123+
push_image
124+
fi

0 commit comments

Comments
 (0)