blob: 535f1a6b7360f0489240b9f9a38745b370248efb [file] [log] [blame]
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +02001#!/bin/bash
2#
Damien Martin-Guillerez3e57fe02015-09-25 14:02:19 +02003# Copyright 2015 The Bazel Authors. All rights reserved.
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +02004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Scripts to configure the service that will poll git repositories for
18# in sync check.
19
20# Format of each entry is:
Philipp Wollermanna28803e2018-04-10 14:40:23 +020021# origin direction destination local-name
22# where
23# direction can be ==> or <=>
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +020024REPOSITORIES=(
Jin7b94f7a2020-12-15 20:42:27 +080025 "https://bazel.googlesource.com/tulsi ==> git@github.com:bazelbuild/tulsi.git tulsi upstream"
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +020026)
27
Philipp Wollermann9d04b982018-04-07 22:12:00 +020028set -euxo pipefail
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +020029
Philipp Wollermann9d04b982018-04-07 22:12:00 +020030# Download & decrypt gitcookies.
Philipp Wollermann046ae0f2019-01-18 11:38:04 +010031gsutil cat "gs://bazel-trusted-encrypted-secrets/gitsync-cookies.enc" | \
Philipp Wollermann9d04b982018-04-07 22:12:00 +020032 gcloud kms decrypt --location "global" --keyring "buildkite" --key "gitsync-cookies-key" --plaintext-file "-" --ciphertext-file "-" \
33 > /home/gitsync/.gitcookies
34chmod 0600 /home/gitsync/.gitcookies
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +020035
Philipp Wollermann9d04b982018-04-07 22:12:00 +020036# Download & decrypt GitHub SSH key.
Philipp Wollermann046ae0f2019-01-18 11:38:04 +010037gsutil cat "gs://bazel-trusted-encrypted-secrets/gitsync-ssh.enc" | \
Philipp Wollermann9d04b982018-04-07 22:12:00 +020038 gcloud kms decrypt --location "global" --keyring "buildkite" --key "gitsync-ssh-key" --plaintext-file "-" --ciphertext-file "-" \
39 > /home/gitsync/.ssh/id_rsa
40chmod 0600 /home/gitsync/.ssh/id_rsa
Damien Martin-Guillerez2af24352017-07-25 13:54:51 +020041
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +020042function clone() {
Philipp Wollermanna28803e2018-04-10 14:40:23 +020043 local origin="$1"
44 local destination="$3"
45 local name="$4"
46
47 rm -rf "${name}"
48 git clone "${origin}" "${name}"
49 pushd "${name}"
50 git remote add destination "${destination}"
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +020051 popd
52}
53
54function sync() {
Philipp Wollermanna28803e2018-04-10 14:40:23 +020055 local origin="$1"
56 local direction="$2"
57 local destination="$3"
58 local name="$4"
Jine01ed362020-08-10 17:18:13 +080059 local branch="$5"
Philipp Wollermanna28803e2018-04-10 14:40:23 +020060
61 pushd "${name}"
Jine01ed362020-08-10 17:18:13 +080062 echo "Syncing ${origin} ${direction} ${destination} on the $branch branch..."
Philipp Wollermanna28803e2018-04-10 14:40:23 +020063 git remote update --prune
Jine01ed362020-08-10 17:18:13 +080064 git checkout "origin/$branch" -B "$branch"
Philipp Wollermanna28803e2018-04-10 14:40:23 +020065 if [[ "${direction}" == "<=>" ]]; then
Jine01ed362020-08-10 17:18:13 +080066 git rebase "destination/$branch"
67 git push -f origin "$branch"
Philipp Wollermanna28803e2018-04-10 14:40:23 +020068 fi
Jine01ed362020-08-10 17:18:13 +080069 git push destination "$branch"
Damien Martin-Guillerez145bd5a2015-09-09 02:21:08 +020070 popd
71}
72
73# Get a local clone
74for i in "${REPOSITORIES[@]}"; do
75 clone $i
76done
77
78# Sync loop
79while true; do
80 for i in "${REPOSITORIES[@]}"; do
81 sync $i
82 done
83 # Sleep 30 seconds between each sync
84 sleep 30
85done