aboutsummaryrefslogtreecommitdiff
path: root/infra/build/functions/deploy.sh
blob: d9ec203fc0336649507d1c2acecc3cb51bdac843 (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
# Copyright 2020 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################

BASE_IMAGE_JOB_TOPIC=schedule-base-image-build
BASE_IMAGE_SCHEDULER_JOB=base-image-scheduler
BASE_IMAGE_SCHEDULE="0 3 * * *"
BASE_IMAGE_MESSAGE="Start base image build"

BUILD_JOB_TOPIC=request-build

COVERAGE_BUILD_JOB_TOPIC=request-coverage-build
COVERAGE_BUILD_SCHEDULER_JOB=coverage-build-scheduler
COVERAGE_BUILD_SCHEDULE="0 6 * * *"
COVERAGE_BUILD_MESSAGE="Start coverage report builds"

SYNC_JOB_TOPIC=schedule-project-sync
SYNC_SCHEDULER_JOB=sync-scheduler
SYNC_JOB_SCHEDULE="*/30 * * * *"
SYNC_MESSAGE="Start Sync"


function deploy_pubsub_topic {
	topic=$1
	project=$2

	if ! gcloud pubsub topics describe $topic --project $project ;
		then
			gcloud pubsub topics create $topic \
			--project $project
	fi
}

function deploy_scheduler {
	scheduler_name=$1
	schedule="$2"
	topic=$3
	message="$4"
	project=$5

	if gcloud scheduler jobs describe $scheduler_name --project $project ;
		then
			gcloud scheduler jobs update pubsub $scheduler_name \
			--schedule "$schedule" \
			--topic $topic \
			--message-body "$message" \
			--project $project
		else
			gcloud scheduler jobs create pubsub $scheduler_name \
			--schedule "$schedule" \
			--topic $topic \
			--message-body "$message" \
			--project $project
	fi
}

function deploy_cloud_function {
	name=$1
	entry_point=$2
	topic=$3
	project=$4

	gcloud functions deploy $name \
	--entry-point $entry_point \
	--trigger-topic $topic \
	--runtime python37 \
	--project $project \
	--timeout 540
}

if [ $# == 2 ]; then
	PROJECT_ID=$1
	BASE_PROJECT_ID=$2
else
	echo -e "\n Usage ./deploy.sh <project-name> <base-project-name>"; exit;
fi

deploy_pubsub_topic $BUILD_JOB_TOPIC $PROJECT_ID
deploy_pubsub_topic $SYNC_JOB_TOPIC $PROJECT_ID
deploy_pubsub_topic $BASE_IMAGE_JOB_TOPIC $BASE_PROJECT_ID
deploy_pubsub_topic $COVERAGE_BUILD_JOB_TOPIC $PROJECT_ID

deploy_scheduler $SYNC_SCHEDULER_JOB \
				 "$SYNC_JOB_SCHEDULE" \
				 $SYNC_JOB_TOPIC \
				 "$SYNC_MESSAGE" \
				  $PROJECT_ID

deploy_scheduler $BASE_IMAGE_SCHEDULER_JOB \
				 "$BASE_IMAGE SCHEDULE" \
				  $BASE_IMAGE_JOB_TOPIC \
				  "$BASE_IMAGE_MESSAGE" \
				  $BASE_PROJECT_ID

deploy_scheduler $COVERAGE_BUILD_SCHEDULER_JOB \
				 "$COVERAGE_BUILD_SCHEDULE" \
				 $COVERAGE_BUILD_JOB_TOPIC \
				 "$COVERAGE_BUILD_MESSAGE" \
				 $PROJECT_ID

deploy_cloud_function sync \
					  sync \
					  $SYNC_JOB_TOPIC \
					  $PROJECT_ID

deploy_cloud_function base-image-build \
					  build_base_images \
					  $BASE_IMAGE_JOB_TOPIC \
					  $BASE_PROJECT_ID

deploy_cloud_function request-build \
					  build_project \
					  $BUILD_JOB_TOPIC \
					  $PROJECT_ID

deploy_cloud_function request-coverage-build \
					  coverage_build \
					  $COVERAGE_BUILD_JOB_TOPIC \
					  $PROJECT_ID