diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0b99357 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +* +*./* +*/* +*.* +!*.sh \ No newline at end of file diff --git a/.gitignore b/.gitignore index 1c36090..c925794 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .history -.vscode \ No newline at end of file +.vscode +*.log \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index af66730..b1ad660 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,9 @@ FROM alpine:3.10 -COPY LICENSE README.md / +RUN apk upgrade --update \ + && apk add --update openssh \ + && rm -rf /tmp/* /usr/share/man /var/cache/apk/* -COPY entrypoint.sh /entrypoint.sh +COPY . / ENTRYPOINT ["/entrypoint.sh"] diff --git a/README.md b/README.md index 6c58c7c..f1c1ace 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,16 @@ -# SCP Deploy action +# SSH-SCP Deploy action Action to send dist files to a remote server with scp command ## Required params -- `src`: Sorce dir to deploy +- `src`: Source dir to deploy - `host`: SSH address - `remote`: Remote dir path - `port`: SSH Port - `user`: SSH User name - `key`: Private key +- `options` : Extra options for scp ### To publish @@ -59,4 +60,4 @@ jobs: port: ${{ secrets.SSH_PORT }} user: ${{ secrets.SSH_USER }} key: ${{ secrets.SSH_KEY }} -``` \ No newline at end of file +``` diff --git a/action.yml b/action.yml index 9b9ab20..afb5784 100644 --- a/action.yml +++ b/action.yml @@ -1,29 +1,35 @@ -name: 'scp-deploy' -description: 'Action to send dist files to a remote server with scp command' -author: 'Fabricio Nogueira' +name: 'SCP Deploy Smart' +description: 'Action to send dist files to a remote server with scp command' +author: 'Fabricio Nogueira & Jackz' inputs: src: - description: 'Sorce dir to deploy' + description: 'Source dir to deploy' required: true host: description: 'SSH address' required: true remote: - description: 'Remote dir path' + description: 'base remote folder' required: true port: description: 'SSH Port' required: false - default: 22 + default: "22" user: description: 'SSH User name' required: true key: description: 'Private key' required: true + options: + description: 'Extra options for scp' + required: false + extract: + description: 'Is src a tar file to be extracted?' + required: false runs: using: 'docker' image: 'Dockerfile' branding: icon: 'upload-cloud' - color: 'blue' \ No newline at end of file + color: 'blue' diff --git a/entrypoint.sh b/entrypoint.sh index cc1f436..380c690 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,7 +1,31 @@ #!/bin/sh -l - +set -e echo -e "${INPUT_KEY}" >__TEMP_INPUT_KEY_FILE +set -x chmod 600 __TEMP_INPUT_KEY_FILE -scp -o StrictHostKeyChecking=no -v -i __TEMP_INPUT_KEY_FILE -P "${INPUT_PORT}" -r ${INPUT_SRC} "${INPUT_USER}"@"${INPUT_HOST}":"${INPUT_REMOTE}" +COMMIT_SHA=$(cat ${INPUT_SRC}/.git-commit) +if [[ -z "$COMMIT_SHA" ]]; then + echo "COMMIT_SHA: Could not find file ${INPUT_SRC}/.git-commit" + exit 1 +fi +echo "SSH Host: ${INPUT_USER}@${INPUT_HOST}:${INPUT_PORT}" +echo "Local Path: ${INPUT_SRC}" +echo "Remote Path: ${INPUT_REMOTE}" +echo "Commit: ${COMMIT_SHA}" + +if [[ -z "$INPUT_EXTRACT"]]; then +# SCP normal files +scp -o StrictHostKeyChecking=no -v -i __TEMP_INPUT_KEY_FILE \ + -P "${INPUT_PORT}" $INPUT_OPTIONS -r ${INPUT_SRC} "${INPUT_USER}"@"${INPUT_HOST}":"${INPUT_REMOTE}/${COMMIT_SHA}" +elif +# Untar directly via SSh +ssh -o StrictHostKeyChecking=no -v -i __TEMP_INPUT_KEY_FILE \ + "${INPUT_USER}"@"${INPUT_HOST}" -p "${INPUT_PORT}" \ + -C "mkdir ${INPUT_REMOTE}/${COMMIT_SHA} && cd ${INPUT_REMOTE}/${COMMIT_SHA} && tar -xvv" < $INPUT_SRC +fi +echo "File transfer complete. Symlinking and purging old entries" +ssh -o StrictHostKeyChecking=no -v -i __TEMP_INPUT_KEY_FILE \ + "${INPUT_USER}"@"${INPUT_HOST}" -p "${INPUT_PORT}" \ + -C "ln -s ${INPUT_REMOTE}/${COMMIT_SHA} ${INPUT_REMOTE}/latest && find ${INPUT_REMOTE} -mindepth 1 -maxdepth 1 -type d -not -path './latest' | tail -n +5 | xargs --no-run-if-empty rm -r"