liamfallon | 5780bdf | 2021-11-29 13:26:27 +0000 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | |
| 3 | # |
| 4 | # ============LICENSE_START================================================ |
| 5 | # ONAP |
| 6 | # ========================================================================= |
| 7 | # Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. |
| 8 | # ========================================================================= |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | # you may not use this file except in compliance with the License. |
| 11 | # You may obtain a copy of the License at |
| 12 | # |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | # |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | # See the License for the specific language governing permissions and |
| 19 | # limitations under the License. |
| 20 | # ============LICENSE_END================================================== |
| 21 | # |
| 22 | |
| 23 | # |
| 24 | # This creates the x.y.z-container.yaml file for releasing a docker image. |
| 25 | # It should be executed from somewhere within the "git" repo to be |
| 26 | # released. Assumes the following: |
| 27 | # - the latest commit is at the top of the "git log" |
| 28 | # - the branch to be released is currently checked out |
| 29 | # - the latest maven-docker-stage jenkins job is the one to be released |
| 30 | # - the defaultbranch within the .gitreview file is set to the |
| 31 | # branch to be released |
| 32 | # |
| 33 | # This uses xmllint, which is part of the libxml2-utils package. |
| 34 | # |
| 35 | # If behind a firewall, then http_proxy must be set so that curl |
| 36 | # can get through the firewall. |
| 37 | # |
| 38 | |
| 39 | if [ $# -lt 1 -o "$1" = "-?" ] |
| 40 | then |
| 41 | echo "arg(s): docker-container-name1 docker-container-name2 ..." >&2 |
| 42 | exit 1 |
| 43 | fi |
| 44 | |
| 45 | TOPDIR=$(git rev-parse --show-toplevel) |
| 46 | if [ -z "${TOPDIR}" ]; then |
| 47 | echo "cannot determine top of 'git' repo" >&2 |
| 48 | exit 1 |
| 49 | fi |
| 50 | |
| 51 | BRANCH=$(awk -F= '$1 == "defaultbranch" { print $2 }' "${TOPDIR}/.gitreview") |
| 52 | if [ -z "${BRANCH}" ]; then |
| 53 | echo "cannot extract default branch from ${TOPDIR}/.gitreview" >&2 |
| 54 | exit 1 |
| 55 | fi |
| 56 | echo Branch: ${BRANCH} |
| 57 | |
| 58 | PROJECT=$(awk -F= '$1 == "project" { print $2 }' "${TOPDIR}/.gitreview" | |
| 59 | sed 's/.git$//') |
| 60 | if [ -z "${PROJECT}" ]; then |
| 61 | echo "cannot extract project from ${TOPDIR}/.gitreview" >&2 |
| 62 | exit 1 |
| 63 | fi |
| 64 | echo Project: ${PROJECT} |
| 65 | TPROJ=$(echo ${PROJECT} | sed 's!/!%2F!') |
| 66 | DPROJ=$(echo ${PROJECT} | sed 's!/!-!') |
| 67 | |
| 68 | RELEASE=$( |
| 69 | xmllint --xpath \ |
| 70 | '/*[local-name()="project"]/*[local-name()="version"]/text()' \ |
| 71 | "${TOPDIR}/pom.xml" | |
| 72 | sed 's!-SNAPSHOT!!' |
| 73 | ) |
| 74 | if [ -z "${RELEASE}" ]; then |
| 75 | echo "cannot extract release from ${TOPDIR}/pom.xml" >&2 |
| 76 | exit 1 |
| 77 | fi |
| 78 | echo Release: ${RELEASE} |
| 79 | |
| 80 | REF_ID=$(git log | grep commit | head -1 | awk '{ print $2 }') |
| 81 | if [ -z "${REF_ID}" ]; then |
| 82 | echo "cannot extract ref from 'git log'" >&2 |
| 83 | exit 1 |
| 84 | fi |
| 85 | echo Ref: ${REF_ID} |
| 86 | |
| 87 | prefix='https://jenkins.onap.org/view/policy/job/' |
| 88 | STAGE_ID=$( |
| 89 | curl --silent ${prefix}${DPROJ}-maven-docker-stage-${BRANCH}/ | |
| 90 | grep "Last completed build" | |
| 91 | sed -e 's!.*Last completed build .#!!' -e 's!).*!!' | |
| 92 | head -1 |
| 93 | ) |
| 94 | if [ -z "${STAGE_ID}" ]; then |
| 95 | echo "cannot extract last docker stage ID from jenkins" >&2 |
| 96 | exit 1 |
| 97 | fi |
| 98 | STAGE_ID=${DPROJ}-maven-docker-stage-${BRANCH}/${STAGE_ID} |
| 99 | echo Stage ID: ${STAGE_ID} |
| 100 | |
| 101 | prefix='https://jenkins.onap.org/view/policy/job/' |
| 102 | JOB_OUT=$(curl --silent ${prefix}${STAGE_ID}/console) |
| 103 | echo "${JOB_OUT}" | grep -q "Finished: SUCCESS" |
| 104 | if [ $? -ne 0 ]; then |
| 105 | echo "last docker build has not completed successfully" >&2 |
| 106 | exit 1 |
| 107 | fi |
| 108 | |
| 109 | echo Creating ${TOPDIR}/releases/${RELEASE}-container.yaml |
| 110 | cat >"${TOPDIR}/releases/${RELEASE}-container.yaml" <<EOT |
| 111 | distribution_type: 'container' |
| 112 | container_release_tag: '${RELEASE}' |
| 113 | project: '${DPROJ}' |
| 114 | log_dir: '${STAGE_ID}' |
| 115 | ref: ${REF_ID} |
| 116 | containers: |
| 117 | EOT |
| 118 | |
| 119 | for CONT_NAME in "$@" |
| 120 | do |
| 121 | VERSION=$( |
| 122 | echo "${JOB_OUT}" | |
| 123 | awk " |
| 124 | /Successfully tagged onap/ { found = 0 } |
| 125 | /Successfully tagged onap\/${CONT_NAME}:/ { found = 1 } |
| 126 | found == 1 && /Tag with/ { print } |
| 127 | " | |
| 128 | head -1 | |
| 129 | sed 's!.*Tag with!!' | |
| 130 | cut -d, -f2 |
| 131 | ) |
| 132 | if [ -z "${VERSION}" ]; then |
| 133 | echo "cannot extract ${CONT_NAME} version from jenkins build output" >&2 |
| 134 | exit 1 |
| 135 | fi |
| 136 | echo ${CONT_NAME} version: ${VERSION} |
| 137 | |
| 138 | cat >>"${TOPDIR}/releases/${RELEASE}-container.yaml" <<EOT_LOOP |
| 139 | - name: '${CONT_NAME}' |
| 140 | version: '${VERSION}' |
| 141 | EOT_LOOP |
| 142 | done |