env var handling #39
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to DigitalOcean | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Install doctl | |
uses: digitalocean/action-doctl@v2 | |
with: | |
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} | |
- name: Create VPS if it doesn't exist | |
run: | | |
if ! doctl compute droplet list | grep -q 'mautic-vps'; then | |
doctl compute droplet create mautic-vps --image docker-20-04 --size s-1vcpu-1gb --region nyc1 --ssh-keys ${{ secrets.DIGITALOCEAN_SSH_FINGERPRINT }} --wait --user-data-file setup.sh --enable-monitoring | |
echo "droplet_created=true" >> $GITHUB_ENV | |
else | |
echo "Droplet 'mautic-vps' already exists." | |
echo "droplet_created=true" >> $GITHUB_ENV | |
fi | |
- name: Get VPS IP | |
run: | | |
echo "Waiting for droplet to be ready..." | |
while : ; do | |
echo "." | |
sleep 2 | |
STATUS=$(doctl compute droplet get mautic-vps --format Status --no-header) | |
if [ "$STATUS" = "active" ]; then | |
IP=$(doctl compute droplet get mautic-vps --format PublicIPv4 --no-header) | |
if [ -n "$IP" ]; then | |
echo "Droplet is active. IP address: $IP" | |
break | |
fi | |
fi | |
done | |
echo "ip=$IP" >> $GITHUB_ENV | |
- name: Wait for server to be accessible | |
run: | | |
echo "Waiting for server at ${{ env.ip }} to be accessible..." | |
while : ; do | |
if nc -z ${{ env.ip }} 22; then | |
echo "Server is up and accessible." | |
break | |
else | |
echo "." | |
sleep 2 | |
fi | |
done | |
- name: Deploy to Server | |
uses: easingthemes/ssh-deploy@main | |
env: | |
DOMAIN_NAME: ${{ env.DOMAIN_NAME }} | |
MAUTIC_PORT: ${{ env.MAUTIC_PORT }} | |
EMAIL: ${{ env.EMAIL }} | |
with: | |
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
ARGS: "-rlgoDzvc --delete" | |
SOURCE: "." | |
REMOTE_HOST: ${{ env.ip }} | |
REMOTE_USER: root | |
TARGET: /var/www | |
EXCLUDE: ".git" | |
SCRIPT_BEFORE: | | |
mkdir -p /var/www | |
SCRIPT_AFTER: | | |
echo $RSYNC_STDOUT | |
cd /var/www | |
pwd | |
ls -la | |
docker compose up -d | |
mv nginx-virtual-host ./$DOMAIN_NAME | |
sed -i '' 's/DOMAIN_NAME/'"$DOMAIN_NAME"'/g; s/PORT/'"${MAUTIC_PORT:-8001}"'/g' $DOMAIN_NAME | |
sudo ln -s $DOMAIN_NAME /etc/nginx/sites-enabled/ | |
nginx -t && nginx -s reload | |
sudo certbot --nginx -d $DOMAIN_NAME --non-interactive --agree-tos --email $EMAIL |