blob: 1e75ff0e4b7554e2fccb8a37cfa37a9b75c3cb6f [file] [log] [blame]
Yannic Bonenbergerff449692019-07-25 05:55:49 -07001#!/bin/bash
2
3# Copyright 2019 The Bazel Authors. All rights reserved.
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# This script fetches
18# `https://github.com/bazelbuild/<rules_xyz>/archive/<version>.tar.gz`
19# and uploads it to the cannonical location on `https://mirror.bazel.build`.
20
21set -euo pipefail
22
23function validate_input {
24 local n="$1"
25 local input="$2"
26 # Valid inputs contain only alphanumeric letters or [_-.]
27 # and must be between 3 (e.g. numbered releases like 0.1 or 1.0) and 40
28 # (e.g. git commit hashes) characters long.
29 if [[ ! "${input}" =~ ^[a-zA-Z0-9_\\-\\.]{3,40}$ ]]; then
30 echo "Argument ${n} with value '${input}' contains invalid characters," \
31 "or is not between 3 and 40 characters long"
32 exit 1
33 fi
34}
35
36if [ "$#" -ne 2 ]; then
37 echo "Usage: bazel run //tools:upload_bazel_mirror -- <rules_xyz> <version>"
38 exit 1
39fi
40
41REPO="$1"
42validate_input 1 "${REPO}"
43
44# TODO(yannic): Add option to get latest commit or release from GitHub API.
45VERSION="$2"
46validate_input 2 "${VERSION}"
47
48# Create a temp directory to hold the versioned tarball,
49# and clean it up when the script exits.
50tmpdir="$(mktemp -d)"
51function cleanup {
52 rm -rf "$tmpdir"
53}
54trap cleanup EXIT
55
56url="https://github.com/bazelbuild/${REPO}/archive/${VERSION}.tar.gz"
57versioned_filename="${REPO}-${VERSION}.tar.gz"
58versioned_archive="${tmpdir}/${versioned_filename}"
59
60# Download tarball into temporary folder.
61# -L to follow redirects.
62curl -L --fail --output "${versioned_archive}" "${url}"
63
64# Upload the tarball to GCS.
65# -n for no-clobber, so we don't overwrite existing files
66gsutil cp -n "${versioned_archive}" \
67 "gs://bazel-mirror/github.com/bazelbuild/${REPO}/archive/${VERSION}.tar.gz"