|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# WordPress SSH Git CI Script |
| 4 | +# v1.0.0 |
| 5 | + |
| 6 | +## ERROR CODES EXPLAINED |
| 7 | +### ERROR 2 = Missing required parameter option. |
| 8 | +### ERROR 3 = Stopping for human intervention. |
| 9 | +### ERROR 4 = Repository needs synced before it can pull changes. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +set_defaults() { |
| 14 | + if [ -z "$GITPATH" ]; then |
| 15 | + print_error_msg "-p is empty. Please provide a path to git directory for repository & try again." |
| 16 | + exit 2 |
| 17 | + fi |
| 18 | + if [ -z "$RUNTYPE" ]; then |
| 19 | + RUNTYPE='push' |
| 20 | + fi |
| 21 | +} |
| 22 | + |
| 23 | +rm_devops_remote() { |
| 24 | + git remote rm devops 2>&1 |
| 25 | + print_status_msg "Remote devops removed" |
| 26 | + git remote -v 2>&1 |
| 27 | + echo "end rm_devops_remote" |
| 28 | +} |
| 29 | + |
| 30 | +add_devops_remote() { |
| 31 | + rm_devops_remote |
| 32 | + git remote add devops "https://${TOKENUSER}:${TOKEN}@${GITREPO}" 2>&1 |
| 33 | + print_status_msg "Remote devops addded" |
| 34 | + git remote -v 2>&1 |
| 35 | +} |
| 36 | + |
| 37 | +merge_from_devops() { |
| 38 | + git merge "${DEVBRANCH}" 2>&1 |
| 39 | + git branch -D "${DEVBRANCH}" 2>&1 |
| 40 | +} |
| 41 | + |
| 42 | +pull_from_devops() { |
| 43 | + add_devops_remote |
| 44 | + git stash clear 2>&1 |
| 45 | + git stash -u 2>&1 |
| 46 | + git remote update 2>&1 |
| 47 | + git checkout "${DEVBRANCH}" 2>&1 |
| 48 | + git pull devops "${DEVBRANCH}" 2>&1 |
| 49 | + git checkout "${BRANCH}" 2>&1 |
| 50 | + git pull devops "${BRANCH}" 2>&1 |
| 51 | + merge_from_devops |
| 52 | + git stash pop 2>&1 |
| 53 | + commit_git |
| 54 | + rm_devops_remote |
| 55 | + print_status_msg "pull_from_dev function was executed successfully." |
| 56 | +} |
| 57 | + |
| 58 | +push_to_devops() { |
| 59 | + # code |
| 60 | + pull_from_devops |
| 61 | + add_devops_remote |
| 62 | + git push devops "$BRANCH" 2>&1 |
| 63 | + print_status_msg "push_to_dev function was executed successfully." |
| 64 | + rm_devops_remote |
| 65 | +} |
| 66 | + |
| 67 | +print_status_msg() { |
| 68 | + printf "\033[32m%s\n" "$1" |
| 69 | +} |
| 70 | + |
| 71 | +print_warning_msg() { |
| 72 | + printf "\033[33m%s\n" "$1" |
| 73 | +} |
| 74 | + |
| 75 | +print_error_msg() { |
| 76 | + printf "\033[31m%s\n" "$1" |
| 77 | +} |
| 78 | + |
| 79 | +commit_git() { |
| 80 | + git add -A . 2>&1 |
| 81 | + git commit -m "Server Side Commit |
| 82 | +
|
| 83 | +This was commited from SiteGround during release push" 2>&1 |
| 84 | +} |
| 85 | + |
| 86 | +TOKEN="NONE" |
| 87 | +TOKENUSER="NONE" |
| 88 | +clean_repository() { |
| 89 | + # code |
| 90 | + if [ -n "$TOKEN" ] || [ "$TOKEN" != "NONE" ] || [ -n "$TOKENUSER" ] || [ "$TOKENUSER" != "NONE" ]; then |
| 91 | + push_to_devops |
| 92 | + commit_git |
| 93 | + push_to_devops |
| 94 | + print_status_msg "clean_rep function was executed successfully." |
| 95 | + else |
| 96 | + print_error_msg "You need to provide a token using -t and a username for it using -u" |
| 97 | + exit 2 |
| 98 | + fi |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +while getopts b:d:g:p:r:t:u: option |
| 103 | +do |
| 104 | +case "${option}" |
| 105 | +in |
| 106 | +b) BRANCH=${OPTARG};; |
| 107 | +d) DEVBRANCH=${OPTARG};; |
| 108 | +g) GITREPO=${OPTARG};; |
| 109 | +p) GITPATH=${OPTARG};; |
| 110 | +r) RUNTYPE=${OPTARG};; |
| 111 | +t) TOKEN=${OPTARG};; |
| 112 | +u) TOKENUSER=${OPTARG};; |
| 113 | +*) RUNTYPE="push";; |
| 114 | +esac |
| 115 | +done |
| 116 | + |
| 117 | +set_defaults |
| 118 | + |
| 119 | +cd "${GITPATH}" || exit 1 |
| 120 | + |
| 121 | +if output=$(git status --untracked-files=no --porcelain) && [ -z "$output" ] && [ "$RUNTYPE" != "Clean" ] && [ "$RUNTYPE" != "clean" ] ; then |
| 122 | + # Working directory clean |
| 123 | + print_status_msg "Working directory clean" |
| 124 | + push_to_devops |
| 125 | +else |
| 126 | + print_warning_msg "Uncommitted changes! Starting to clean..." |
| 127 | + clean_repository |
| 128 | +fi |
| 129 | +export DEFAULT |
0 commit comments