Skip to content

Commit

Permalink
Merge pull request #10 from WilliamMayor/feature/pass-shellcheck
Browse files Browse the repository at this point in the history
Pass shellcheck plus small other things (4/4)
  • Loading branch information
vitalyliber authored May 28, 2020
2 parents 4b7c6e4 + c70b731 commit 57b7fd8
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/tests/_run.sh
/tests/*.log
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 11 additions & 10 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
#!/bin/sh

set -e

echo "Setting up SSH directory"
SSH_PATH="$HOME/.ssh"
mkdir -p "$SSH_PATH"
chmod 700 "$SSH_PATH"

DEPLOY_BRANCH="${BRANCH-master}"
FORCE=$([ "$FORCE_DEPLOY" = true ] && echo "--force" || echo "")

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"
GIT_COMMAND="$GIT_COMMAND --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
GIT_SSH_COMMAND="$GIT_SSH_COMMAND -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
echo "Disabling host key checking"
GIT_SSH_COMMAND="$GIT_SSH_COMMAND -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_COMMAND
11 changes: 11 additions & 0 deletions tests/force_deploy.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion tests/host_key_fails.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down
1 change: 0 additions & 1 deletion tests/host_key_skipped.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down
1 change: 0 additions & 1 deletion tests/host_key_works.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down
38 changes: 23 additions & 15 deletions tests/run.sh
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
#!/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
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")
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
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"

0 comments on commit 57b7fd8

Please sign in to comment.