Fatih Degirmenci | 16ccc35 | 2019-03-14 19:18:03 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # ============LICENSE_START======================================================= |
| 4 | # Copyright (C) 2019 The Nordix Foundation. All rights reserved. |
| 5 | # ================================================================================ |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | # SPDX-License-Identifier: Apache-2.0 |
| 19 | # ============LICENSE_END========================================================= |
| 20 | |
| 21 | # |
robert.tomczyk | 591f1b8 | 2019-06-24 09:43:47 +0100 | [diff] [blame] | 22 | # This script will take the single commit form the given Nordix Gerrit review |
| 23 | # and push it, and all its ancestor commit till $GERRIT_BRANCH, to Acumos |
| 24 | # Gerrit. |
Fatih Degirmenci | 16ccc35 | 2019-03-14 19:18:03 -0700 | [diff] [blame] | 25 | # Pre-requisites for script to run successfully: |
robert.tomczyk | 591f1b8 | 2019-06-24 09:43:47 +0100 | [diff] [blame] | 26 | # - The Gerrit username in Nordix needs to match with the Gerrit username in |
| 27 | # Acumos |
| 28 | # - infra public key on build server needs to be added to your users SSH |
| 29 | # Public Keys in Acumos Gerrit |
Fatih Degirmenci | 16ccc35 | 2019-03-14 19:18:03 -0700 | [diff] [blame] | 30 | # |
robert.tomczyk | 591f1b8 | 2019-06-24 09:43:47 +0100 | [diff] [blame] | 31 | set -euxo pipefail |
Fatih Degirmenci | 16ccc35 | 2019-03-14 19:18:03 -0700 | [diff] [blame] | 32 | |
robert.tomczyk | 591f1b8 | 2019-06-24 09:43:47 +0100 | [diff] [blame] | 33 | cd "$WORKSPACE" |
Fatih Degirmenci | 16ccc35 | 2019-03-14 19:18:03 -0700 | [diff] [blame] | 34 | |
robert.tomczyk | 591f1b8 | 2019-06-24 09:43:47 +0100 | [diff] [blame] | 35 | acumos_gerrit_base='gerrit.acumos.org:29418' |
| 36 | nordix_gerrit_rest='https://gerrit.nordix.org' |
Fatih Degirmenci | 16ccc35 | 2019-03-14 19:18:03 -0700 | [diff] [blame] | 37 | |
robert.tomczyk | 591f1b8 | 2019-06-24 09:43:47 +0100 | [diff] [blame] | 38 | echo >&2 "Collecting information about what and how to push towards Acumos" |
Fatih Degirmenci | 16ccc35 | 2019-03-14 19:18:03 -0700 | [diff] [blame] | 39 | |
robert.tomczyk | 591f1b8 | 2019-06-24 09:43:47 +0100 | [diff] [blame] | 40 | # The git hash of the commit in the review this job runs on |
| 41 | commit_hash="$GERRIT_PATCHSET_REVISION" |
Fatih Degirmenci | 16ccc35 | 2019-03-14 19:18:03 -0700 | [diff] [blame] | 42 | |
robert.tomczyk | 591f1b8 | 2019-06-24 09:43:47 +0100 | [diff] [blame] | 43 | # GERRIT_PROJECT is in the form of acumos/<acumos-project> |
| 44 | acumos_project=$(echo "$GERRIT_PROJECT" | cut -d/ -f2- ) |
Fatih Degirmenci | 16ccc35 | 2019-03-14 19:18:03 -0700 | [diff] [blame] | 45 | |
robert.tomczyk | 591f1b8 | 2019-06-24 09:43:47 +0100 | [diff] [blame] | 46 | # Nordix Gerrit can map the email address of the user pushed the change to |
| 47 | # the review to the Gerrit username of the account in Nordix Gerrit. This |
| 48 | # should match with the Gerrit username in the Acumos Gerrit. |
| 49 | username=$( |
| 50 | curl -s -H 'Accept: application/json' \ |
| 51 | "$nordix_gerrit_rest/accounts/?q=email:$GERRIT_EVENT_ACCOUNT_EMAIL&o=DETAILS" \ |
| 52 | | tee /dev/stderr | tail -1 | jq -r '.[0].username') |
| 53 | |
| 54 | acumos_remote_url="ssh://$username@$acumos_gerrit_base/$acumos_project" |
| 55 | |
| 56 | # GERRIT_BRANCH is the _intended_ branch of the commit under review |
| 57 | # E.g. git push HEAD:refs/for/master => GERRIT_BRANCH='master' |
| 58 | # GERRIT_TOPIC is the rest of the refspec after GERRIT_BRANCH when the topic |
| 59 | # is set for the change. |
| 60 | if [ -z "${GERRIT_TOPIC:-}" ] ; then |
| 61 | refspec="$commit_hash:refs/for/$GERRIT_BRANCH" |
Fatih Degirmenci | 16ccc35 | 2019-03-14 19:18:03 -0700 | [diff] [blame] | 62 | else |
robert.tomczyk | 591f1b8 | 2019-06-24 09:43:47 +0100 | [diff] [blame] | 63 | refspec="$commit_hash:refs/for/$GERRIT_BRANCH/$GERRIT_TOPIC" |
Fatih Degirmenci | 16ccc35 | 2019-03-14 19:18:03 -0700 | [diff] [blame] | 64 | fi |
| 65 | |
robert.tomczyk | 591f1b8 | 2019-06-24 09:43:47 +0100 | [diff] [blame] | 66 | echo >&2 "Pushing to Acumos" |
Fatih Degirmenci | 16ccc35 | 2019-03-14 19:18:03 -0700 | [diff] [blame] | 67 | |
robert.tomczyk | 591f1b8 | 2019-06-24 09:43:47 +0100 | [diff] [blame] | 68 | # do not fail if git push fails as in case of no new changes we want to succeed. See below. |
| 69 | set +o pipefail |
| 70 | |
| 71 | git push "$acumos_remote_url" "$refspec" 2>&1 | tee push_result.txt |
| 72 | push_result=${PIPESTATUS[0]} |
| 73 | |
| 74 | # make the job a success if the above git push fails due to no new changes are needed |
| 75 | # to be created upstream. This will be useful when we switch to automatic triggering |
| 76 | # of the push-upstream for each patch set and jobs on different commits in a same chain |
| 77 | # will race with each other to push parts of the chain |
| 78 | if [ $push_result -ne 0 ]; then |
| 79 | grep '(no new changes)' push_result.txt |
Fatih Degirmenci | 16ccc35 | 2019-03-14 19:18:03 -0700 | [diff] [blame] | 80 | fi |