summaryrefslogtreecommitdiff
path: root/.gitlab-ci/run-docker.sh
blob: 63cbacf6099ce20ffc1859c9437fa367e9ae4a8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash

read_arg() {
    # $1 = arg name
    # $2 = arg value
    # $3 = arg parameter
    local rematch='^[^=]*=(.*)$'
    if [[ $2 =~ $rematch ]]; then
        read -r "$1" <<< "${BASH_REMATCH[1]}"
    else
        read -r "$1" <<< "$3"
        # There is no way to shift our callers args, so
        # return 1 to indicate they should do it instead.
        return 1
    fi
}

if type -p podman &>/dev/null; then
    # Using podman
    DOCKER_CMD="podman"
    # Docker is actually implemented by podman, and its OCI output
    # is incompatible with some of the dockerd instances on GitLab
    # CI runners.
    export BUILDAH_FORMAT=docker
elif getent group docker | grep -q "\b${USER}\b"; then
    DOCKER_CMD="docker"
else
    DOCKER_CMD="sudo docker"
fi

set -e

branch=""
version=""
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;;
        --branch|-b) read_arg branch "$@" || shift;;
        --version|-v) read_arg 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 <command> [options] [basename]"
    echo ""
    echo "Available commands"
    echo ""
    echo "  build     - Build Docker image"
    echo "  run       - Run Docker image"
    echo "  push      - 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

# We really need to know the branch name after this point
if [[ -z "${branch}" ]]; then
    branch=master
fi

DOCKERFILE="${branch}.Dockerfile"
if [ ! -f "$DOCKERFILE" ]; then
    echo -e "\\e[1;31mERROR\\e[0m: '$DOCKERFILE' not found"
    exit 1
fi

if [ -z "${version}" ]; then
    version="latest"
else
    version="v$version"
fi

TAG="registry.gitlab.gnome.org/gnome/libsecret/${branch}:${version}"

if [ $build == 1 ]; then
    echo -e "\\e[1;32mBUILDING\\e[0m: ${TAG} for branch '${branch}'"
    $DOCKER_CMD build \
        --build-arg HOST_USER_ID="$UID" \
        --tag "${TAG}" \
        --file "$DOCKERFILE" .
    exit $?
fi

if [ $push == 1 ]; then
    echo -e "\\e[1;32mPUSHING\\e[0m: ${TAG} for branch '${branch}'"

    if [ $no_login == 0 ]; then
        $DOCKER_CMD login registry.gitlab.gnome.org
    fi

    $DOCKER_CMD push "${TAG}"
    exit $?
fi

if [ $run == 1 ]; then
    echo -e "\\e[1;32mRUNNING\\e[0m: ${TAG} for branch '${branch}'"
    $DOCKER_CMD run \
        --rm \
        --volume "$(pwd)/..:/home/user/app" \
        --workdir "/home/user/app" \
        --tty \
        --interactive "${TAG}" \
        bash
    exit $?
fi