From a21fded449a4de33daaa1278f328ae58e088e6a8 Mon Sep 17 00:00:00 2001 From: Evangelos Ribeiro Tzaras Date: Tue, 21 Dec 2021 12:27:10 +0100 Subject: [PATCH] gitlab-ci: Use prebuilt containers This image has all build dependencies to speed up build and tests. Copied from phosh (https://gitlab.gnome.org/World/Phosh/phosh/-/commit/d8b2e5651b837adb6698c9868a9d278ef7929400) with minor modifications for Calls. Use .gitlab-ci/run-docker.sh --base debian build --version 0.0. .gitlab-ci/run-docker.sh --base debian push --version 0.0. to update. --- .gitlab-ci/README.md | 12 +++ .gitlab-ci/debian.Dockerfile | 10 +++ .gitlab-ci/run-docker.sh | 137 +++++++++++++++++++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 .gitlab-ci/README.md create mode 100644 .gitlab-ci/debian.Dockerfile create mode 100755 .gitlab-ci/run-docker.sh diff --git a/.gitlab-ci/README.md b/.gitlab-ci/README.md new file mode 100644 index 0000000..df64a45 --- /dev/null +++ b/.gitlab-ci/README.md @@ -0,0 +1,12 @@ +### Checklist for Updating the Docker Images + + - [ ] Update the `${image}.Dockerfile` file with the dependencies + - [ ] Run `./run-docker.sh build --base ${image} --version ${number}` + - [ ] Run `./run-docker.sh push --base ${image} --version ${number}` + once the Docker image is built; you may need to log in by using + `docker login` or `podman login` like + podman login -u -p registry.gitlab.gnome.org/world/phosh/phosh + See https://docs.gitlab.com/ee/user/packages/container_registry/ + - [ ] Update the `image` keys in the `.gitlab-ci.yml` file with the new + image tag + - [ ] Open a merge request with your changes and let it run diff --git a/.gitlab-ci/debian.Dockerfile b/.gitlab-ci/debian.Dockerfile new file mode 100644 index 0000000..c759869 --- /dev/null +++ b/.gitlab-ci/debian.Dockerfile @@ -0,0 +1,10 @@ +FROM debian:bookworm-slim + +RUN export DEBIAN_FRONTEND=noninteractive \ + && apt-get -y update \ + && apt-get -y install --no-install-recommends wget ca-certificates gnupg eatmydata \ + && eatmydata apt-get -y update \ + && cd /home/user/app \ + && eatmydata apt-get --no-install-recommends -y build-dep . \ + && eatmydata apt-get --no-install-recommends -y install build-essential git wget gcovr \ + && eatmydata apt-get clean diff --git a/.gitlab-ci/run-docker.sh b/.gitlab-ci/run-docker.sh new file mode 100755 index 0000000..4c32429 --- /dev/null +++ b/.gitlab-ci/run-docker.sh @@ -0,0 +1,137 @@ +#!/bin/bash + +read_arg() { + # $1 = arg name + # $2 = arg value + # $3 = arg parameter + local rematch='^[^=]*=(.*)$' + if [[ $2 =~ $rematch ]]; then + read "$1" <<< "${BASH_REMATCH[1]}" + else + read "$1" <<< "$3" + # There is no way to shift our callers args, so + # return 1 to indicate they should do it instead. + return 1 + fi +} + +set -e + +build=0 +run=0 +push=0 +list=0 +print_help=0 +no_login=0 + +while (($# > 0)); do + case "${1%%=*}" in + build) build=1;; + run) run=1;; + push) push=1;; + list) list=1;; + help) print_help=1;; + --base|-b) read_arg base "$@" || shift;; + --version|-v) read_arg base_version "$@" || shift;; + --no-login) no_login=1;; + *) echo -e "\e[1;31mERROR\e[0m: Unknown option '$1'"; exit 1;; + esac + shift +done + +if [ $print_help == 1 ]; then + echo "$0 - Build and run Docker images" + echo "" + echo "Usage: $0 [options] [basename]" + echo "" + echo "Available commands" + echo "" + echo " build --base= - Build Docker image .Dockerfile" + echo " run --base= - Run Docker image " + echo " push --base= - Push Docker image to the registry" + echo " list - List available images" + echo " help - This help message" + echo "" + exit 0 +fi + +cd "$(dirname "$0")" + +if [ $list == 1 ]; then + echo "Available Docker images:" + for f in *.Dockerfile; do + filename=$( basename -- "$f" ) + basename="${filename%.*}" + + echo -e " \e[1;39m$basename\e[0m" + done + exit 0 +fi + +# All commands after this require --base to be set +if [ -z $base ]; then + echo "Usage: $0 " + exit 1 +fi + +if [ ! -f "$base.Dockerfile" ]; then + echo -e "\e[1;31mERROR\e[0m: Dockerfile for '$base' not found" + exit 1 +fi + +if [ -z $base_version ]; then + base_version="latest" +elif [ $base_version != "latest" ]; then + base_version="v$base_version" +fi + +if [ ! -x "$(command -v docker)" ] || docker --help |& grep -q podman; then + # Docker is actually implemented by podman, and its OCI output + # is incompatible with some of the dockerd instances on GitLab + # CI runners. + echo "Using: Podman" + format="--format docker" + CMD="podman" +else + echo "Using: Docker" + format="" + CMD="sudo docker" +fi + +REGISTRY="registry.gitlab.gnome.org" +REPO="gnome/calls" +TAG="${REGISTRY}/${REPO}/${base}:${base_version}" + +if [ $build == 1 ]; then + echo -e "\e[1;32mBUILDING\e[0m: ${base} as ${TAG}" + ${CMD} build \ + ${format} \ + --volume "$(pwd)/..:/home/user/app" \ + --build-arg HOST_USER_ID="$UID" \ + --tag "${TAG}" \ + --file "${base}.Dockerfile" . + exit $? +fi + +if [ $push == 1 ]; then + echo -e "\e[1;32mPUSHING\e[0m: ${base} as ${TAG}" + + if [ $no_login == 0 ]; then + ${CMD} login ${REGISTRY} + fi + + ${CMD} push ${TAG} + exit $? +fi + +if [ $run == 1 ]; then + echo -e "\e[1;32mRUNNING\e[0m: ${base} as ${TAG}" + ${CMD} run \ + --rm \ + --volume "$(pwd)/..:/home/user/app" \ + --workdir "/home/user/app" \ + --tty \ + --interactive "${TAG}" \ + bash + exit $? +fi