-
Notifications
You must be signed in to change notification settings - Fork 10
/
travis_ci_operator.sh
114 lines (106 loc) · 5.06 KB
/
travis_ci_operator.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
_install_travis_ci_operator() {
ls -alh "${GITHUB_WORKSPACE}/bin/travis_ci_operator.sh"
chmod +x "${GITHUB_WORKSPACE}/bin/travis_ci_operator.sh"
}
_install_script() {
if [ -e "${1}" ]; then cp "${1}" "${GITHUB_WORKSPACE}/bin/${1}"
else curl -L "https://raw.githubusercontent.com/OriHoch/travis-ci-operator/master/${1}" > "${GITHUB_WORKSPACE}/bin/${1}"
fi && chmod +x "${GITHUB_WORKSPACE}/bin/${1}"
ls -alh "${GITHUB_WORKSPACE}/bin/${1}"
}
if [ "${1}" == "init" ]; then
GITHUB_WORKSPACE=${2}
_install_travis_ci_operator &&\
_install_script read_yaml.py &&\
_install_script update_yaml.py &&\
if [ -e .travis.banner ]; then cat .travis.banner; else curl -L https://raw.githubusercontent.com/OriHoch/travis-ci-operator/master/.travis.banner; fi &&\
echo Successfully initialized travis-ci-operator && exit 0
echo Failed to initialize travis-ci-operator && exit 1
elif [ "${1}" == "docker-login" ]; then
docker login -u "${DOCKER_USER}" -p "${DOCKER_PASSWORD}" &&\
echo Logged in to Docker && exit 0
echo failed to login to Docker && exit 1
elif [ "${1}" == "github-update" ]; then
DEPLOY_KEY_NAME="${2}"
GIT_BRANCH="${3}"
UPDATE_SCRIPT="${4}"
COMMIT_MSG="${5}"
GITHUB_WORKSPACE="${6}"
if [ "${DEPLOY_KEY_NAME}" == "self" ]; then
GITHUB_REPO_SLUG="${TRAVIS_REPO_SLUG}"
else
GITHUB_REPO_SLUG="${6}"
fi
[ -z "${DEPLOY_KEY_NAME}" ] || [ -z "${GIT_BRANCH}" ] || [ -z "${UPDATE_SCRIPT}" ] || [ -z "${COMMIT_MSG}" ] \
&& echo missing required arguments && exit 1
[ "${DEPLOY_KEY_NAME}" == "self" ] && [ "${COMMIT_MSG}" == "${TRAVIS_COMMIT_MESSAGE}" ] && [ "${GIT_BRANCH}" == "${TRAVIS_BRANCH}" ] \
&& echo skipping update of self with same commit msg and branch && exit 0
[ -z "${GITHUB_REPO_SLUG}" ] && echo missing GITHUB_REPO_SLUG && exit 1
ls -alh /home/runner/bin/read_yaml.py
! $(eval echo `python ${GITHUB_WORKSPACE}/bin/read_yaml.py ${GITHUB_WORKSPACE}/bin/.travis-ci-operator.yaml ${DEPLOY_KEY_NAME}DeployKeyDecryptCmd`) \
&& echo Failed to get deploy key && exit 1
GITHUB_DEPLOY_KEY_FILE=".travis_ci_operator_${DEPLOY_KEY_NAME}_github_deploy_key.id_rsa"
if [ -e "${GITHUB_DEPLOY_KEY_FILE}" ]; then
cp -f "${GITHUB_DEPLOY_KEY_FILE}" ~/.ssh/id_rsa && chmod 400 ~/.ssh/id_rsa
[ "$?" != "0" ] && echo failed to setup deploy key for pushing to GitHub && exit 1
else
echo WARNING: deploy key file not found
fi
GIT_REPO="[email protected]:${GITHUB_REPO_SLUG}.git"
TEMPDIR=`mktemp -d`
echo Cloning git repo ${GIT_REPO} branch ${GIT_BRANCH}
! git clone --branch ${GIT_BRANCH} ${GIT_REPO} ${TEMPDIR} && echo failed to clone repo && exit 1
pushd $TEMPDIR
eval "${UPDATE_SCRIPT}"
echo Committing and pushing to GitHub repo ${GIT_REPO} branch ${GIT_BRANCH}
git commit -m "${COMMIT_MSG}" && ! git push ${GIT_REPO} ${GIT_BRANCH} \
&& echo failed to push change to GitHub && exit 1
popd
rm -rf $TEMPDIR
echo GitHub update completed successfully
exit 0
elif [ "${1}" == "github-yaml-update" ]; then
DEPLOY_KEY_NAME="${2}"
GIT_BRANCH="${3}"
YAML_FILE="${4}"
UPDATE_VALUES="${5}"
COMMIT_MSG="${6}"
if [ "${DEPLOY_KEY_NAME}" == "self" ]; then
GITHUB_REPO_SLUG="${TRAVIS_REPO_SLUG}"
else
GITHUB_REPO_SLUG="${7}"
fi
[ -z "${DEPLOY_KEY_NAME}" ] || [ -z "${GIT_BRANCH}" ] || [ -z "${YAML_FILE}" ] || [ -z "${UPDATE_VALUES}" ] || [ -z "${COMMIT_MSG}" ] \
&& echo missing required arguments && exit 1
[ "${DEPLOY_KEY_NAME}" == "self" ] && [ "${COMMIT_MSG}" == "${TRAVIS_COMMIT_MESSAGE}" ] && [ "${GIT_BRANCH}" == "${TRAVIS_BRANCH}" ] \
&& echo skipping update of self with same commit msg and branch && exit 0
[ -z "${GITHUB_REPO_SLUG}" ] && echo missing GITHUB_REPO_SLUG && exit 1
! $(eval echo `read_yaml.py .travis-ci-operator.yaml ${DEPLOY_KEY_NAME}DeployKeyDecryptCmd`) \
&& echo Failed to get deploy key && exit 1
GITHUB_DEPLOY_KEY_FILE=".travis_ci_operator_${DEPLOY_KEY_NAME}_github_deploy_key.id_rsa"
if [ -e "${GITHUB_DEPLOY_KEY_FILE}" ]; then
cp -f "${GITHUB_DEPLOY_KEY_FILE}" ~/.ssh/id_rsa && chmod 400 ~/.ssh/id_rsa
[ "$?" != "0" ] && echo failed to setup deploy key for pushing to GitHub && exit 1
else
echo WARNING: deploy key file not found
fi
GIT_REPO="[email protected]:${GITHUB_REPO_SLUG}.git"
TEMPDIR=`mktemp -d`
echo Cloning git repo ${GIT_REPO} branch ${GIT_BRANCH}
! git clone --branch ${GIT_BRANCH} ${GIT_REPO} ${TEMPDIR} && echo failed to clone repo && exit 1
pushd $TEMPDIR
! update_yaml.py "${UPDATE_VALUES}" "${YAML_FILE}" \
&& echo failed to update yaml file && exit 1
echo Committing and pushing to GitHub repo ${GIT_REPO} branch ${GIT_BRANCH}
git add "${YAML_FILE}"
git commit -m "${COMMIT_MSG}" && ! git push ${GIT_REPO} ${GIT_BRANCH} \
&& echo failed to push change to GitHub && exit 1
popd
rm -rf $TEMPDIR
echo GitHub yaml update completed successfully
exit 0
else
echo unknown command
exit 1
fi