2022-04-09 13:55:14 +00:00
|
|
|
#!/bin/bash
|
2022-04-16 02:52:43 +00:00
|
|
|
# ci.sh: Helper script to automate deployment operations on CI/CD
|
2022-04-09 13:55:14 +00:00
|
|
|
# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-04-16 06:23:49 +00:00
|
|
|
set -xEeuo pipefail
|
2022-04-16 05:56:22 +00:00
|
|
|
#source $(pwd)/scripts/lib.sh
|
2022-04-09 13:55:14 +00:00
|
|
|
|
2022-04-16 02:52:43 +00:00
|
|
|
readonly SSH_ID_FILE=/tmp/ci-ssh-id
|
2022-04-16 06:23:49 +00:00
|
|
|
readonly SSH_REMOTE_NAME=origin-ssh
|
2022-04-09 13:55:14 +00:00
|
|
|
|
2022-04-16 05:56:22 +00:00
|
|
|
match_arg() {
|
|
|
|
if [ $1 == $2 ] || [ $1 == $3 ]
|
|
|
|
then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-04-09 13:55:14 +00:00
|
|
|
help() {
|
|
|
|
cat << EOF
|
2022-04-16 02:52:43 +00:00
|
|
|
USAGE: ci.sh [SUBCOMMAND]
|
|
|
|
Helper script to automate deployment operations on CI/CD
|
|
|
|
|
|
|
|
Subcommands
|
|
|
|
|
|
|
|
-c --clean cleanup secrets, SSH key and other runtime data
|
|
|
|
-i --init <SSH_PRIVATE_KEY> initialize environment, write SSH private to file
|
|
|
|
-d --deploy <PAGES-SECRET> <TARGET BRANCH> push branch to Gitea and call Pages server
|
|
|
|
-h --help print this help menu
|
2022-04-09 13:55:14 +00:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2022-04-16 02:52:43 +00:00
|
|
|
# $1: SSH private key
|
|
|
|
write_ssh(){
|
2022-04-16 09:55:51 +00:00
|
|
|
truncate --size 0 $SSH_ID_FILE
|
|
|
|
echo "$1" > $SSH_ID_FILE
|
2022-04-16 09:38:01 +00:00
|
|
|
chmod 600 $SSH_ID_FILE
|
2022-04-16 02:52:43 +00:00
|
|
|
}
|
2022-04-09 13:55:14 +00:00
|
|
|
|
2022-04-16 06:23:49 +00:00
|
|
|
set_ssh_remote() {
|
|
|
|
http_remote_url=$(git remote get-url origin)
|
|
|
|
remote_hostname=$(echo $http_remote_url | cut -d '/' -f 3)
|
|
|
|
repository_owner=$(echo $http_remote_url | cut -d '/' -f 4)
|
|
|
|
repository_name=$(echo $http_remote_url | cut -d '/' -f 5)
|
|
|
|
ssh_remote="git@$remote_hostname:$repository_owner/$repository_name"
|
2022-04-16 07:58:27 +00:00
|
|
|
ssh_remote="git@gitea.hostea.org:Hostea/website.git"
|
|
|
|
git remote add $SSH_REMOTE_NAME $ssh_remote
|
2022-04-16 06:23:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 02:52:43 +00:00
|
|
|
clean() {
|
|
|
|
if [ -f $SSH_ID_FILE ]
|
|
|
|
then
|
|
|
|
shred $SSH_ID_FILE
|
|
|
|
rm $SSH_ID_FILE
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# $1: Pages API secret
|
|
|
|
# $2: Deployment target branch
|
|
|
|
deploy() {
|
|
|
|
if (( "$#" < 2 ))
|
|
|
|
then
|
|
|
|
help
|
|
|
|
else
|
2022-04-16 09:28:52 +00:00
|
|
|
git -c core.sshCommand="/usr/bin/ssh -oStrictHostKeyChecking=no -i $SSH_ID_FILE"\
|
2022-04-16 07:36:39 +00:00
|
|
|
push --force $SSH_REMOTE_NAME $2
|
2022-04-16 02:52:43 +00:00
|
|
|
curl -vv --location --request \
|
2022-04-16 07:34:51 +00:00
|
|
|
POST "http://hostea.org:5000/api/v1/update"\
|
2022-04-16 02:52:43 +00:00
|
|
|
--header 'Content-Type: application/json' \
|
2022-04-16 07:34:51 +00:00
|
|
|
--data-raw "{ \"secret\": \"$1\", \"branch\": \"$2\" }"
|
2022-04-16 02:52:43 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
if (( "$#" < 1 ))
|
|
|
|
then
|
|
|
|
help
|
|
|
|
exit -1
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
if match_arg $1 '-i' '--init'
|
|
|
|
then
|
|
|
|
if (( "$#" < 2 ))
|
|
|
|
then
|
|
|
|
help
|
|
|
|
exit -1
|
|
|
|
fi
|
2022-04-16 06:23:49 +00:00
|
|
|
set_ssh_remote
|
2022-04-16 07:07:18 +00:00
|
|
|
write_ssh "$2"
|
2022-04-16 02:52:43 +00:00
|
|
|
elif match_arg $1 '-c' '--clean'
|
|
|
|
then
|
|
|
|
clean
|
|
|
|
elif match_arg $1 '-d' '--deploy'
|
|
|
|
then
|
|
|
|
if (( "$#" < 3 ))
|
|
|
|
then
|
|
|
|
help
|
|
|
|
exit -1
|
|
|
|
fi
|
|
|
|
deploy $2 $3
|
|
|
|
elif match_arg $1 '-h' '--help'
|
2022-04-09 13:55:14 +00:00
|
|
|
then
|
|
|
|
help
|
|
|
|
else
|
2022-04-16 02:52:43 +00:00
|
|
|
help
|
2022-04-09 13:55:14 +00:00
|
|
|
fi
|