added script

This commit is contained in:
Asher 2025-03-02 10:32:37 +00:00
commit bc6178471e
3 changed files with 92 additions and 0 deletions

3
.env.example Normal file
View File

@ -0,0 +1,3 @@
CD_HOST="127.0.0.1"
CD_USER="asher"
CD_PATH="/home/ubuntu/infra/"

23
readme.md Normal file
View File

@ -0,0 +1,23 @@
## compose-deploy
A simple script to update the docker compose file on a remote host and restart the containers.
I created this script to suit my personal requirements, which were:
* authenticate with the remote host using ssh keys
* use a git repository to store the file and pull on the host
* having one docker compose file for all the services
If you want to use it, create a .env file and add the following
The host of the remote server
`CD_HOST=1.1.1.1`
The username for the remote server
`CD_USER=asher`
To run the script, simply execute the script, it uses [gum](https://github.com/charmbracelet/gum) for an interactive prompt for the commit message
`./redeploy.sh`
Make sure your private key is in the repo and please, please make sure it is in your gitignore.

66
redeploy.sh Executable file
View File

@ -0,0 +1,66 @@
#!/bin/bash
# A script to redeploy this docker infrastructure using ssh
if [ -f .env ]; then
source .env
else
echo ".env file not found. Please create it and set the necessary environment variables."
exit 1
fi
check_gum() {
if command -v gum &> /dev/null; then
return 0
else
echo "Gum not found, installing..."
return 1
fi
}
install_gum() {
if [[ "$OSTYPE" == "darwin"* ]]; then
/bin/bash -c "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install gum
elif [[ -f /etc/os-release ]]; then
source /etc/os-release
if [[ $ID == "ubuntu" ]] || [[ $ID == "debian" ]]; then
sudo apt update
sudo apt install -y gum
elif [[ $ID == "fedora" ]] || [[ $ID == "centos" ]] || [[ $ID == "rhel" ]]; then
sudo dnf install -y gum
else
echo "Unsupported Linux distribution. Please install Gum
manually."
exit 1
fi
else
echo "Unsupported operating system. Please install Gum manually."
exit 1
fi
}
# Main script execution
if ! check_gum; then
if ! install_gum; then
echo "Failed to install Gum. Please install it manually and run
the script again."
exit 1
fi
fi
message=$(gum input --placeholder "Commit message")
git add .
git commit -m "${message}"
git push
ssh -o "StrictHostKeyChecking=no" -i ssh.key $CD_USER@$CD_HOST << EOF
sudo su
cd $CD_PATH
docker compose down
git pull
docker compose up -d
exit
EOF