Bladeren bron

Added script for building images with injection of build_code

master
jfederico 4 jaren geleden
bovenliggende
commit
675f8f6f27
1 gewijzigde bestanden met toevoegingen van 97 en 0 verwijderingen
  1. 97
    0
      scripts/build.sh

+ 97
- 0
scripts/build.sh Bestand weergeven

@@ -0,0 +1,97 @@
1
+#!/bin/bash
2
+
3
+################################################################################
4
+# For this script to work properly it is required to define some environment variables
5
+# in the CI/CD Env variable declaration, while others should be passed as parameters.
6
+#
7
+#------------------------------------------------------------------------------
8
+# Defined as part of the CD/CI Env Variables:
9
+#
10
+# CD_DOCKER_USERNAME
11
+# A DockerHub username to be used for uploading the build.
12
+#
13
+# CD_DOCKER_PASSWORD
14
+# A DockerHub password to be used for uploading the build.
15
+#
16
+# CD_DOCKER_REPO
17
+# A DockerHub repository. By default the CD_REF_SLUG is also used as the docker repo.
18
+#
19
+# CD_BUILD_ALL
20
+# As the build is supposed to be done only for master (for a nightly deployments) and for releases
21
+# (like 'release-2.0.5' for production deployments), it is additionally required to include this
22
+# variable in order to build any other brnach, as it may be required for testing or reviewing work
23
+# as part of the development process.
24
+#
25
+
26
+display_usage() {
27
+  echo "This script should be used as part of a CI strategy."
28
+  echo -e "Usage:\n  build_image.sh [ARGUMENTS]"
29
+  echo -e "\nMandatory arguments \n"
30
+  echo -e "  repo_slug     The git repository  (e.g. bigbluebutton/greenlight)"
31
+  echo -e "\nOptional arguments \n"
32
+  echo -e "  branch | tag  The branch (e.g. master | release-2.0.5)"
33
+}
34
+
35
+# if less than two arguments supplied, display usage
36
+if [ $# -le 0 ]; then
37
+	display_usage
38
+	exit 1
39
+fi
40
+
41
+# check whether user had supplied -h or --help . If yes display usage
42
+if [[ ($# == "--help") ||  $# == "-h" ]]; then
43
+	display_usage
44
+	exit 0
45
+fi
46
+
47
+export CD_REF_SLUG=$1
48
+export CD_REF_NAME=$2
49
+if [ -z $CD_REF_NAME ]; then
50
+  export CD_REF_NAME=$(git branch | grep \* | cut -d ' ' -f2)
51
+fi
52
+
53
+if [ "$CD_REF_NAME" != "master" ] && [[ "$CD_REF_NAME" != *"release"* ]] && ( [ -z "$CD_BUILD_ALL" ] || [ "$CD_BUILD_ALL" != "true" ] ); then
54
+  echo "#### Docker image for $CD_REF_SLUG:$CD_REF_NAME won't be built"
55
+  exit 0
56
+fi
57
+
58
+# Include sqlite for production
59
+sqliteCount="$(grep "gem 'sqlite3'" Gemfile | wc -l)"
60
+
61
+if [ $sqliteCount -lt 2 ]; then
62
+  sed -i "/^group :production do/a\ \ gem 'sqlite3', '~> 1.3.6'" Gemfile
63
+fi
64
+
65
+# Set the version tag when it is a release or the commit sha was included.
66
+if [[ "$CD_REF_NAME" == *"release"* ]]; then
67
+  export CD_BUILD_NUMBER=${CD_REF_NAME:8}
68
+else
69
+  export CD_BUILD_NUMBER="$CD_REF_NAME ($(expr substr $(git rev-parse HEAD) 1 7))"
70
+fi
71
+
72
+# Build the image
73
+if [ -z $CD_DOCKER_REPO ]; then
74
+  export CD_DOCKER_REPO=$CD_REF_SLUG
75
+fi
76
+echo "#### Docker image $CD_DOCKER_REPO:$CD_REF_NAME is being built"
77
+docker build --build-arg build_number="${CD_BUILD_NUMBER}" -t $CD_DOCKER_REPO:$CD_REF_NAME .
78
+
79
+if [ -z "$CD_DOCKER_USERNAME" ] || [ -z "$CD_DOCKER_PASSWORD" ]; then
80
+  echo "#### Docker image for $CD_DOCKER_REPO can't be published because CD_DOCKER_USERNAME or CD_DOCKER_PASSWORD are missing (Ignore this warning if running outside a CD/CI environment)"
81
+  exit 0
82
+fi
83
+
84
+# Publish the image
85
+docker login -u="$CD_DOCKER_USERNAME" -p="$CD_DOCKER_PASSWORD"
86
+echo "#### Docker image $CD_DOCKER_REPO:$CD_REF_NAME is being published"
87
+docker push $CD_DOCKER_REPO
88
+
89
+# Publish image as latest and v2 if it is a release (excluding alpha and beta)
90
+if [[ "$CD_REF_NAME" == *"release"* ]] && [[ "$CD_REF_NAME" != *"alpha"* ]] && [[ "$CD_REF_NAME" != *"beta"* ]]; then
91
+  docker_image_id=$(docker images | grep -E "^$CD_DOCKER_REPO.*$CD_REF_NAME" | awk -e '{print $3}')
92
+  docker tag $docker_image_id $CD_DOCKER_REPO:latest
93
+  docker push $CD_DOCKER_REPO:latest
94
+  docker tag $docker_image_id $CD_DOCKER_REPO:v2
95
+  docker push $CD_DOCKER_REPO:v2
96
+fi
97
+exit 0

Laden…
Annuleren
Opslaan