From 79501a4af73c9a221851f9d0d359f198de146967 Mon Sep 17 00:00:00 2001 From: ebbit1q Date: Tue, 19 Apr 2022 01:04:49 +0200 Subject: [PATCH] fix the uid and gid of the user in the docker container (#4610) * fix the uid and gid of the user in the container this fixes this error: unsafe repository ('/src' is owned by someone else) this caused the hash to go missing in the version number * add --interactive option to .ci/docker.sh * add --dir to .ci/compile.sh * fix up the comments on the ci scripts * add extra comment to docker.sh --- .ci/compile.sh | 31 ++++++++++++++++++++++++++++--- .ci/docker.sh | 27 ++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/.ci/compile.sh b/.ci/compile.sh index 73337210..72c73781 100755 --- a/.ci/compile.sh +++ b/.ci/compile.sh @@ -1,6 +1,19 @@ #!/bin/bash # This script is to be used by the ci environment from the project root directory, do not use it from somewhere else. + +# Compiles cockatrice inside of a ci environment +# --format runs the clang-format script first +# --install runs make install +# --package [] runs make package, optionally force the type +# --suffix renames package with this suffix, requires arg +# --server compiles servatrice +# --test runs tests +# --debug or --release or sets the build type ie CMAKE_BUILD_TYPE +# --ccache uses ccache and shows stats +# --dir sets the name of the build dir, default is "build" +# uses env: BUILDTYPE CHECK_FORMAT MAKE_INSTALL MAKE_PACKAGE PACKAGE_TYPE PACKAGE_SUFFIX MAKE_SERVER MAKE_TEST USE_CCACHE BUILD_DIR (correspond to args: /--debug/--release --format --install --package --suffix --server --test --ccache --dir ) +# exitcode: 1 for failure, 3 for invalid arguments LINT_SCRIPT=".ci/lint_cpp.sh" # Read arguments @@ -29,7 +42,7 @@ while [[ $# != 0 ]]; do shift if [[ $# == 0 ]]; then echo "::error file=$0::--suffix expects an argument" - exit 1 + exit 3 fi PACKAGE_SUFFIX="$1" shift @@ -54,6 +67,15 @@ while [[ $# != 0 ]]; do USE_CCACHE=1 shift ;; + '--dir') + shift + if [[ $# == 0 ]]; then + echo "::error file=$0::--dir expects an argument" + exit 3 + fi + BUILD_DIR="$1" + shift + ;; *) if [[ $1 == -* ]]; then echo "::error file=$0::unrecognized option: $1" @@ -76,8 +98,11 @@ set -e # Setup ./servatrice/check_schema_version.sh -mkdir -p build -cd build +if [[ ! $BUILD_DIR ]]; then + BUILD_DIR="build" +fi +mkdir -p "$BUILD_DIR" +cd "$BUILD_DIR" if [[ ! $CMAKE_BUILD_PARALLEL_LEVEL ]]; then CMAKE_BUILD_PARALLEL_LEVEL=2 # default machines have 2 cores diff --git a/.ci/docker.sh b/.ci/docker.sh index 1e2b0a95..12052309 100644 --- a/.ci/docker.sh +++ b/.ci/docker.sh @@ -3,11 +3,14 @@ # This script is to be used by the ci environment from the project root directory, do not use it from somewhere else. # Creates or loads docker images to use in compilation, creates RUN function to start compilation on the docker image. +# sets the name of the docker image, these correspond to directories in .ci # --get loads the image from a previously saved image cache, will build if no image is found # --build builds the image from the Dockerfile in .ci/$NAME # --save stores the image, if an image was loaded it will not be stored +# --interactive immediately starts the image interactively for debugging +# --set-cache sets the location to cache the image or for ccache # requires: docker -# uses env: NAME CACHE BUILD GET SAVE (correspond to args: --set-cache --build --get --save) +# uses env: NAME CACHE BUILD GET SAVE INTERACTIVE (correspond to args: --set-cache --build --get --save --interactive) # sets env: RUN CCACHE_DIR IMAGE_NAME RUN_ARGS RUN_OPTS BUILD_SCRIPT # exitcode: 1 for failure, 2 for missing dockerfile, 3 for invalid arguments export BUILD_SCRIPT=".ci/compile.sh" @@ -28,6 +31,10 @@ while [[ $# != 0 ]]; do GET=1 shift ;; + '--interactive') + INTERACTIVE=1 + shift + ;; '--save') SAVE=1 shift @@ -36,7 +43,7 @@ while [[ $# != 0 ]]; do CACHE=$2 if ! [[ -d $CACHE ]]; then echo "could not find cache path: $CACHE" >&2 - exit 3 + return 3 fi shift 2 ;; @@ -88,7 +95,6 @@ else fi fi - # Get the docker image from previously stored save if [[ $GET ]]; then if [[ $img_save ]] && docker load --input "$img_save"; then @@ -133,9 +139,12 @@ function RUN () { echo "running image:" if docker images | grep "$IMAGE_NAME"; then - args=(--mount "type=bind,source=$PWD,target=/src" -w="/src") + local args=(--mount "type=bind,source=$PWD,target=/src") + args+=(--workdir "/src") + args+=(--user "$(id -u):$(id -g)") if [[ $CCACHE_DIR ]]; then - args+=(--mount "type=bind,source=$CCACHE_DIR,target=/.ccache" -e "CCACHE_DIR=/.ccache") + args+=(--mount "type=bind,source=$CCACHE_DIR,target=/.ccache") + args+=(--env "CCACHE_DIR=/.ccache") fi docker run "${args[@]}" $RUN_ARGS "$IMAGE_NAME" bash "$BUILD_SCRIPT" $RUN_OPTS "$@" return $? @@ -144,3 +153,11 @@ function RUN () return 3 fi } + +# for debugging, start the docker image interactively instead of building +# starts immediately, does not require sourcing or RUN +if [[ $INTERACTIVE ]]; then + export BUILD_SCRIPT="-i" + export RUN_ARGS="$RUN_ARGS -it" + RUN +fi