Rework creating tags & branches from release files
[releng.git] / jjb / releng / releng-release-create-branch.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2018 The Linux Foundation and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 set -xe
11
12 GIT_URL=${GIT_URL:-https://gerrit.opnfv.org/gerrit}
13 STREAM=${STREAM:-'nostream'}
14 RELEASE_FILES=$(git diff HEAD^1 --name-only -- "releases/$STREAM")
15
16 # Configure the git user/email as we'll be pushing up changes
17 git config --global user.name "jenkins-ci"
18 git config --global user.email "jenkins-opnfv-ci@opnfv.org"
19
20 # Ensure we are able to generate Commit-IDs for new patchsets
21 curl -kLo .git/hooks/commit-msg https://gerrit.opnfv.org/gerrit/tools/hooks/commit-msg
22 chmod +x .git/hooks/commit-msg
23
24 clone_repo(){
25 echo "--> Cloning $repo"
26 if [ ! -d $repo ]; then
27     git clone $GIT_URL/$repo.git $repo
28 fi
29 }
30
31 check_if_ref_exists(){
32 clone_repo
33 cd "$repo"
34 if git rev-list refs/heads/master | grep "$ref"; then
35   echo "$ref exists"
36   REF_EXISTS=true
37   cd -
38 else
39   echo "$ref Does not exist please submit a valid ref for branching"
40   exit 1
41 fi
42 }
43
44 run_merge(){
45 unset NEW_FILES
46 if [[ $REF_EXISTS = true && "$JOB_NAME" =~ "merge" ]]; then
47   ssh -n -f -p 29418 gerrit.opnfv.org gerrit create-branch "$repo" "$branch" "$ref"
48   python releases/scripts/create_jobs.py -f $release_file
49   NEW_FILES=$(git status --porcelain --untracked=no | cut -c4-)
50 fi
51 if [ -n "$NEW_FILES" ]; then
52   git add $NEW_FILES
53   git commit -sm "Create Stable Branch Jobs for $(basename $release_file .yaml)"
54   git push origin HEAD:refs/for/master
55 fi
56 }
57
58 main(){
59 for release_file in $RELEASE_FILES; do
60     while read -r repo branch ref; do
61         echo "$repo" "$branch" "$ref"
62         branches="$(git ls-remote "https://gerrit.opnfv.org/gerrit/$repo.git" "refs/heads/$branch")"
63         if ! [ -z "$branches" ]; then
64             echo "refs/heads/$branch already exists at $ref ($branches)"
65         else
66             run_merge
67         fi
68     done < <(python releases/scripts/repos.py -b -f "$release_file")
69 done
70 }
71
72 check_if_ref_exists
73 main