Merge "Fix crashes in various GParamSpec creation functions" into tizen
[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 if type -p podman; then
19         # Using podman
20         DOCKER_CMD="podman"
21         # Docker is actually implemented by podman, and its OCI output
22         # is incompatible with some of the dockerd instances on GitLab
23         # CI runners.
24         export BUILDAH_FORMAT=docker
25 elif getent group docker | grep -q "\b${USER}\b"; then
26         DOCKER_CMD="docker"
27 else
28         DOCKER_CMD="sudo docker"
29 fi
30
31 set -e
32
33 base=""
34 base_version=""
35 build=0
36 run=0
37 push=0
38 list=0
39 print_help=0
40 no_login=0
41
42 while (($# > 0)); do
43         case "${1%%=*}" in
44                 build) build=1;;
45                 run) run=1;;
46                 push) push=1;;
47                 list) list=1;;
48                 help) print_help=1;;
49                 --base|-b) read_arg base "$@" || shift;;
50                 --base-version) read_arg base_version "$@" || shift;;
51                 --no-login) no_login=1;;
52                 *) echo -e "\\e[1;31mERROR\\e[0m: Unknown option '$1'"; exit 1;;
53         esac
54         shift
55 done
56
57 if [ $print_help == 1 ]; then
58         echo "$0 - Build and run Docker images"
59         echo ""
60         echo "Usage: $0 <command> [options] [basename]"
61         echo ""
62         echo "Available commands"
63         echo ""
64         echo "  build --base=<BASENAME> - Build Docker image <BASENAME>.Dockerfile"
65         echo "  run --base=<BASENAME>   - Run Docker image <BASENAME>"
66         echo "  push --base=<BASENAME>  - Push Docker image <BASENAME> to the registry"
67         echo "  list                    - List available images"
68         echo "  help                    - This help message"
69         echo ""
70         exit 0
71 fi
72
73 cd "$(dirname "$0")"
74
75 if [ $list == 1 ]; then
76         echo "Available Docker images:"
77         for f in *.Dockerfile; do
78                 filename=$( basename -- "$f" )
79                 basename="${filename%.*}"
80
81                 echo -e "  \\e[1;39m$basename\\e[0m"
82         done
83         exit 0
84 fi
85
86 # All commands after this require --base to be set
87 if [ -z "${base}" ]; then
88         echo "Usage: $0 <command>"
89         exit 1
90 fi
91
92 if [ ! -f "$base.Dockerfile" ]; then
93         echo -e "\\e[1;31mERROR\\e[0m: Dockerfile for '$base' not found"
94         exit 1
95 fi
96
97 if [ -z "${base_version}" ]; then
98         base_version="latest"
99 else
100         base_version="v$base_version"
101 fi
102
103 TAG="registry.gitlab.gnome.org/gnome/glib/${base}:${base_version}"
104
105 if [ $build == 1 ]; then
106         echo -e "\\e[1;32mBUILDING\\e[0m: ${base} as ${TAG}"
107         $DOCKER_CMD build \
108                 --build-arg HOST_USER_ID="$UID" \
109                 --build-arg COVERITY_SCAN_PROJECT_NAME="${COVERITY_SCAN_PROJECT_NAME}" \
110                 --build-arg COVERITY_SCAN_TOKEN="${COVERITY_SCAN_TOKEN}" \
111                 --tag "${TAG}" \
112                 --file "${base}.Dockerfile" .
113         exit $?
114 fi
115
116 if [ $push == 1 ]; then
117         echo -e "\\e[1;32mPUSHING\\e[0m: ${base} as ${TAG}"
118
119         if [ $no_login == 0 ]; then
120                 $DOCKER_CMD login registry.gitlab.gnome.org
121         fi
122
123         $DOCKER_CMD push $TAG
124         exit $?
125 fi
126
127 if [ $run == 1 ]; then
128         echo -e "\\e[1;32mRUNNING\\e[0m: ${base} as ${TAG}"
129         $DOCKER_CMD run \
130                 --rm \
131                 --volume "$(pwd)/..:/home/user/app" \
132                 --workdir "/home/user/app" \
133                 --tty \
134                 --interactive "${TAG}" \
135                 bash
136         exit $?
137 fi