From 86a27a07e0fdb9bf672ee406044ea56ee70ddd0f Mon Sep 17 00:00:00 2001 From: Fabio Manganiello <fabio@manganiello.tech> Date: Fri, 28 Feb 2025 00:17:08 +0100 Subject: [PATCH] Added Drone CI/CD pipelines (for Github sync) --- .drone.yml | 21 ++++++++++++++++++++ .drone/github-mirror.sh | 24 +++++++++++++++++++++++ .drone/macros/configure-git.sh | 29 ++++++++++++++++++++++++++++ .drone/macros/configure-ssh.sh | 35 ++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 .drone.yml create mode 100755 .drone/github-mirror.sh create mode 100755 .drone/macros/configure-git.sh create mode 100755 .drone/macros/configure-ssh.sh diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..aee27a8 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,21 @@ +--- +kind: pipeline +type: docker +name: default + +steps: + +### +### Mirror the current repository state to Github +### + +- name: github-mirror + image: alpine + environment: + SSH_PUBKEY: + from_secret: ssh_pubkey + SSH_PRIVKEY: + from_secret: ssh_privkey + + commands: + - . .drone/github-mirror.sh diff --git a/.drone/github-mirror.sh b/.drone/github-mirror.sh new file mode 100755 index 0000000..5a95b25 --- /dev/null +++ b/.drone/github-mirror.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +. .drone/macros/configure-git.sh +. .drone/macros/configure-ssh.sh + +export PROJECT_NAME="gpstracker" +ssh-keyscan github.com >> ~/.ssh/known_hosts 2>/dev/null + +# Clone the repository +branch=$(git rev-parse --abbrev-ref HEAD) +if [ -z "${branch}" ]; then + echo "No branch checked out" + exit 1 +fi + +git remote add github git@github.com:/blacklight/${PROJECT_NAME}.git + +if [[ "$branch" == "main" ]]; then + git pull --rebase github "${branch}" || echo "No such branch on Github" +fi + +# Push the changes to the GitHub mirror +git push -f --all -v github +git push --tags -v github diff --git a/.drone/macros/configure-git.sh b/.drone/macros/configure-git.sh new file mode 100755 index 0000000..ffbbb52 --- /dev/null +++ b/.drone/macros/configure-git.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +# Install git +if [ -z "$(which git)" ]; then + if [ -n "$(which apt-get)" ]; then + apt-get update + apt-get install -y git + elif [ -n "$(which apk)" ]; then + apk add --update --no-cache git + elif [ -n "$(which yum)" ]; then + yum install -y git + elif [ -n "$(which dnf)" ]; then + dnf install -y git + elif [ -n "$(which pacman)" ]; then + pacman -Sy --noconfirm git + else + echo "Could not find a package manager to install git" + exit 1 + fi +fi + +# Backup the original git configuration before changing attributes +export GIT_CONF="$PWD/.git/config" +export TMP_GIT_CONF=/tmp/git.config.orig +cp "$GIT_CONF" "$TMP_GIT_CONF" + +git config --global --add safe.directory "$PWD" +git config user.name "CI/CD Automation" +git config user.email "admin@platypush.tech" diff --git a/.drone/macros/configure-ssh.sh b/.drone/macros/configure-ssh.sh new file mode 100755 index 0000000..1f77c47 --- /dev/null +++ b/.drone/macros/configure-ssh.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +if [ -z "$SSH_PUBKEY" ] || [ -z "$SSH_PRIVKEY" ]; then + echo "SSH_PUBKEY and SSH_PRIVKEY environment variables must be set" + exit 1 +fi + +# Install ssh +if [ -z "$(which ssh)" ]; then + if [ -n "$(which apt-get)" ]; then + apt-get update + apt-get install -y openssh + elif [ -n "$(which apk)" ]; then + apk add --update --no-cache openssh + elif [ -n "$(which yum)" ]; then + yum install -y openssh + elif [ -n "$(which dnf)" ]; then + dnf install -y openssh + elif [ -n "$(which pacman)" ]; then + pacman -Sy --noconfirm openssh + else + echo "Could not find a package manager to install openssh" + exit 1 + fi +fi + +mkdir -p ~/.ssh +echo $SSH_PUBKEY > ~/.ssh/id_rsa.pub + +cat <<EOF > ~/.ssh/id_rsa +$SSH_PRIVKEY +EOF + +chmod 0600 ~/.ssh/id_rsa +ssh-keyscan git.platypush.tech >> ~/.ssh/known_hosts 2>/dev/null