-
Notifications
You must be signed in to change notification settings - Fork 0
170 lines (152 loc) · 6.05 KB
/
deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
name: Deploy to DigitalOcean
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Load .mautic_env variables
run: |
set -a
source .mautic_env
set +a
echo "MAUTIC_PORT=${MAUTIC_PORT}" >> $GITHUB_ENV
- name: Check EMAIL_ADDRESS environment variable
run: |
if [ -z "${EMAIL_ADDRESS}" ]; then
echo "Error: Missing required environment variable: EMAIL_ADDRESS"
exit 1
fi
env:
EMAIL_ADDRESS: ${{ vars.EMAIL_ADDRESS }}
- name: Check required secrets
env:
DIGITALOCEAN_ACCESS_TOKEN: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
DIGITALOCEAN_SSH_FINGERPRINT: ${{ secrets.DIGITALOCEAN_SSH_FINGERPRINT }}
MAUTIC_PASSWORD: ${{ secrets.MAUTIC_PASSWORD }}
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
run: |
error_missing_secrets=()
check_secret() {
if [ -z "${!1}" ]; then
error_missing_secrets+=("$1")
fi
}
check_secret "DIGITALOCEAN_ACCESS_TOKEN"
check_secret "DIGITALOCEAN_SSH_FINGERPRINT"
check_secret "MAUTIC_PASSWORD"
check_secret "SSH_PRIVATE_KEY"
if [ ${#error_missing_secrets[@]} -ne 0 ]; then
echo "Error: Missing required secrets: ${error_missing_secrets[*]}"
exit 1
fi
- 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-vps.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: Prepare virtual server configuration
if: ${{ vars.DOMAIN }}
run: |
DOMAIN_IP=$(dig +short ${{ vars.DOMAIN }})
if [ "$DOMAIN_IP" == "${{ env.ip }}" ]; then
echo "Domain ${{ vars.DOMAIN }} correctly points to the droplet IP."
# Rename the nginx-virtual-host-template file
mv nginx-virtual-host-template "nginx-virtual-host-${{ vars.DOMAIN }}"
# Replace DOMAIN_NAME inside the file with the actual domain
sed -i "s/DOMAIN_NAME/${{ vars.DOMAIN }}/g" "nginx-virtual-host-${{ vars.DOMAIN }}"
sed -i "s/PORT/${{ env.MAUTIC_PORT }}/g" "nginx-virtual-host-${{ vars.DOMAIN }}"
cat nginx-virtual-host-${{ vars.DOMAIN }} # debug
else
echo "Error: Domain ${{ vars.DOMAIN }} does not point to the droplet IP."
echo "To configure your DNS settings, access your domain registrar's DNS management page. Locate the DNS settings or DNS management section. You should create or update an A record with the following details: Name: @ (or your subdomain, e.g., www if your domain is www.example.com), Type: A, Value: ${{ env.ip }}. This change will point ${{ vars.DOMAIN }} to the IP address ${{ env.ip }}. Note that DNS changes can take up to 48 hours to propagate globally."
exit 1
fi
- name: Prepare setup-dc.sh script
run: |
# Replace placeholders in setup-dc.sh
sed -i "s/{{IP_ADDRESS}}/${{ env.ip }}/g" setup-dc.sh
sed -i "s/{{PORT}}/${{ env.MAUTIC_PORT }}/g" setup-dc.sh
sed -i "s/{{EMAIL_ADDRESS}}/${{ env.EMAIL_ADDRESS }}/g" setup-dc.sh
sed -i "s/{{MAUTIC_PASSWORD}}/${{ secrets.MAUTIC_PASSWORD }}/g" setup-dc.sh
if [ ! -z "${{ env.DOMAIN }}" ]; then
sed -i "s/{{DOMAIN_NAME}}/${{ env.DOMAIN }}/g" setup-dc.sh
fi
cat setup-dc.sh # debug
env:
EMAIL_ADDRESS: ${{ vars.EMAIL_ADDRESS }}
DOMAIN: ${{ vars.DOMAIN }}
- name: Deploy to Server
uses: easingthemes/ssh-deploy@main
with:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
ARGS: "-rlgoDzvc"
SOURCE: "."
REMOTE_HOST: ${{ env.ip }}
REMOTE_USER: root
TARGET: /var/www
EXCLUDE: ".git"
SCRIPT_BEFORE: mkdir -p /var/www
SCRIPT_AFTER: /var/www/setup-dc.sh > /var/log/setup-dc.log 2>&1
- name: Open your Mautic instance
run: |
if [ -z "${DOMAIN}" ]; then
echo "You can visit the Mautic installation at http://${{ env.ip }}:${{ env.MAUTIC_PORT }}"
else
echo "You can visit the Mautic installation at http://${DOMAIN}"
fi
env:
DOMAIN: ${{ vars.DOMAIN }}
- name: Download setup-dc.log from Server
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa root@${{ env.ip }}:/var/log/setup-dc.log ./setup-dc.log
rm -f ~/.ssh/id_rsa
shell: bash
- name: Upload setup-dc.log as Artifact
uses: actions/upload-artifact@v4
with:
name: setup-dc-log
path: ./setup-dc.log