From bbfda9deef0b7d3ff0138dd9feb55c4e1c4f3045 Mon Sep 17 00:00:00 2001 From: Koen <koen@pop-os.localdomain> Date: Fri, 7 Jun 2024 09:07:52 +0200 Subject: [PATCH] Added CI/CD. --- .gitlab-ci.yml | 9 +++++++++ build/deploy.sh | 1 - deploy.sh | 25 +++++++++++++++++++++++++ sign.sh | 29 +++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 .gitlab-ci.yml delete mode 100644 build/deploy.sh create mode 100644 deploy.sh create mode 100644 sign.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..62c4c07 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,9 @@ +stages: +- deploy + +deploy: + stage: deploy + script: + - sh deploy.sh + only: + - master diff --git a/build/deploy.sh b/build/deploy.sh deleted file mode 100644 index 96b4b06..0000000 --- a/build/deploy.sh +++ /dev/null @@ -1 +0,0 @@ -#!/bin/sh \ No newline at end of file diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..ad878cd --- /dev/null +++ b/deploy.sh @@ -0,0 +1,25 @@ +#!/bin/sh +DOCUMENT_ROOT=/var/www/sources + +# Take site offline +echo "Taking site offline..." +touch $DOCUMENT_ROOT/maintenance.file + +# Swap over the content +echo "Deploying content..." +mkdir -p $DOCUMENT_ROOT/Spotify +cp build/SpotifyIcon.png $DOCUMENT_ROOT/Spotify +cp build/SpotifyConfig.json $DOCUMENT_ROOT/Spotify +cp build/SpotifyScript.js $DOCUMENT_ROOT/Spotify +sh sign.sh $DOCUMENT_ROOT/Spotify/SpotifyScript.js $DOCUMENT_ROOT/Spotify/SpotifyConfig.json + +# Notify Cloudflare to wipe the CDN cache +echo "Purging Cloudflare cache for zone $CLOUDFLARE_ZONE_ID..." +curl -X POST "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/purge_cache" \ + -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ + -H "Content-Type: application/json" \ + --data '{"files":["https://plugins.grayjay.app/Spotify/SpotifyIcon.png", "https://plugins.grayjay.app/Spotify/SpotifyConfig.json", "https://plugins.grayjay.app/Spotify/SpotifyScript.js"]}' + +# Take site back online +echo "Bringing site back online..." +rm $DOCUMENT_ROOT/maintenance.file \ No newline at end of file diff --git a/sign.sh b/sign.sh new file mode 100644 index 0000000..7cf0905 --- /dev/null +++ b/sign.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +# Parameters +JS_FILE_PATH=$1 +CONFIG_FILE_PATH=$2 + +# Decode and save the private key to a temporary file +echo "$SIGNING_PRIVATE_KEY" | base64 -d > tmp_private_key.pem + +# Validate private key +if ! openssl rsa -check -noout -in tmp_private_key.pem > /dev/null 2>&1; then + echo "Invalid private key." + rm tmp_private_key.pem + exit 1 +fi + +# Generate signature for the provided JS file +SIGNATURE=$(cat $JS_FILE_PATH | openssl dgst -sha512 -sign tmp_private_key.pem | base64 -w 0) + +# Extract public key from the temporary private key file +PUBLIC_KEY=$(openssl rsa -pubout -outform DER -in tmp_private_key.pem 2>/dev/null | openssl pkey -pubin -inform DER -outform PEM | tail -n +2 | head -n -1 | tr -d '\n') + +echo "PUBLIC_KEY: $PUBLIC_KEY" + +# Remove temporary key files +rm tmp_private_key.pem + +# Update "scriptSignature" and "scriptPublicKey" fields in Config JSON +cat $CONFIG_FILE_PATH | jq --arg signature "$SIGNATURE" --arg publicKey "$PUBLIC_KEY" '. + {scriptSignature: $signature, scriptPublicKey: $publicKey}' > temp_config.json && mv temp_config.json $CONFIG_FILE_PATH \ No newline at end of file -- GitLab