6f0a0c35c5fc844783223335c05718de1b251b90
[releng.git] / jjb / opnfv / opnfv-docker.sh
1 #!/bin/bash
2 set -o errexit
3 set -o nounset
4 set -o pipefail
5
6
7 echo "Starting opnfv-docker for $DOCKER_REPO_NAME ..."
8 echo "--------------------------------------------------------"
9 echo
10
11
12 # Remove previous running containers if exist
13 if [[ ! -z $(docker ps -a | grep $DOCKER_REPO_NAME) ]]; then
14     echo "Removing existing $DOCKER_REPO_NAME containers..."
15     docker ps | grep $DOCKER_REPO_NAME | awk '{{print $1}}' | xargs docker stop
16     docker ps -a | grep $DOCKER_REPO_NAME | awk '{{print $1}}' | xargs docker rm
17 fi
18
19
20 # Remove existing images if exist
21 if [[ ! -z $(docker images | grep $DOCKER_REPO_NAME) ]]; then
22     echo "Docker images to remove:"
23     docker images | head -1 && docker images | grep $DOCKER_REPO_NAME
24     image_tags=($(docker images | grep $DOCKER_REPO_NAME | awk '{{print $2}}'))
25     for tag in "${{image_tags[@]}}"; do
26         echo "Removing docker image $DOCKER_REPO_NAME:$tag..."
27         docker rmi $DOCKER_REPO_NAME:$tag
28     done
29 fi
30
31 # If we just want to update the latest_stable image
32 if [[ "$UPDATE_LATEST_STABLE" == "true" ]]; then
33     echo "Pulling $DOCKER_REPO_NAME:$STABLE_TAG ..."
34     docker pull $DOCKER_REPO_NAME:$STABLE_TAG
35     if [[ $? -ne 0 ]]; then
36         echo "ERROR: The image $DOCKER_REPO_NAME with tag $STABLE_TAG does not exist."
37         exit 1
38     fi
39     docker tag $DOCKER_REPO_NAME:$STABLE_TAG $DOCKER_REPO_NAME:latest_stable
40     echo "Pushing $DOCKER_REPO_NAME:latest_stable ..."
41     docker push $DOCKER_REPO_NAME:latest_stable
42     exit 0
43 fi
44
45
46 # Get tag version
47 cd $WORKSPACE
48 git clone https://gerrit.opnfv.org/gerrit/releng
49
50 DOCKER_TAG=$($WORKSPACE/releng/utils/calculate_version.sh -t docker \
51     -n $DOCKER_REPO_NAME)
52
53 ret_val=$?
54 if [ $ret_val -ne 0 ]; then
55     echo "Error retrieving the version tag."
56     exit 1
57 else
58     echo "Tag version to be build and pushed: $DOCKER_TAG"
59 fi
60
61
62 # cd to directory where Dockerfile is located
63 if [[ "$DOCKER_REPO_NAME" == "opnfv/functest" ]]; then
64     cd $WORKSPACE/docker
65 elif [[ "$DOCKER_REPO_NAME" == "opnfv/yardstick" ]]; then
66     cd $WORKSPACE/ci/docker/yardstick-ci
67 elif [[ "$DOCKER_REPO_NAME" == "opnfv/storperf" ]]; then
68     cd $WORKSPACE/docker
69 else
70     echo "ERROR: DOCKER_REPO_NAME parameter not valid: $DOCKER_REPO_NAME"
71     exit 1
72 fi
73
74 # Start the build
75 echo "Building docker image: $DOCKER_REPO_NAME:$DOCKER_TAG..."
76
77 docker build -t $DOCKER_REPO_NAME:$DOCKER_TAG .
78 echo "Creating tag 'latest'..."
79 docker tag $DOCKER_REPO_NAME:$DOCKER_TAG $DOCKER_REPO_NAME:latest
80
81 # list the images
82 echo "Available images are:"
83 docker images
84
85 # Push image to Dockerhub
86 if [[ "$PUSH_IMAGE" == "true" ]]; then
87     echo "Pushing $DOCKER_REPO_NAME:$DOCKER_TAG to the docker registry..."
88     echo "--------------------------------------------------------"
89     echo
90     # Push to the Dockerhub repository
91     docker push $DOCKER_REPO_NAME:$DOCKER_TAG
92
93     echo "Updating $DOCKER_REPO_NAME:latest to the docker registry..."
94     docker push $DOCKER_REPO_NAME:latest
95 fi