Added Drone CI/CD pipelines (for Github sync)

This commit is contained in:
Fabio Manganiello 2025-02-28 00:17:08 +01:00
parent e68fd8303c
commit 86a27a07e0
Signed by: blacklight
GPG key ID: D90FBA7F76362774
4 changed files with 109 additions and 0 deletions

21
.drone.yml Normal file
View file

@ -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

24
.drone/github-mirror.sh Executable file
View file

@ -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

29
.drone/macros/configure-git.sh Executable file
View file

@ -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"

35
.drone/macros/configure-ssh.sh Executable file
View file

@ -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