From 8a9dfcb1d339249e39de2cd3ebc4e1b0fb0ceafa Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 16 Apr 2022 08:22:43 +0530 Subject: [PATCH] feat: deploy with git push to Gitea --- scripts/ci.sh | 78 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 68 insertions(+), 10 deletions(-) diff --git a/scripts/ci.sh b/scripts/ci.sh index ba25b35..691d2ae 100755 --- a/scripts/ci.sh +++ b/scripts/ci.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Used in CI workflow: call Pages hook to deploy update +# ci.sh: Helper script to automate deployment operations on CI/CD # Copyright © 2022 Aravinth Manivannan # # This program is free software: you can redistribute it and/or modify @@ -18,23 +18,81 @@ set -Eeuo pipefail source $(pwd)/scripts/lib.sh - +readonly SSH_ID_FILE=/tmp/ci-ssh-id help() { cat << EOF -ci.sh: Call Pages webhook to update repository -USAGE: - ci.sh +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 initialize environment, write SSH private to file + -d --deploy push branch to Gitea and call Pages server + -h --help print this help menu EOF } +# $1: SSH private key +write_ssh(){ + echo $1 > $SSH_ID_FILE +} -if (( "$#" < 2 )) +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 + git push -c core.sshCommand="/usr/bin/ssh -i $SSH_ID_FILE" origin $2 + curl -vv --location --request \ + POST "https://hostea.org:5000/api/v1/update"\ + --header 'Content-Type: application/json' \ + - + agate --content dist/content/ \ + -data-raw "{ \"secret\": \"$1\", \"branch\": \"$2\" }" + fi +} + +if (( "$#" < 1 )) +then + help + exit -1 +fi + + +if match_arg $1 '-i' '--init' +then + if (( "$#" < 2 )) + then + help + exit -1 + fi + write_ssh $2 +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' then help else - curl -vv --location --request \ - POST "https://hostea.org:5000/api/v1/update"\ - --header 'Content-Type: application/json' \ - --data-raw "{ \"secret\": \"$1\", \"branch\": \"$2\" }" + help fi