-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsetup-local-dev-repos.sh
executable file
·136 lines (116 loc) · 5.65 KB
/
setup-local-dev-repos.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
echo "Configure the repos for Innerloop dev"
TEST_REPO_ORG="${MY_TEST_REPO_ORG:-redhat-appstudio}"
TEST_REPO_GITLAB_ORG="${MY_TEST_REPO_GITLAB_ORG:-redhat-appstudio}"
if [ $TEST_REPO_ORG == "redhat-appstudio" ]; then
echo "Build can use redhat-appstudio directly, gitops needs a forked repo."
echo "Set up a github and gitlab fork in github and gitlab "
echo "Pass org names in MY_TEST_REPO_ORG and MY_TEST_REPO_GITLAB_ORG"
fi
if [ $TEST_REPO_GITLAB_ORG == "redhat-appstudio" ]; then
echo "WARNING: Gitops may not use redhat-appstudio, gitops needs a forked repo."
echo "Set up a github and gitlab fork in github and gitlab "
echo "Pass org names in MY_TEST_REPO_ORG and MY_TEST_REPO_GITLAB_ORG"
fi
UPSTREAM_BUILD_REPO=https://github.com/redhat-appstudio/devfile-sample-nodejs-dance
UPSTREAM_GITOPS_REPO=https://github.com/redhat-appstudio/tssc-dev-gitops
# this will be copied into a temp directory
# pipelines will be pushed into it for local test
# build and gitops on gitlab
TEST_BUILD_REPO=https://github.com/$TEST_REPO_ORG/devfile-sample-nodejs-dance
# These repos are all optional, if they don't exist, the build can continue
TEST_GITOPS_REPO=https://github.com/$TEST_REPO_ORG/tssc-dev-gitops
TEST_BUILD_GITLAB_REPO=https://gitlab.com/$TEST_REPO_GITLAB_ORG/devfile-sample-nodejs-dance
TEST_GITOPS_GITLAB_REPO=https://gitlab.com/$TEST_REPO_GITLAB_ORG/tssc-dev-gitops
TEST_BUILD_JENKINS_REPO=https://github.com/$TEST_REPO_ORG/tssc-dev-source-jenkins
TEST_GITOPS_JENKINS_REPO=https://github.com/$TEST_REPO_ORG/tssc-dev-gitops-jenkins
# If you prefer to use ssh git urls
if [ -n "$USE_SSH_GIT_URLS" ]; then
[email protected]:$TEST_REPO_ORG/devfile-sample-nodejs-dance.git
[email protected]:$TEST_REPO_ORG/tssc-dev-gitops.git
[email protected]:$TEST_REPO_GITLAB_ORG/devfile-sample-nodejs-dance.git
[email protected]:$TEST_REPO_GITLAB_ORG/tssc-dev-gitops.git
[email protected]:$TEST_REPO_ORG/tssc-dev-source-jenkins.git
[email protected]:$TEST_REPO_ORG/tssc-dev-gitops-jenkins.git
fi
function cloneRepo() {
UPSTREAM_REPO=$1
REPO=$2
REPO_HTTPS=$3
# $REPO and $REPO_HTTPS are the same unless $USE_SSH_GIT_URLS is set
DEST=$4
# Assume $REPO_HTTPS is like "https://github.com/org/repo"
# and use cut to pull out the "org/repo" part
ORG_AND_REPO=$(echo "$REPO_HTTPS" | cut -d/ -f4,5)
# Clone from the "uptream" repo
echo "Clone from $UPSTREAM_REPO into $DEST"
git clone --quiet $UPSTREAM_REPO $DEST > /dev/null
REPO_EXISTS=$(curl -s -o /dev/null -I -w "%{http_code}" $REPO_HTTPS)
# 200 == exists
# 404 == does not exist
if [ $REPO_EXISTS == 200 ]; then
# Test "fork" repo exists
true
else
# Test "fork" repo does not exist
if [[ $REPO_HTTPS =~ github.com ]]; then
# Create it
echo Creating repo at $REPO_HTTPS
gh repo create "$ORG_AND_REPO" --public
else
# Ask user to create it
# (Todo: Create it using GitLab's REST API)
echo Please manually create a repo at $REPO_HTTPS
echo "(Configure to allow force push to main)"
exit 1
fi
fi
# Remove the original "upstream" remote and add a new one
pushd $DEST
echo "Updating origin to $REPO"
git remote set-url origin "$REPO"
# Force push so we start from a known state
echo "Force pushing to main branch"
git push --quiet -f origin main:main
popd
}
# clone if repos exist, disable gitops
# Github in build/gitops
# Gitlab in gitlab-build, gitlab-gitops
# Jenkins in jenkins-build, jenkins-gitops
TMP_REPOS=tmp
rm -rf $TMP_REPOS/*
BUILD=$TMP_REPOS/build
GITOPS=$TMP_REPOS/gitops
GITLAB_BUILD=$TMP_REPOS/gitlab-build
GITLAB_GITOPS=$TMP_REPOS/gitlab-gitops
JENKINS_BUILD=$TMP_REPOS/jenkins-build
JENKINS_GITOPS=$TMP_REPOS/jenkins-gitops
# Change this for public or private image testing
export TEST_PRIVATE_REGISTRY=${TEST_PRIVATE_REGISTRY:-true}
if [[ "$TEST_PRIVATE_REGISTRY" == "true" ]]; then
echo "Note, private image being built by ci-test, acs disabled"
IMAGE_TO_BUILD=quay.io/$MY_QUAY_USER/private-image
else
IMAGE_TO_BUILD=quay.io/$MY_QUAY_USER/bootstrap
fi
cloneRepo $UPSTREAM_BUILD_REPO ${TEST_BUILD_REPO_SSH:-$TEST_BUILD_REPO} $TEST_BUILD_REPO $BUILD
cloneRepo $UPSTREAM_GITOPS_REPO ${TEST_GITOPS_REPO_SSH:-$TEST_GITOPS_REPO} $TEST_GITOPS_REPO $GITOPS
cloneRepo $UPSTREAM_BUILD_REPO ${TEST_BUILD_GITLAB_REPO_SSH:-$TEST_BUILD_GITLAB_REPO} $TEST_BUILD_GITLAB_REPO $GITLAB_BUILD
cloneRepo $UPSTREAM_GITOPS_REPO ${TEST_GITOPS_GITLAB_REPO_SSH:-$TEST_GITOPS_GITLAB_REPO} $TEST_GITOPS_GITLAB_REPO $GITLAB_GITOPS
cloneRepo $UPSTREAM_BUILD_REPO ${TEST_BUILD_JENKINS_REPO_SSH:-$TEST_BUILD_JENKINS_REPO} $TEST_BUILD_JENKINS_REPO $JENKINS_BUILD
cloneRepo $UPSTREAM_GITOPS_REPO ${TEST_GITOPS_JENKINS_REPO_SSH:-$TEST_GITOPS_JENKINS_REPO} $TEST_GITOPS_JENKINS_REPO $JENKINS_GITOPS
# Avoid messing with your real $HOME/.gitconfig when running locally
if [ -n "$LOCAL_SHELL_RUN" ]; then
export GIT_CONFIG_GLOBAL=$(pwd)/$TMP_REPOS/.gitconfig
echo GIT_CONFIG_GLOBAL=$GIT_CONFIG_GLOBAL
fi
# WARNING - if GITOPS_REPO_URL is set, update deployment will try to update
# the gitops repo. Disable this here if the gitops repo is NOT a fork
if [ $TEST_REPO_ORG == "redhat-appstudio" ]; then
echo "------------- INFO-------------"
echo "DISABLING GITOPS REPO UPDATE for the "redhat-appstudio" org"
echo "This prevents random accidental updates to the shared repo by dev team members with access"
export DISABLE_GITOPS_UPDATE=true
echo "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
echo
fi