blob: cd34fd1a1bbb3c524d62ca42f65578acbf281e84 [file] [log] [blame]
liamfallon5780bdf2021-11-29 13:26:27 +00001#!/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
39if [ $# -lt 1 -o "$1" = "-?" ]
40then
41 echo "arg(s): docker-container-name1 docker-container-name2 ..." >&2
42 exit 1
43fi
44
45TOPDIR=$(git rev-parse --show-toplevel)
46if [ -z "${TOPDIR}" ]; then
47 echo "cannot determine top of 'git' repo" >&2
48 exit 1
49fi
50
51BRANCH=$(awk -F= '$1 == "defaultbranch" { print $2 }' "${TOPDIR}/.gitreview")
52if [ -z "${BRANCH}" ]; then
53 echo "cannot extract default branch from ${TOPDIR}/.gitreview" >&2
54 exit 1
55fi
56echo Branch: ${BRANCH}
57
58PROJECT=$(awk -F= '$1 == "project" { print $2 }' "${TOPDIR}/.gitreview" |
59 sed 's/.git$//')
60if [ -z "${PROJECT}" ]; then
61 echo "cannot extract project from ${TOPDIR}/.gitreview" >&2
62 exit 1
63fi
64echo Project: ${PROJECT}
65TPROJ=$(echo ${PROJECT} | sed 's!/!%2F!')
66DPROJ=$(echo ${PROJECT} | sed 's!/!-!')
67
68RELEASE=$(
69 xmllint --xpath \
70 '/*[local-name()="project"]/*[local-name()="version"]/text()' \
71 "${TOPDIR}/pom.xml" |
72 sed 's!-SNAPSHOT!!'
73 )
74if [ -z "${RELEASE}" ]; then
75 echo "cannot extract release from ${TOPDIR}/pom.xml" >&2
76 exit 1
77fi
78echo Release: ${RELEASE}
79
80REF_ID=$(git log | grep commit | head -1 | awk '{ print $2 }')
81if [ -z "${REF_ID}" ]; then
82 echo "cannot extract ref from 'git log'" >&2
83 exit 1
84fi
85echo Ref: ${REF_ID}
86
87prefix='https://jenkins.onap.org/view/policy/job/'
88STAGE_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 )
94if [ -z "${STAGE_ID}" ]; then
95 echo "cannot extract last docker stage ID from jenkins" >&2
96 exit 1
97fi
98STAGE_ID=${DPROJ}-maven-docker-stage-${BRANCH}/${STAGE_ID}
99echo Stage ID: ${STAGE_ID}
100
101prefix='https://jenkins.onap.org/view/policy/job/'
102JOB_OUT=$(curl --silent ${prefix}${STAGE_ID}/console)
103echo "${JOB_OUT}" | grep -q "Finished: SUCCESS"
104if [ $? -ne 0 ]; then
105 echo "last docker build has not completed successfully" >&2
106 exit 1
107fi
108
109echo Creating ${TOPDIR}/releases/${RELEASE}-container.yaml
110cat >"${TOPDIR}/releases/${RELEASE}-container.yaml" <<EOT
111distribution_type: 'container'
112container_release_tag: '${RELEASE}'
113project: '${DPROJ}'
114log_dir: '${STAGE_ID}'
115ref: ${REF_ID}
116containers:
117EOT
118
119for CONT_NAME in "$@"
120do
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}'
141EOT_LOOP
142done