diff --git a/0-DOCS.md b/0-DOCS.md index 55c931c..c27a7af 100644 --- a/0-DOCS.md +++ b/0-DOCS.md @@ -59,6 +59,10 @@ pipeline: - "app.tar.gz.md5" exclude: - "**.*" + prescript: + - cd ~/packages + - md5sum -c app.tar.gz.md5 + - tar -xf app.tar.gz -C ~/app script: - cd ~/packages - md5sum -c app.tar.gz.md5 diff --git a/1-DOCS.md b/1-DOCS.md index e0530c8..9b79998 100644 --- a/1-DOCS.md +++ b/1-DOCS.md @@ -12,7 +12,8 @@ The following parameters are used to configure the plugin: - **exclude** - rsync exclude filter - **recursive** - recursively synchronize, defaults to `false` - **delete** - delete target folder contents, defaults to `false` -- **script** - list of commands to execute on remote machines +- **prescript** - list of commands to execute on remote machines before rsync occurs +- **postscript** - list of commands to execute on remote machines after rsync occurs ## Secrets The following secrets can be used to secure the sensitive parts of your configuration: @@ -73,7 +74,11 @@ steps: - "app.tar.gz.md5" exclude: - "**.*" - script: + prescript: + - cd ~/packages + - md5sum -c app.tar.gz.md5 + - tar -xf app.tar.gz -C ~/app + script: - cd ~/packages - md5sum -c app.tar.gz.md5 - tar -xf app.tar.gz -C ~/app diff --git a/README.md b/README.md index c296860..a7a2b51 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,8 @@ docker run --rm \ -e PLUGIN_HOSTS="127.0.0.1, 127.0.0.2, 127.0.0.3" \ -e PLUGIN_PORTS="22, 23, 24" \ -e PLUGIN_TARGET="./" \ - -e PLUGIN_SCRIPT="echo \"Done!\"" \ + -e PLUGIN_PRESCRIPT="echo \"Prescript Done!\"" \ + -e PLUGIN_SCRIPT="echo \"Postscript Done!\"" \ -e PLUGIN_ARGS="--blocking-io" \ -v $(pwd):$(pwd) \ -w $(pwd) \ diff --git a/upload.sh b/upload.sh index 4aa03db..86feda0 100755 --- a/upload.sh +++ b/upload.sh @@ -8,6 +8,7 @@ if [ -z "$PLUGIN_TARGET" ]; then echo "Specify a target!" exit 1 fi + DEFAULT_PORT=$PLUGIN_PORT if [ -z "$PLUGIN_PORT" ]; then echo "Port not specified, using default port 22!" @@ -100,10 +101,14 @@ fi echo "$SSH_KEY" > $keyfile chmod 0600 $keyfile -# Parse SSH commands function join_with { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; } + +# Parse SSH precommands +IFS=','; read -ra COMMANDS <<< "$PLUGIN_PRESCRIPT" +prescript=$(join_with ' && ' "${COMMANDS[@]}") +# Parse SSH postcommands IFS=','; read -ra COMMANDS <<< "$PLUGIN_SCRIPT" -script=$(join_with ' && ' "${COMMANDS[@]}") +postscript=$(join_with ' && ' "${COMMANDS[@]}") # Run rsync IFS=','; read -ra HOSTS <<< "$PLUGIN_HOSTS" @@ -119,13 +124,21 @@ do PORT=$DEFAULT_PORT fi echo $(printf "%s" "$ $(printf "$expr" "$PORT") $USER@$HOST:$PLUGIN_TARGET ...") + if [ -n "$PLUGIN_PRESCRIPT" ]; then + echo $(printf "%s" "$ ssh -p $PORT $USER@$HOST ...") + echo $(printf "%s" " > $prescript ...") + eval "ssh -p $PORT $USER@$HOST '$prescript'" + result=$(($result+$?)) + echo $(printf "%s" "$ ssh -p $PORT $USER@$HOST result: $?") + if [ "$result" -gt "0" ]; then exit $result; fi + fi eval "$(printf "$expr" "$PORT") $USER@$HOST:$PLUGIN_TARGET" result=$(($result+$?)) if [ "$result" -gt "0" ]; then exit $result; fi if [ -n "$PLUGIN_SCRIPT" ]; then echo $(printf "%s" "$ ssh -p $PORT $USER@$HOST ...") - echo $(printf "%s" " > $script ...") - eval "ssh -p $PORT $USER@$HOST '$script'" + echo $(printf "%s" " > $postscript ...") + eval "ssh -p $PORT $USER@$HOST '$postscript'" result=$(($result+$?)) echo $(printf "%s" "$ ssh -p $PORT $USER@$HOST result: $?") if [ "$result" -gt "0" ]; then exit $result; fi