-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr_script.sh
executable file
·94 lines (76 loc) · 2.33 KB
/
pr_script.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
###
# EXIT CODES
# 0 - success
# 1 - parameter error
###
# Sample environment variables
# REPO_URL=https://github.com/bird-house/birdhouse-deploy
# PROJECT_NAME=birdhouse-deploy
# IMAGE_ID="weaver-worker"
# BUMP_TAG="s/(.*pavics\/canarieapi:).*/\\1NEW_TAG_VALUE/"
# NEW_TAG_VALUE="1.13.2-worker"
# BUMP_FILE="birdhouse/default.env"
# GITHUB_TOKEN=""
REQUIRED_ENV_VARS='
REPO_URL
PROJECT_NAME
IMAGE_ID
BUMP_TAG
NEW_TAG_VALUE
BUMP_FILE
GITHUB_TOKEN
'
# args parsing
for env_var in $REQUIRED_ENV_VARS
do
if [[ ! -v "${env_var}" ]]; then
echo "[ERROR] [$0] Missing ${env_var} environment variable. Exiting."
exit 1
fi
done
WORKING_DIR="working_dir"
echo "[STEP] [$0] [$PROJECT_NAME] PR"
COMMIT_MESSAGE="bump \`${IMAGE_ID}\` to ${NEW_TAG_VALUE}"
BRANCH_NAME="bump_${IMAGE_ID}_to_${NEW_TAG_VALUE}"
# prepare repo
mkdir -p $WORKING_DIR
# rm -rf $WORKING_DIR/$PROJECT_NAME
cd $WORKING_DIR
if [[ ! -d "$PROJECT_NAME" ]]
then
echo "[INFO] Not existing project directory, cloning $PROJECT_ORG_REPO"
hub clone $PROJECT_ORG_REPO
cd $PROJECT_NAME
else
echo "[INFO] Existing project directory, cleaning"
cd $PROJECT_NAME
git clean -fd
git reset --hard origin
git checkout master
hub pull
git branch -D $BRANCH_NAME # TODO : currently avoids already existing branch error. Handle error instead
fi
git checkout -b $BRANCH_NAME
# for testing purpose only, since birdhouse-deploy's default.env doesn't has WEAVER_WORKER_IMAGE variable for now
if [[ ! -z "${TEST_ENV_CONFIG}" ]] && [[ "$BUMP_TAG" == "WEAVER_WORKER_IMAGE" ]]; then
echo "export WEAVER_WORKER_IMAGE=\"pavics/weaver:1.13.1-worker\"" >> $BUMP_FILE
fi
# bumpversion
BUMP_TAG=$BUMP_TAG NEW_TAG_VALUE=$NEW_TAG_VALUE BUMP_FILE=$BUMP_FILE ../../bump_version.sh
# to track last diff result
git diff &> ../../last-diff-result.log
# commit
git add -A
git commit -m "$COMMIT_MESSAGE"
# when in test mode, we can avoid to make a real PR
if [[ ! -z "${EXIT_BEFORE_PR}" ]]; then
echo "[INFO] Exiting before PR, since EXIT_BEFORE_PR is defined. Exiting."
exit 0
fi
# PR
git remote set-url origin https://$GITHUB_TOKEN:[email protected]/$PROJECT_ORG_REPO.git
hub push origin $BRANCH_NAME
hub pull-request -F- <<<"$COMMIT_MESSAGE
* $COMMIT_MESSAGE"
hub pr list -f "%U" -L 1 > ../../$DATA_DIR/last-pr-url.log