From 7abdf4b03cffb8724b022e09ef240a3209e99a38 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Thu, 11 Jan 2024 12:49:33 +0100 Subject: [PATCH] feat: Add a variant using an SSH deploy key --- Dockerfile.ssh | 6 ++++++ entrypoint-ssh.sh | 36 ++++++++++++++++++++++++++++++++++++ ssh_config | 3 +++ 3 files changed, 45 insertions(+) create mode 100644 Dockerfile.ssh create mode 100644 entrypoint-ssh.sh create mode 100644 ssh_config diff --git a/Dockerfile.ssh b/Dockerfile.ssh new file mode 100644 index 0000000..2aadda7 --- /dev/null +++ b/Dockerfile.ssh @@ -0,0 +1,6 @@ +FROM alpine:3.19 +COPY --chmod=555 entrypoint-ssh.sh /bin/entrypoint.sh +COPY --chmod=600 ssh_config /root/.ssh/config +RUN apk add --no-cache git openssh zsh + +ENTRYPOINT ["zsh", "/bin/entrypoint.sh"] \ No newline at end of file diff --git a/entrypoint-ssh.sh b/entrypoint-ssh.sh new file mode 100644 index 0000000..3cb39ce --- /dev/null +++ b/entrypoint-ssh.sh @@ -0,0 +1,36 @@ +#!/bin/zsh +set -e + +branch=${PLUGIN_BRANCH:-"pages"} +token_env=${PLUGIN_TOKEN_ENV} +output=${PLUGIN_OUTPUT} +name=${PLUGIN_NAME} +email=${PLUGIN_EMAIL} +if [[ -z "$token_env" || -z "$branch" || -z "$name" || -z "$email" ]]; then + echo "Invalid configuration: Token environment variable name, branch name empty, name, or email" + exit 1 +fi + +# Setup the SSH directory +mkdir -p ~/.ssh +chown root:root ~/.ssh +chmod 700 ~/.ssh + +# Setup the SSH key +print -rl -- ${(P)token_env} > ~/.ssh/id_rsa +chown root:root ~/.ssh/id_rsa +chmod 600 ~/.ssh/id_rsa + +# Setup git +git config --global user.email "$email" +git config --global user.name "$name" +git config --global --add safe.directory "$CI_WORKSPACE/$CI_REPO_OWNER/$CI_REPO_NAME/$output" + +# Init a new repository in the output directory and add all files +git init -b "$branch" "$output" +cd "$output" +git add --all +git commit -m "Deploy new version $CI_COMMIT_SHA" + +# Push the repository +git push -u "$CI_REPO_CLONE_SSH_URL" -f "$branch" \ No newline at end of file diff --git a/ssh_config b/ssh_config new file mode 100644 index 0000000..f4f7e76 --- /dev/null +++ b/ssh_config @@ -0,0 +1,3 @@ +Host * + StrictHostKeyChecking no + UserKnownHostsFile=/dev/null \ No newline at end of file