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