1#!/bin/bash 2# Copyright 2018 The gRPC Authors 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16set -ex 17 18shopt -s nullglob 19 20cd "$(dirname "$0")/../../.." 21 22GRPC_VERSION=$(grep -e "^ *version: " build_handwritten.yaml | head -n 1 | sed 's/.*: //') 23 24INPUT_ARTIFACTS=$KOKORO_GFILE_DIR/github/grpc/artifacts 25INDEX_FILENAME=index.xml 26 27BUILD_ID=${KOKORO_BUILD_ID:-$(uuidgen)} 28BUILD_BRANCH_NAME=master 29BUILD_GIT_COMMIT=${KOKORO_GIT_COMMIT:-unknown} 30BUILD_TIMESTAMP=$(date -Iseconds) 31BUILD_RELPATH=$(date "+%Y/%m")/$BUILD_GIT_COMMIT-$BUILD_ID/ 32 33GCS_ROOT=gs://packages.grpc.io/ 34GCS_ARCHIVE_PREFIX=archive/ 35GCS_ARCHIVE_ROOT=$GCS_ROOT$GCS_ARCHIVE_PREFIX 36GCS_INDEX=$GCS_ROOT$INDEX_FILENAME 37 38LOCAL_STAGING_TEMPDIR=$(mktemp -d) 39LOCAL_BUILD_ROOT=$LOCAL_STAGING_TEMPDIR/$BUILD_RELPATH 40LOCAL_BUILD_INDEX=$LOCAL_BUILD_ROOT$INDEX_FILENAME 41 42mkdir -p "$LOCAL_BUILD_ROOT" 43 44find "$INPUT_ARTIFACTS" -type f 45 46# protoc Plugins 47PROTOC_PLUGINS_ZIPPED_PACKAGES=$(mktemp -d) 48for zip_dir in protoc_windows_{x86,x64} 49do 50 zip -jr "$PROTOC_PLUGINS_ZIPPED_PACKAGES/grpc-$zip_dir-$GRPC_VERSION.zip" "$INPUT_ARTIFACTS/$zip_dir/"* 51done 52for tar_dir in protoc_{linux,macos}_{x86,x64} 53do 54 chmod +x "$INPUT_ARTIFACTS/$tar_dir"/* 55 tar -cvzf "$PROTOC_PLUGINS_ZIPPED_PACKAGES/grpc-$tar_dir-$GRPC_VERSION.tar.gz" -C "$INPUT_ARTIFACTS/$tar_dir" . 56done 57 58PROTOC_PACKAGES=( 59 "$PROTOC_PLUGINS_ZIPPED_PACKAGES"/grpc-protoc_windows_{x86,x64}-"$GRPC_VERSION.zip" 60 "$PROTOC_PLUGINS_ZIPPED_PACKAGES"/grpc-protoc_{linux,macos}_{x86,x64}-"$GRPC_VERSION.tar.gz" 61) 62 63# C# 64UNZIPPED_CSHARP_PACKAGES=$(mktemp -d) 65unzip "$INPUT_ARTIFACTS/csharp_nugets_windows_dotnetcli.zip" -d "$UNZIPPED_CSHARP_PACKAGES" 66CSHARP_PACKAGES=( 67 "$UNZIPPED_CSHARP_PACKAGES"/* 68 "$INPUT_ARTIFACTS"/grpc_unity_package.[0-9]*.zip 69) 70 71# Python 72PYTHON_GRPCIO_PACKAGES=( 73 "$INPUT_ARTIFACTS"/grpcio-[0-9]*.tar.gz 74 "$INPUT_ARTIFACTS"/grpcio-[0-9]*.whl 75 "$INPUT_ARTIFACTS"/python_linux_extra_arm*/grpcio-[0-9]*.whl 76) 77PYTHON_GRPCIO_TOOLS_PACKAGES=( 78 "$INPUT_ARTIFACTS"/grpcio-tools-[0-9]*.tar.gz 79 "$INPUT_ARTIFACTS"/grpcio_tools-[0-9]*.whl 80 "$INPUT_ARTIFACTS"/python_linux_extra_arm*/grpcio_tools-[0-9]*.whl 81) 82PYTHON_GRPCIO_HEALTH_CHECKING_PACKAGES=( 83 "$INPUT_ARTIFACTS"/grpcio-health-checking-[0-9]*.tar.gz 84) 85PYTHON_GRPCIO_REFLECTION_PACKAGES=( 86 "$INPUT_ARTIFACTS"/grpcio-reflection-[0-9]*.tar.gz 87) 88PYTHON_GRPCIO_TESTING_PACKAGES=( 89 "$INPUT_ARTIFACTS"/grpcio-testing-[0-9]*.tar.gz 90) 91 92# PHP 93PHP_PACKAGES=( 94 "$INPUT_ARTIFACTS"/grpc-[0-9]*.tgz 95) 96 97# Ruby 98RUBY_PACKAGES=( 99 "$INPUT_ARTIFACTS"/grpc-[0-9]*.gem 100 "$INPUT_ARTIFACTS"/grpc-tools-[0-9]*.gem 101) 102 103function add_to_manifest() { 104 local artifact_type=$1 105 local artifact_file=$2 106 local artifact_prefix=$3 107 local artifact_name 108 artifact_name=$(basename "$artifact_file") 109 local artifact_size 110 artifact_size=$(stat -c%s "$artifact_file") 111 local artifact_sha256 112 artifact_sha256=$(openssl sha256 -r "$artifact_file" | cut -d " " -f 1) 113 local artifact_target=$LOCAL_BUILD_ROOT/$artifact_type/$artifact_prefix 114 mkdir -p "$artifact_target" 115 cp "$artifact_file" "$artifact_target" 116 cat <<EOF 117 <artifact name='$artifact_name' 118 type='$artifact_type' 119 path='$artifact_type/$artifact_prefix$artifact_name' 120 size='$artifact_size' 121 sha256='$artifact_sha256' /> 122EOF 123} 124 125{ 126 cat <<EOF 127<?xml version="1.0"?> 128<?xml-stylesheet href="/web-assets/build-201807.xsl" type="text/xsl"?> 129<build id='$BUILD_ID' timestamp='$BUILD_TIMESTAMP' version="201807"> 130 <metadata> 131 <project>gRPC</project> 132 <repository>https://github.com/grpc/grpc</repository> 133 <branch>$BUILD_BRANCH_NAME</branch> 134 <commit>$BUILD_GIT_COMMIT</commit> 135 </metadata> 136 <artifacts> 137EOF 138 139 for pkg in "${PROTOC_PACKAGES[@]}"; do add_to_manifest protoc "$pkg"; done 140 for pkg in "${CSHARP_PACKAGES[@]}"; do add_to_manifest csharp "$pkg"; done 141 for pkg in "${PHP_PACKAGES[@]}"; do add_to_manifest php "$pkg"; done 142 for pkg in "${PYTHON_GRPCIO_PACKAGES[@]}"; do add_to_manifest python "$pkg" grpcio/; done 143 for pkg in "${PYTHON_GRPCIO_TOOLS_PACKAGES[@]}"; do add_to_manifest python "$pkg" grpcio-tools/; done 144 for pkg in "${PYTHON_GRPCIO_HEALTH_CHECKING_PACKAGES[@]}"; do add_to_manifest python "$pkg" grpcio-health-checking/; done 145 for pkg in "${PYTHON_GRPCIO_REFLECTION_PACKAGES[@]}"; do add_to_manifest python "$pkg" grpcio-reflection/; done 146 for pkg in "${PYTHON_GRPCIO_TESTING_PACKAGES[@]}"; do add_to_manifest python "$pkg" grpcio-testing/; done 147 for pkg in "${RUBY_PACKAGES[@]}"; do add_to_manifest ruby "$pkg"; done 148 149 cat <<EOF 150 </artifacts> 151</build> 152EOF 153}> "$LOCAL_BUILD_INDEX" 154 155LOCAL_BUILD_INDEX_SIZE=$(stat -c%s "$LOCAL_BUILD_INDEX") 156LOCAL_BUILD_INDEX_SHA256=$(openssl sha256 -r "$LOCAL_BUILD_INDEX" | cut -d " " -f 1) 157 158OLD_INDEX=$(mktemp) 159NEW_INDEX=$(mktemp) 160 161# Download the current /index.xml into $OLD_INDEX 162gsutil cp "$GCS_INDEX" "$OLD_INDEX" 163 164{ 165 # we want to add an entry as the first child under <builds> tag 166 # we can get by without a real XML parser by rewriting the header, 167 # injecting our new tag, and then dumping the rest of the file as is. 168 cat <<EOF 169<?xml version="1.0"?> 170<?xml-stylesheet href="/web-assets/home.xsl" type="text/xsl"?> 171<packages> 172 <builds> 173 <build id='$BUILD_ID' 174 timestamp='$BUILD_TIMESTAMP' 175 branch='$BUILD_BRANCH_NAME' 176 commit='$BUILD_GIT_COMMIT' 177 path='$GCS_ARCHIVE_PREFIX$BUILD_RELPATH$INDEX_FILENAME' 178 size='$LOCAL_BUILD_INDEX_SIZE' 179 sha256='$LOCAL_BUILD_INDEX_SHA256' /> 180EOF 181 tail --lines=+5 "$OLD_INDEX" 182}> "$NEW_INDEX" 183 184 185function generate_directory_index() 186{ 187 local target_dir=$1 188 local current_directory_name 189 current_directory_name=$(basename "$target_dir") 190 cat <<EOF 191<!DOCTYPE html> 192<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang=""> 193 <head> 194 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 195 <title>Index of $current_directory_name - packages.grpc.io</title> 196 <link rel="stylesheet" type="text/css" href="/web-assets/dirindex.css" /> 197 </head> 198 <body> 199 <h1>Index of <a href="#"><code>$current_directory_name</code></a></h1> 200 <ul> 201 <li><a href="#">.</a></li> 202 <li><a href="..">..</a></li> 203EOF 204 205( 206 cd "$target_dir" 207 find * -maxdepth 0 -type d -print | sort | while read -r line 208 do 209 echo " <li><a href='$line/'>$line/</a></li>" 210 done 211 find * -maxdepth 0 -type f -print | sort | while read -r line 212 do 213 echo " <li><a href='$line'>$line</a></li>" 214 done 215) 216 217cat <<EOF 218 </ul> 219 </body> 220</html> 221EOF 222} 223 224# Upload the current build artifacts 225gsutil -m cp -r "$LOCAL_STAGING_TEMPDIR/${BUILD_RELPATH%%/*}" "$GCS_ARCHIVE_ROOT" 226# Upload directory indices for subdirectories 227( 228 cd "$LOCAL_BUILD_ROOT" 229 find * -type d | while read -r directory 230 do 231 generate_directory_index "$directory" | gsutil -h 'Content-Type:text/html' cp - "$GCS_ARCHIVE_ROOT$BUILD_RELPATH$directory/$INDEX_FILENAME" 232 done 233) 234# Upload the new /index.xml 235gsutil -h "Content-Type:application/xml" cp "$NEW_INDEX" "$GCS_INDEX" 236 237# Upload C# nugets to the dev nuget feed 238pushd "$UNZIPPED_CSHARP_PACKAGES" 239docker pull mcr.microsoft.com/dotnet/core/sdk:2.1 240for nugetfile in *.nupkg 241do 242 echo "Going to push $nugetfile" 243 # use nuget from a docker container to push the nupkg 244 set +x # IMPORTANT: avoid revealing the nuget api key by the command echo 245 docker run -v "$(pwd):/nugets:ro" --rm=true mcr.microsoft.com/dotnet/core/sdk:2.1 bash -c "dotnet nuget push /nugets/$nugetfile -k $(cat ${KOKORO_GFILE_DIR}/artifactory_grpc_nuget_dev_api_key) --source https://grpc.jfrog.io/grpc/api/nuget/v3/grpc-nuget-dev" 246 set -ex 247done 248popd 249