|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -o nounset |
| 4 | +set -o pipefail |
| 5 | +set -x |
| 6 | + |
| 7 | +CI_SERVER_URL=https://prow.svc.ci.openshift.org/view/gcs/origin-ci-test |
| 8 | +COVER_PROFILE=${COVER_PROFILE:-"$1"} |
| 9 | +JOB_TYPE=${JOB_TYPE:-"local"} |
| 10 | + |
| 11 | +# Configure the git refs and job link based on how the job was triggered via prow |
| 12 | +if [[ "${JOB_TYPE}" == "presubmit" ]]; then |
| 13 | + echo "detected PR code coverage job for #${PULL_NUMBER}" |
| 14 | + REF_FLAGS="-P ${PULL_NUMBER} -C ${PULL_PULL_SHA}" |
| 15 | + JOB_LINK="${CI_SERVER_URL}/pr-logs/pull/${REPO_OWNER}_${REPO_NAME}/${PULL_NUMBER}/${JOB_NAME}/${BUILD_ID}" |
| 16 | +elif [[ "${JOB_TYPE}" == "batch" ]] || [[ "${JOB_TYPE}" == "postsubmit" ]]; then |
| 17 | + echo "detected branch code coverage job for ${PULL_BASE_REF}" |
| 18 | + REF_FLAGS="-B ${PULL_BASE_REF} -C ${PULL_BASE_SHA}" |
| 19 | + JOB_LINK="${CI_SERVER_URL}/logs/${JOB_NAME}/${BUILD_ID}" |
| 20 | +elif [[ "${JOB_TYPE}" == "local" ]]; then |
| 21 | + echo "coverage report available at ${COVER_PROFILE}" |
| 22 | + exit 0 |
| 23 | +else |
| 24 | + echo "${JOB_TYPE} jobs not supported" >&2 |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | + |
| 28 | +# Configure certain internal codecov variables with values from prow. |
| 29 | +export CI_BUILD_URL="${JOB_LINK}" |
| 30 | +export CI_BUILD_ID="${JOB_NAME}" |
| 31 | +export CI_JOB_ID="${BUILD_ID}" |
| 32 | + |
| 33 | +if [[ "${JOB_TYPE}" != "local" ]]; then |
| 34 | + if [[ -z "${ARTIFACT_DIR:-}" ]] || [[ ! -d "${ARTIFACT_DIR}" ]] || [[ ! -w "${ARTIFACT_DIR}" ]]; then |
| 35 | + # shellcheck disable=SC2016 |
| 36 | + echo '${ARTIFACT_DIR} must be set for non-local jobs, and must point to a writable directory' >&2 |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + curl -sS https://codecov.io/bash -o "${ARTIFACT_DIR}/codecov.sh" |
| 40 | + bash <(cat "${ARTIFACT_DIR}/codecov.sh") -Z -K -f "${COVER_PROFILE}" -r "${REPO_OWNER}/${REPO_NAME}" "${REF_FLAGS}" |
| 41 | + # shellcheck disable=SC2181 |
| 42 | + if [ $? -ne 0 ]; then |
| 43 | + echo "Failed uploading coverage report from a non local environment. Exiting gracefully with status code 0." |
| 44 | + exit 0 |
| 45 | + fi |
| 46 | +else |
| 47 | + bash <(curl -s https://codecov.io/bash) -Z -K -f "${COVER_PROFILE}" -r "${REPO_OWNER}/${REPO_NAME}" "${REF_FLAGS}" |
| 48 | + # shellcheck disable=SC2181 |
| 49 | + if [ $? -ne 0 ]; then |
| 50 | + echo "Failed uploading coverage report from local environment. Exiting gracefully with status code 0." |
| 51 | + exit 0 |
| 52 | + fi |
| 53 | +fi |
0 commit comments