|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2019 The Kubernetes Authors. |
| 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 build node problem detector for presubmit and CI jobs and push to |
| 18 | +# staging, so that kubernetes E2E tests have access to the build. |
| 19 | + |
| 20 | +set -o errexit |
| 21 | +set -o nounset |
| 22 | +set -o pipefail |
| 23 | + |
| 24 | + |
| 25 | +NPD_STAGING_PATH=${NPD_STAGING_PATH:-"gs://node-problem-detector-staging"} |
| 26 | +NPD_STAGING_REGISTRY=${NPD_STAGING_REGISTRY:-"gcr.io/node-problem-detector-staging"} |
| 27 | +PR_ENV_FILENAME=${PR_ENV_FILENAME:-"pr.env"} |
| 28 | +CI_ENV_FILENAME=${CI_ENV_FILENAME:-"ci.env"} |
| 29 | +ROOT_PATH=$(git rev-parse --show-toplevel) |
| 30 | + |
| 31 | +function print-help() { |
| 32 | + echo "Usage: build.sh [args...]" |
| 33 | + echo "Available arguments:" |
| 34 | + echo " pr [pull_refs]: Build node-problem-detector for presubmit jobs and push to staging." |
| 35 | + echo " ci: Build node-problem-detector for CI jobs and push to staging." |
| 36 | + echo " get-ci-env: Download environment variable file from staging for CI job." |
| 37 | +} |
| 38 | + |
| 39 | +function get-version() { |
| 40 | + if [ -d .git ]; then |
| 41 | + echo `git describe --tags --dirty` |
| 42 | + else |
| 43 | + echo "UNKNOWN" |
| 44 | + fi |
| 45 | +} |
| 46 | + |
| 47 | +function write-env-file() { |
| 48 | + local -r env_file="${1}" |
| 49 | + if [[ -z "${env_file}" ]]; then |
| 50 | + echo "ERROR: env_file is missing." |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | + |
| 54 | + cat > ${ROOT_PATH}/${env_file} <<EOF |
| 55 | +export EXTRA_ENVS=NODE_PROBLEM_DETECTOR_IMAGE=${REGISTRY}/node-problem-detector:${TAG} |
| 56 | +export NODE_PROBLEM_DETECTOR_RELEASE_PATH=${UPLOAD_PATH} |
| 57 | +export NODE_PROBLEM_DETECTOR_VERSION=${VERSION} |
| 58 | +export NODE_PROBLEM_DETECTOR_TAR_HASH=$(sha1sum ${ROOT_PATH}/node-problem-detector-${VERSION}.tar.gz | cut -d ' ' -f1) |
| 59 | +EOF |
| 60 | +} |
| 61 | + |
| 62 | +function build-pr() { |
| 63 | + local -r PR="${1}" |
| 64 | + if [[ -z "${PR}" ]]; then |
| 65 | + echo "ERROR: pull_refs is missing." |
| 66 | + print-help |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | + |
| 70 | + export UPLOAD_PATH="${NPD_STAGING_PATH}/pr/${PR}" |
| 71 | + export REGISTRY="${NPD_STAGING_REGISTRY}/pr/${PR}" |
| 72 | + export VERSION=$(get-version) |
| 73 | + export TAG="${VERSION}" |
| 74 | + make push |
| 75 | + write-env-file ${PR_ENV_FILENAME} |
| 76 | +} |
| 77 | + |
| 78 | +function build-ci() { |
| 79 | + export UPLOAD_PATH="${NPD_STAGING_PATH}/ci" |
| 80 | + export REGISTRY="${NPD_STAGING_REGISTRY}/ci" |
| 81 | + export VERSION=$(get-version) |
| 82 | + export TAG="${VERSION}" |
| 83 | + make push |
| 84 | + write-env-file ${CI_ENV_FILENAME} |
| 85 | + gsutil mv ${ROOT_PATH}/${CI_ENV_FILENAME} ${UPLOAD_PATH} |
| 86 | +} |
| 87 | + |
| 88 | +function get-ci-env() { |
| 89 | + gsutil cp ${NPD_STAGING_PATH}/ci/${CI_ENV_FILENAME} ${ROOT_PATH}/${CI_ENV_FILENAME} |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +cd ${ROOT_PATH} |
| 94 | + |
| 95 | +if [[ "$#" -ne 1 && "$#" -ne 2 ]]; then |
| 96 | + echo "ERROR: Illegal number of parameters." |
| 97 | + print-help |
| 98 | + exit 1 |
| 99 | +fi |
| 100 | +COMMAND="${1}" |
| 101 | + |
| 102 | +if [[ "${COMMAND}" == "pr" ]]; then |
| 103 | + if [[ "$#" -ne 2 ]]; then |
| 104 | + echo "ERROR: pull_refs is missing." |
| 105 | + print-help |
| 106 | + exit 1 |
| 107 | + fi |
| 108 | + build-pr "${2}" |
| 109 | +elif [[ "${COMMAND}" == "ci" ]]; then |
| 110 | + build-ci |
| 111 | +elif [[ "${COMMAND}" == "get-ci-env" ]]; then |
| 112 | + get-ci-env |
| 113 | +else |
| 114 | + echo "ERROR: Invalid command." |
| 115 | + print-help |
| 116 | + exit 1 |
| 117 | +fi |
| 118 | + |
0 commit comments