diff options
-rw-r--r-- | config.sh | 4 | ||||
-rwxr-xr-x | dscip | 68 | ||||
-rwxr-xr-x | post.sh | 2 | ||||
-rwxr-xr-x | pre.sh | 2 |
4 files changed, 52 insertions, 24 deletions
diff --git a/config.sh b/config.sh index 8da5c81..c78e95e 100644 --- a/config.sh +++ b/config.sh @@ -7,7 +7,7 @@ DSCIP_GITREPO="https://www.example.com/example/example.git" DSCIP_GITMODE="clone" # Branch to check # DSCIP_BRANCH="master" -WORKING_DIRECTORY="." +WORKING_DIRECTORY="$(dirname $(realpath $0))" # Commands to run before building. # DSCIP_PRE_CMD="$WORKING_DIRECTORY/pre.sh" # Commands to run to build program. # @@ -21,4 +21,4 @@ DSCIP_SLEEP="60" # How many seconds before the daemon re-runs itself. # # etc # DSCIP_DISREGARD_COMMIT_CHECK="false" # If the script should just rebuild even # # if upstream has not updated. # -DSCIP_OUTPUT_TO="/dev/stdout" # Output to file, default is stdout. +DSCIP_OUTPUT_TO="$WORKING_DIRECTORY/output.log" # Output to file, default is stdout. diff --git a/dscip b/dscip index 8d8b33c..db7726e 100755 --- a/dscip +++ b/dscip @@ -17,53 +17,77 @@ ################################################################################ set -e -. "$(dirname "$0")/config.sh" +. "$(dirname "$(realpath "$0")")/config.sh" ################################################################################ +# Move script to background if DSCIP_DAEMON_FORK is set to true # +if [ "$DSCIP_DAEMON" = "true" ]; then + if [ "$DSCIP_DAEMON_FORK" = "true" ] && [ ! "$FORKED" = "true" ]; then + FORKED=true nohup "$0" "$@" > /dev/null 2>&1 + exit 0 + fi +fi + # Check if script is currently running, and if not, reset LOCK # if [ -f "$WORKING_DIRECTORY/LOCK" ]; then LOCKED_PID=$(cat "$WORKING_DIRECTORY/LOCK") if ps -p "$LOCKED_PID"; then - echo "Script still running. Exiting..." 2>> "$DSCIP_OUTPUT_TO" + echo "Script still running. Exiting..." exit 0 else - echo "Script not running, resetting lock..." 2>> "$DSCIP_OUTPUT_TO" + echo "Script not running, resetting lock..." rm "$WORKING_DIRECTORY/LOCK" fi fi echo "$$" > "$WORKING_DIRECTORY/LOCK" -if [ "$DSCIP_DAEMON" = "true" ]; then - if [ "$DSCIP_DAEMON_FORK" = "true" ] && [ ! "$FORKED" = "true" ]; then - FORKED=true nohup "$0" "$@" > /dev/null 2>&1 - exit 0 - fi -fi +# Print variables +{ +echo "################################################################################"; +date; +echo "################################################################################"; +echo "DSCIP_GITREPO=$DSCIP_GITREPO"; +echo "DSCIP_GITMODE=$DSCIP_GITMODE"; +echo "DSCIP_BRANCH=$DSCIP_BRANCH"; +echo "WORKING_DIRECTORY=$WORKING_DIRECTORY"; +echo "DSCIP_PRE_CMD=$DSCIP_PRE_CMD"; +echo "DSCIP_BUILD_CMD=$DSCIP_BUILD_CMD"; +echo "DSCIP_POST_CMD=$DSCIP_POST_CMD"; +echo "DSCIP_DAEMON=$DSCIP_DAEMON"; +echo "DSCIP_DAEMON_FORK=$DSCIP_DAEMON_FORK"; +echo "DSCIP_SLEEP=$DSCIP_SLEEP"; +echo "DSCIP_DISREGARD_COMMIT_CHECK=$DSCIP_DISREGARD_COMMIT_CHECK"; +echo "DSCIP_OUTPUT_TO=$DSCIP_OUTPUT_TO" +} >> "$DSCIP_OUTPUT_TO" build () { if [ "$DSCIP_GITMODE" = "clone" ]; then rm -rf "$WORKING_DIRECTORY/wrkdir" # Clean Up # - git clone -b "$DSCIP_BRANCH" "$DSCIP_GITREPO" wrkdir 2>> "$DSCIP_OUTPUT_TO" # Clone git # + git clone -b "$DSCIP_BRANCH" "$DSCIP_GITREPO" wrkdir >> "$DSCIP_OUTPUT_TO" 2>&1 # Clone git # elif [ "$DSCIP_GITMODE" = "pull" ]; then if [ ! -d "$WORKING_DIRECTORY/wrkdir" ]; then - git clone -b "$DSCIP_BRANCH" "$DSCIP_GITREPO" wrkdir 2>> "$DSCIP_OUTPUT_TO" + git clone -b "$DSCIP_BRANCH" "$DSCIP_GITREPO" wrkdir >> "$DSCIP_OUTPUT_TO" 2>&1 fi cd wrkdir - git pull 2>> "$DSCIP_OUTPUT_TO" + git pull >> "$DSCIP_OUTPUT_TO" 2>&1 else echo "Invalid GITMODE, choose either 'clone' or 'pull'" 2>> "$DSCIP_OUTPUT_TO" exit 1 fi - cd "$WORKING_DIRECTORY/wrkdir" - $DSCIP_PRE_CMD 2>> "$DSCIP_OUTPUT_TO" - cd "$WORKING_DIRECTORY/wrkdir" - $DSCIP_BUILD_CMD 2>> "$DSCIP_OUTPUT_TO" - cd "$WORKING_DIRECTORY/wrkdir" - $DSCIP_POST_CMD 2>> "$DSCIP_OUTPUT_TO" - mkdir -p "$WORKING_DIRECTORY/meta" - echo "$CURRENT_COMMIT" > "$WORKING_DIRECTORY/meta/LAST_COMMIT" # Save commit # + { + cd "$WORKING_DIRECTORY/wrkdir"; + echo "Running pre-build commands..."; + $DSCIP_PRE_CMD; + echo "Running build commands..."; + cd "$WORKING_DIRECTORY/wrkdir"; + $DSCIP_BUILD_CMD; + echo "Running post-build commands..."; + cd "$WORKING_DIRECTORY/wrkdir"; + $DSCIP_POST_CMD; + } >> "$DSCIP_OUTPUT_TO" 2>&1 + echo "$CURRENT_COMMIT" > "$WORKING_DIRECTORY/LAST_COMMIT" # Save commit # } run () { @@ -71,7 +95,7 @@ run () { cd "$WORKING_DIRECTORY" # Loads last commit hash if it exists. # - if [ -f "$WORKING_DIRECTORY/meta/LAST_COMMIT" ]; then + if [ -f "$WORKING_DIRECTORY/LAST_COMMIT" ]; then LAST_COMMIT="$(cat "$WORKING_DIRECTORY/meta/LAST_COMMIT")" fi @@ -90,7 +114,7 @@ run () { build fi rm "$WORKING_DIRECTORY/LOCK" # Remove lock # - echo "Done." 2>> "$DSCIP_OUTPUT_TO" + printf "Done.\n\n" >> "$DSCIP_OUTPUT_TO" } run diff --git a/post.sh b/post.sh index d085dcb..1d048f9 100755 --- a/post.sh +++ b/post.sh @@ -1,3 +1,5 @@ #!/bin/sh # Execute commands after building. Like pushing to an FTP server. # + +exit 0 diff --git a/pre.sh b/pre.sh index 5941ffa..0d614ed 100755 --- a/pre.sh +++ b/pre.sh @@ -1,3 +1,5 @@ #!/bin/sh # Execute commands before building. # + +exit 0 |