Imported Upstream version 2.67.1
[platform/upstream/glib.git] / .gitlab-ci / run-docker.sh
1 #!/bin/bash
2
3 read_arg() {
4     # $1 = arg name
5     # $2 = arg value
6     # $3 = arg parameter
7     local rematch='^[^=]*=(.*)$'
8     if [[ $2 =~ $rematch ]]; then
9         read -r "$1" <<< "${BASH_REMATCH[1]}"
10     else
11         read -r "$1" <<< "$3"
12         # There is no way to shift our callers args, so
13         # return 1 to indicate they should do it instead.
14         return 1
15     fi
16 }
17
18 SUDO_CMD="sudo"
19 if docker -v |& grep -q podman; then
20         # Using podman
21         SUDO_CMD=""
22         # Docker is actually implemented by podman, and its OCI output
23         # is incompatible with some of the dockerd instances on GitLab
24         # CI runners.
25         export BUILDAH_FORMAT=docker
26 fi
27
28 set -e
29
30 base=""
31 base_version=""
32 build=0
33 run=0
34 push=0
35 list=0
36 print_help=0
37 no_login=0
38
39 while (($# > 0)); do
40         case "${1%%=*}" in
41                 build) build=1;;
42                 run) run=1;;
43                 push) push=1;;
44                 list) list=1;;
45                 help) print_help=1;;
46                 --base|-b) read_arg base "$@" || shift;;
47                 --base-version) read_arg base_version "$@" || shift;;
48                 --no-login) no_login=1;;
49                 *) echo -e "\\e[1;31mERROR\\e[0m: Unknown option '$1'"; exit 1;;
50         esac
51         shift
52 done
53
54 if [ $print_help == 1 ]; then
55         echo "$0 - Build and run Docker images"
56         echo ""
57         echo "Usage: $0 <command> [options] [basename]"
58         echo ""
59         echo "Available commands"
60         echo ""
61         echo "  build --base=<BASENAME> - Build Docker image <BASENAME>.Dockerfile"
62         echo "  run --base=<BASENAME>   - Run Docker image <BASENAME>"
63         echo "  push --base=<BASENAME>  - Push Docker image <BASENAME> to the registry"
64         echo "  list                    - List available images"
65         echo "  help                    - This help message"
66         echo ""
67         exit 0
68 fi
69
70 cd "$(dirname "$0")"
71
72 if [ $list == 1 ]; then
73         echo "Available Docker images:"
74         for f in *.Dockerfile; do
75                 filename=$( basename -- "$f" )
76                 basename="${filename%.*}"
77
78                 echo -e "  \\e[1;39m$basename\\e[0m"
79         done
80         exit 0
81 fi
82
83 # All commands after this require --base to be set
84 if [ -z "${base}" ]; then
85         echo "Usage: $0 <command>"
86         exit 1
87 fi
88
89 if [ ! -f "$base.Dockerfile" ]; then
90         echo -e "\\e[1;31mERROR\\e[0m: Dockerfile for '$base' not found"
91         exit 1
92 fi
93
94 if [ -z "${base_version}" ]; then
95         base_version="latest"
96 else
97         base_version="v$base_version"
98 fi
99
100 TAG="registry.gitlab.gnome.org/gnome/glib/${base}:${base_version}"
101
102 if [ $build == 1 ]; then
103         echo -e "\\e[1;32mBUILDING\\e[0m: ${base} as ${TAG}"
104         $SUDO_CMD docker build \
105                 --build-arg HOST_USER_ID="$UID" \
106                 --build-arg COVERITY_SCAN_PROJECT_NAME="${COVERITY_SCAN_PROJECT_NAME}" \
107                 --build-arg COVERITY_SCAN_TOKEN="${COVERITY_SCAN_TOKEN}" \
108                 --tag "${TAG}" \
109                 --file "${base}.Dockerfile" .
110         exit $?
111 fi
112
113 if [ $push == 1 ]; then
114         echo -e "\\e[1;32mPUSHING\\e[0m: ${base} as ${TAG}"
115
116         if [ $no_login == 0 ]; then
117                 $SUDO_CMD docker login registry.gitlab.gnome.org
118         fi
119
120         $SUDO_CMD docker push $TAG
121         exit $?
122 fi
123
124 if [ $run == 1 ]; then
125         echo -e "\\e[1;32mRUNNING\\e[0m: ${base} as ${TAG}"
126         $SUDO_CMD docker run \
127                 --rm \
128                 --volume "$(pwd)/..:/home/user/app" \
129                 --workdir "/home/user/app" \
130                 --tty \
131                 --interactive "${TAG}" \
132                 bash
133         exit $?
134 fi