dfbee9f8fff277dd8411489f4a603e818393285b
[policy/parent.git] / integration / src / release_scripts / mkdock.sh
1 #!/bin/bash
2
3 #
4 # ============LICENSE_START================================================
5 # ONAP
6 # =========================================================================
7 # Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
8 # Modifications Copyright (C) 2022 Nordix Foundation.
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
40 set -e
41
42 if [ $# -lt 1 -o "$1" = "-?" ]
43 then
44     echo "arg(s): docker-container-name1 docker-container-name2 ..." >&2
45     exit 1
46 fi
47
48 TOPDIR=$(git rev-parse --show-toplevel)
49 if [ -z "${TOPDIR}" ]; then
50     echo "cannot determine top of 'git' repo" >&2
51     exit 1
52 fi
53
54 BRANCH=$(awk -F= '$1 == "defaultbranch" { print $2 }' "${TOPDIR}/.gitreview")
55 if [ -z "${BRANCH}" ]; then
56     echo "cannot extract default branch from ${TOPDIR}/.gitreview" >&2
57     exit 1
58 fi
59 echo Branch: ${BRANCH}
60
61 PROJECT=$(awk -F= '$1 == "project" { print $2 }' "${TOPDIR}/.gitreview" |
62             sed 's/.git$//')
63 if [ -z "${PROJECT}" ]; then
64     echo "cannot extract project from ${TOPDIR}/.gitreview" >&2
65     exit 1
66 fi
67 echo Project: ${PROJECT}
68 TPROJ=$(echo ${PROJECT} | sed 's!/!%2F!')
69 DPROJ=$(echo ${PROJECT} | sed 's!/!-!')
70
71 RELEASE=$(
72     xmllint --xpath \
73         '/*[local-name()="project"]/*[local-name()="version"]/text()' \
74         "${TOPDIR}/pom.xml" |
75     sed 's!-SNAPSHOT!!'
76     )
77 if [ -z "${RELEASE}" ]; then
78     echo "cannot extract release from ${TOPDIR}/pom.xml" >&2
79     exit 1
80 fi
81 echo Release: ${RELEASE}
82
83 REF_ID=$(git log | grep commit | head -1 | awk '{ print $2 }')
84 if [ -z "${REF_ID}" ]; then
85     echo "cannot extract ref from 'git log'" >&2
86     exit 1
87 fi
88 echo Ref: ${REF_ID}
89
90 prefix='https://jenkins.onap.org/view/policy/job/'
91 STAGE_ID=$(
92     curl --silent ${prefix}${DPROJ}-maven-docker-stage-${BRANCH}/ |
93     grep "Last completed build" |
94     sed -e 's!.*Last completed build .#!!' -e 's!).*!!' |
95     head -1
96     )
97 if [ -z "${STAGE_ID}" ]; then
98     echo "cannot extract last docker stage ID from jenkins" >&2
99     exit 1
100 fi
101 STAGE_ID=${DPROJ}-maven-docker-stage-${BRANCH}/${STAGE_ID}
102 echo Stage ID: ${STAGE_ID}
103
104 prefix='https://jenkins.onap.org/view/policy/job/'
105 JOB_OUT=$(curl --silent ${prefix}${STAGE_ID}/console)
106 echo "${JOB_OUT}" | grep -q "Finished: SUCCESS"
107 if [ $? -ne 0 ]; then
108     echo "last docker build has not completed successfully" >&2
109     exit 1
110 fi
111
112 echo Creating ${TOPDIR}/releases/${RELEASE}-container.yaml
113 cat >"${TOPDIR}/releases/${RELEASE}-container.yaml" <<EOT
114 distribution_type: 'container'
115 container_release_tag: '${RELEASE}'
116 project: '${DPROJ}'
117 log_dir: '${STAGE_ID}'
118 ref: ${REF_ID}
119 containers:
120 EOT
121
122 for CONT_NAME in "$@"
123 do
124     VERSION=$(
125         echo "${JOB_OUT}" |
126         awk "
127             /Successfully tagged onap/ { found = 0 }
128             /Successfully tagged onap\/${CONT_NAME}:/ { found = 1 }
129             found == 1 && /Tag with/ { print }
130         " |
131         head -1 |
132         sed 's!.*Tag with!!' |
133         cut -d, -f2
134         )
135     if [ -z "${VERSION}" ]; then
136         echo "cannot extract ${CONT_NAME} version from jenkins build output" >&2
137         exit 1
138     fi
139     echo ${CONT_NAME} version: ${VERSION}
140
141     cat >>"${TOPDIR}/releases/${RELEASE}-container.yaml" <<EOT_LOOP
142     - name: '${CONT_NAME}'
143       version: '${VERSION}'
144 EOT_LOOP
145 done