Damien Martin-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
Damien Martin-Guillerez | 3e57fe0 | 2015-09-25 14:02:19 +0200 | [diff] [blame] | 3 | # Copyright 2015 The Bazel Authors. All rights reserved. |
Damien Martin-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 4 | # |
| 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 Wollermann | a28803e | 2018-04-10 14:40:23 +0200 | [diff] [blame] | 21 | # origin direction destination local-name |
| 22 | # where |
| 23 | # direction can be ==> or <=> |
Damien Martin-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 24 | REPOSITORIES=( |
Jin | 7b94f7a | 2020-12-15 20:42:27 +0800 | [diff] [blame] | 25 | "https://bazel.googlesource.com/tulsi ==> git@github.com:bazelbuild/tulsi.git tulsi upstream" |
Damien Martin-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 26 | ) |
| 27 | |
Philipp Wollermann | 9d04b98 | 2018-04-07 22:12:00 +0200 | [diff] [blame] | 28 | set -euxo pipefail |
Damien Martin-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 29 | |
Philipp Wollermann | 9d04b98 | 2018-04-07 22:12:00 +0200 | [diff] [blame] | 30 | # Download & decrypt gitcookies. |
Philipp Wollermann | 046ae0f | 2019-01-18 11:38:04 +0100 | [diff] [blame] | 31 | gsutil cat "gs://bazel-trusted-encrypted-secrets/gitsync-cookies.enc" | \ |
Philipp Wollermann | 9d04b98 | 2018-04-07 22:12:00 +0200 | [diff] [blame] | 32 | gcloud kms decrypt --location "global" --keyring "buildkite" --key "gitsync-cookies-key" --plaintext-file "-" --ciphertext-file "-" \ |
| 33 | > /home/gitsync/.gitcookies |
| 34 | chmod 0600 /home/gitsync/.gitcookies |
Damien Martin-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 35 | |
Philipp Wollermann | 9d04b98 | 2018-04-07 22:12:00 +0200 | [diff] [blame] | 36 | # Download & decrypt GitHub SSH key. |
Philipp Wollermann | 046ae0f | 2019-01-18 11:38:04 +0100 | [diff] [blame] | 37 | gsutil cat "gs://bazel-trusted-encrypted-secrets/gitsync-ssh.enc" | \ |
Philipp Wollermann | 9d04b98 | 2018-04-07 22:12:00 +0200 | [diff] [blame] | 38 | gcloud kms decrypt --location "global" --keyring "buildkite" --key "gitsync-ssh-key" --plaintext-file "-" --ciphertext-file "-" \ |
| 39 | > /home/gitsync/.ssh/id_rsa |
| 40 | chmod 0600 /home/gitsync/.ssh/id_rsa |
Damien Martin-Guillerez | 2af2435 | 2017-07-25 13:54:51 +0200 | [diff] [blame] | 41 | |
Damien Martin-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 42 | function clone() { |
Philipp Wollermann | a28803e | 2018-04-10 14:40:23 +0200 | [diff] [blame] | 43 | 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-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 51 | popd |
| 52 | } |
| 53 | |
| 54 | function sync() { |
Philipp Wollermann | a28803e | 2018-04-10 14:40:23 +0200 | [diff] [blame] | 55 | local origin="$1" |
| 56 | local direction="$2" |
| 57 | local destination="$3" |
| 58 | local name="$4" |
Jin | e01ed36 | 2020-08-10 17:18:13 +0800 | [diff] [blame] | 59 | local branch="$5" |
Philipp Wollermann | a28803e | 2018-04-10 14:40:23 +0200 | [diff] [blame] | 60 | |
| 61 | pushd "${name}" |
Jin | e01ed36 | 2020-08-10 17:18:13 +0800 | [diff] [blame] | 62 | echo "Syncing ${origin} ${direction} ${destination} on the $branch branch..." |
Philipp Wollermann | a28803e | 2018-04-10 14:40:23 +0200 | [diff] [blame] | 63 | git remote update --prune |
Jin | e01ed36 | 2020-08-10 17:18:13 +0800 | [diff] [blame] | 64 | git checkout "origin/$branch" -B "$branch" |
Philipp Wollermann | a28803e | 2018-04-10 14:40:23 +0200 | [diff] [blame] | 65 | if [[ "${direction}" == "<=>" ]]; then |
Jin | e01ed36 | 2020-08-10 17:18:13 +0800 | [diff] [blame] | 66 | git rebase "destination/$branch" |
| 67 | git push -f origin "$branch" |
Philipp Wollermann | a28803e | 2018-04-10 14:40:23 +0200 | [diff] [blame] | 68 | fi |
Jin | e01ed36 | 2020-08-10 17:18:13 +0800 | [diff] [blame] | 69 | git push destination "$branch" |
Damien Martin-Guillerez | 145bd5a | 2015-09-09 02:21:08 +0200 | [diff] [blame] | 70 | popd |
| 71 | } |
| 72 | |
| 73 | # Get a local clone |
| 74 | for i in "${REPOSITORIES[@]}"; do |
| 75 | clone $i |
| 76 | done |
| 77 | |
| 78 | # Sync loop |
| 79 | while true; do |
| 80 | for i in "${REPOSITORIES[@]}"; do |
| 81 | sync $i |
| 82 | done |
| 83 | # Sleep 30 seconds between each sync |
| 84 | sleep 30 |
| 85 | done |