From 16ff0db80fd0b24fe36a931274e51ff987f6679f Mon Sep 17 00:00:00 2001 From: William Mayor Date: Sat, 16 May 2020 13:14:35 +0100 Subject: [PATCH 1/7] Remove branch env var, use HEAD MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I think we can rely on the GitHub checkout action to set the HEAD to be the commit that the user wants to deploy. Even if they want to deploy a different branch to the one that triggered the action, they can still do this by running the checkout action with the right arguments. This removes the git checkout line from this script too, so I think it’ll make it faster? --- README.md | 1 - entrypoint.sh | 6 +----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/README.md b/README.md index 5717fa6..961b508 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,6 @@ You'll need to provide some env to use the action. - **HOST**: The host the action will SSH to run the git push command. ie, `your.site.com`. - **PROJECT**: The project is Dokku project name. -- **BRANCH**: Repository branch that should be used for deploy, `master` is set by default. - **PORT**: Port of the sshd listen to, `22` is set by default. ### Optional Environments diff --git a/entrypoint.sh b/entrypoint.sh index 4301dca..0507bcd 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,12 +1,10 @@ #!/bin/sh - set -e SSH_PATH="$HOME/.ssh" mkdir -p "$SSH_PATH" chmod 700 "$SSH_PATH" -DEPLOY_BRANCH="${BRANCH-master}" FORCE=$([ "$FORCE_DEPLOY" = true ] && echo "--force" || echo "") echo "$PRIVATE_KEY" > "$SSH_PATH/deploy_key" @@ -23,8 +21,6 @@ else GIT_SSH_COMMAND="$GIT_SSH_COMMAND -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" fi -git checkout $DEPLOY_BRANCH - echo "The deploy is starting" -GIT_SSH_COMMAND="$GIT_SSH_COMMAND" git push dokku@$HOST:$PROJECT $DEPLOY_BRANCH:master $FORCE +GIT_SSH_COMMAND="$GIT_SSH_COMMAND" git push dokku@$HOST:$PROJECT HEAD:master $FORCE From 92d83eaa41e7191db5b91c9ab6e56cdcef6f46e0 Mon Sep 17 00:00:00 2001 From: William Mayor Date: Sat, 16 May 2020 13:21:43 +0100 Subject: [PATCH 2/7] Quote vars and refactor force deploy --- entrypoint.sh | 8 ++++---- tests/force_deploy.sh | 11 +++++++++++ tests/run.sh | 1 + 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100755 tests/force_deploy.sh diff --git a/entrypoint.sh b/entrypoint.sh index 0507bcd..6392b80 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -5,15 +5,15 @@ SSH_PATH="$HOME/.ssh" mkdir -p "$SSH_PATH" chmod 700 "$SSH_PATH" -FORCE=$([ "$FORCE_DEPLOY" = true ] && echo "--force" || echo "") - echo "$PRIVATE_KEY" > "$SSH_PATH/deploy_key" chmod 600 "$SSH_PATH/deploy_key" +if [ -n "$FORCE_DEPLOY" ]; then + FORCE_DEPLOY="--force" +fi GIT_SSH_COMMAND="ssh -p ${PORT-22} -i $SSH_PATH/deploy_key" - if [ -n "$HOST_KEY" ]; then echo "$HOST_KEY" >> "$SSH_PATH/known_hosts" chmod 600 "$SSH_PATH/known_hosts" @@ -23,4 +23,4 @@ fi echo "The deploy is starting" -GIT_SSH_COMMAND="$GIT_SSH_COMMAND" git push dokku@$HOST:$PROJECT HEAD:master $FORCE +GIT_SSH_COMMAND="$GIT_SSH_COMMAND" git push "dokku@$HOST:$PROJECT" HEAD:master "$FORCE_DEPLOY" diff --git a/tests/force_deploy.sh b/tests/force_deploy.sh new file mode 100755 index 0000000..6bc569f --- /dev/null +++ b/tests/force_deploy.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +docker run \ + --env "BRANCH=$BRANCH" \ + --env "PRIVATE_KEY=$PRIVATE_KEY" \ + --env "HOST=$HOST" \ + --env "PROJECT=$PROJECT" \ + --env "FORCE_DEPLOY=1" \ + --workdir "/repo" \ + --volume "$LOCAL_REPO:/repo" \ + dokku-github-action diff --git a/tests/run.sh b/tests/run.sh index 18ff17e..ac822e8 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -32,3 +32,4 @@ export HOST_KEY ./tests/host_key_fails.sh ./tests/host_key_skipped.sh ./tests/host_key_works.sh +./tests/force_deploy.sh From 9ef6570372de032df226f65498e8e963bfe00ec8 Mon Sep 17 00:00:00 2001 From: William Mayor Date: Sat, 16 May 2020 13:22:02 +0100 Subject: [PATCH 3/7] Add debug log lines --- entrypoint.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/entrypoint.sh b/entrypoint.sh index 6392b80..9faef1e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,23 +1,28 @@ #!/bin/sh set -e +echo "Setting up SSH directory" SSH_PATH="$HOME/.ssh" mkdir -p "$SSH_PATH" chmod 700 "$SSH_PATH" +echo "Saving SSH key" echo "$PRIVATE_KEY" > "$SSH_PATH/deploy_key" chmod 600 "$SSH_PATH/deploy_key" if [ -n "$FORCE_DEPLOY" ]; then + echo "Enabling force deploy" FORCE_DEPLOY="--force" fi GIT_SSH_COMMAND="ssh -p ${PORT-22} -i $SSH_PATH/deploy_key" if [ -n "$HOST_KEY" ]; then + echo "Adding hosts key to known_hosts" echo "$HOST_KEY" >> "$SSH_PATH/known_hosts" chmod 600 "$SSH_PATH/known_hosts" else + echo "Disabling host key checking" GIT_SSH_COMMAND="$GIT_SSH_COMMAND -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" fi From 8df8973f95ddd363d3afd1013477020d01f05f26 Mon Sep 17 00:00:00 2001 From: William Mayor Date: Sat, 16 May 2020 13:46:42 +0100 Subject: [PATCH 4/7] Remove public key config from tests --- tests/host_key_fails.sh | 1 - tests/host_key_skipped.sh | 1 - tests/host_key_works.sh | 1 - tests/run.sh | 4 ---- 4 files changed, 7 deletions(-) diff --git a/tests/host_key_fails.sh b/tests/host_key_fails.sh index 2da2558..59cc580 100755 --- a/tests/host_key_fails.sh +++ b/tests/host_key_fails.sh @@ -3,7 +3,6 @@ docker run \ --env "BRANCH=$BRANCH" \ --env "PRIVATE_KEY=$PRIVATE_KEY" \ - --env "PUBLIC_KEY=$PUBLIC_KEY" \ --env "HOST=$HOST" \ --env "HOST_KEY=123" \ --env "PROJECT=$PROJECT" \ diff --git a/tests/host_key_skipped.sh b/tests/host_key_skipped.sh index d45d812..37589b0 100755 --- a/tests/host_key_skipped.sh +++ b/tests/host_key_skipped.sh @@ -3,7 +3,6 @@ docker run \ --env "BRANCH=$BRANCH" \ --env "PRIVATE_KEY=$PRIVATE_KEY" \ - --env "PUBLIC_KEY=$PUBLIC_KEY" \ --env "HOST=$HOST" \ --env "PROJECT=$PROJECT" \ --workdir "/repo" \ diff --git a/tests/host_key_works.sh b/tests/host_key_works.sh index 74b1f26..0aa7f44 100755 --- a/tests/host_key_works.sh +++ b/tests/host_key_works.sh @@ -3,7 +3,6 @@ docker run \ --env "BRANCH=$BRANCH" \ --env "PRIVATE_KEY=$PRIVATE_KEY" \ - --env "PUBLIC_KEY=$PUBLIC_KEY" \ --env "HOST=$HOST" \ --env "HOST_KEY=$HOST_KEY" \ --env "PROJECT=$PROJECT" \ diff --git a/tests/run.sh b/tests/run.sh index ac822e8..57c609e 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -20,10 +20,6 @@ export LOCAL_REPO= PRIVATE_KEY= PRIVATE_KEY=$(cat $PRIVATE_KEY) export PRIVATE_KEY -# An absolute path to the public key for the above private key -PUBLIC_KEY= -PUBLIC_KEY=$(cat $PUBLIC_KEY) -export PUBLIC_KEY # This grabs the current host key for HOST HOST_KEY=$(ssh-keyscan -t rsa "$HOST") From be2b72478ab07e34528f32207bef53f41696d12a Mon Sep 17 00:00:00 2001 From: William Mayor Date: Sat, 16 May 2020 13:49:19 +0100 Subject: [PATCH 5/7] Refactor test runner to save logs and show nicer output --- .gitignore | 1 + tests/run.sh | 35 +++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 0c0bd24..41c1879 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /tests/_run.sh +/tests/*.log diff --git a/tests/run.sh b/tests/run.sh index 57c609e..d6c6ff1 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -1,19 +1,15 @@ #!/bin/sh -# Sets up the environment for running the tests, then runs them. -# Use this as a way to test that the action works in lots of different scenarios +# Tries to deploy to a dokku project using the docker image and entrypoint. +# Use this as a way to test that the action works. # -# Make a copy of this file called _run.sh and fill in the env vars. +# Make a copy of this file called _run.sh and fill in the env vars in the copy. # _run.sh is gitignored so you won't accidently commit your SSH keys. -set -eu - -docker build --tag dokku-github-action . # Set these as you would expect to set them when using the action export HOST= export PROJECT= export BRANCH= - # An absolute path to a git repo on your local machine that can be pushed to dokku export LOCAL_REPO= # An absolute path to the private key that can be used to deploy this project @@ -22,10 +18,25 @@ PRIVATE_KEY=$(cat $PRIVATE_KEY) export PRIVATE_KEY # This grabs the current host key for HOST -HOST_KEY=$(ssh-keyscan -t rsa "$HOST") +HOST_KEY=$(ssh-keyscan -t rsa "$HOST" 2> /dev/null) export HOST_KEY -./tests/host_key_fails.sh -./tests/host_key_skipped.sh -./tests/host_key_works.sh -./tests/force_deploy.sh +runtest() { + echo "Running $1" + PASS_CODE="${2-0}" + "./tests/$1.sh" > "./tests/$1.log" 2>&1 + CODE="$?" + if [ "$CODE" = "$PASS_CODE" ]; then + echo " Passed!" + else + echo " Failed (exit code was $CODE not $PASS_CODE)" + fi +} + +echo "Building docker image" +docker build --tag dokku-github-action . > "./tests/build.log" 2>&1 + +runtest "host_key_fails" 128 +runtest "host_key_skipped" +runtest "host_key_works" +runtest "force_deploy" From db1994148fa073c3b0dca10599940e821c7cba2e Mon Sep 17 00:00:00 2001 From: William Mayor Date: Sat, 16 May 2020 13:49:40 +0100 Subject: [PATCH 6/7] Fix force deploy --- entrypoint.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 9faef1e..20259f2 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -10,9 +10,10 @@ echo "Saving SSH key" echo "$PRIVATE_KEY" > "$SSH_PATH/deploy_key" chmod 600 "$SSH_PATH/deploy_key" +GIT_COMMAND="git push dokku@$HOST:$PROJECT HEAD:master" if [ -n "$FORCE_DEPLOY" ]; then echo "Enabling force deploy" - FORCE_DEPLOY="--force" + GIT_COMMAND="$GIT_COMMAND --force" fi GIT_SSH_COMMAND="ssh -p ${PORT-22} -i $SSH_PATH/deploy_key" @@ -28,4 +29,4 @@ fi echo "The deploy is starting" -GIT_SSH_COMMAND="$GIT_SSH_COMMAND" git push "dokku@$HOST:$PROJECT" HEAD:master "$FORCE_DEPLOY" +GIT_SSH_COMMAND="$GIT_SSH_COMMAND" $GIT_COMMAND From c70b731cd7f226c220c75cecf0a8c4e5b1f2652c Mon Sep 17 00:00:00 2001 From: William Mayor Date: Sat, 16 May 2020 13:55:52 +0100 Subject: [PATCH 7/7] Remove the unknown hosts file option --- entrypoint.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 20259f2..c682500 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -17,14 +17,13 @@ if [ -n "$FORCE_DEPLOY" ]; then fi GIT_SSH_COMMAND="ssh -p ${PORT-22} -i $SSH_PATH/deploy_key" - if [ -n "$HOST_KEY" ]; then echo "Adding hosts key to known_hosts" echo "$HOST_KEY" >> "$SSH_PATH/known_hosts" chmod 600 "$SSH_PATH/known_hosts" else echo "Disabling host key checking" - GIT_SSH_COMMAND="$GIT_SSH_COMMAND -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" + GIT_SSH_COMMAND="$GIT_SSH_COMMAND -o StrictHostKeyChecking=no" fi echo "The deploy is starting"