-
Notifications
You must be signed in to change notification settings - Fork 1
153 lines (134 loc) · 4.83 KB
/
Copy pathdeploy.yml
File metadata and controls
153 lines (134 loc) · 4.83 KB
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
name: Build and Deploy
on:
push:
branches:
- master
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
environment: mainnet
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v5
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha,prefix={{branch}}-
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
BLOCKCHAIN_API_URL=${{ vars.BLOCKCHAIN_API_URL }}
GOVERNANCE_URL=${{ vars.GOVERNANCE_URL }}
INDEX_API_URL=${{ vars.INDEX_API_URL }}
IS_TESTNET=${{ vars.IS_TESTNET }}
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache,mode=max
deploy:
needs: build
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
environment: mainnet
permissions:
packages: read
concurrency:
group: deploy-explorer
cancel-in-progress: false
env:
DOCKER_HOST: ssh://${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}
steps:
- name: Setup SSH
uses: MrSquaare/ssh-setup-action@2d028b70b5e397cf8314c6eaea229a6c3e34977a
with:
host: ${{ secrets.SSH_HOST }}
private-key: ${{ secrets.SSH_PRIVATE_KEY }}
private-key-name: deploy_key
- name: Log in to GHCR on remote host
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Deploy
run: |
IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:master"
NAME="explorer"
echo "Pulling $IMAGE..."
docker pull "$IMAGE"
echo "Stopping existing container..."
docker stop "$NAME" 2>/dev/null || true
docker rm "$NAME" 2>/dev/null || true
echo "Starting new container..."
docker run \
--detach \
--restart always \
--name "$NAME" \
--network core-network \
--expose 8000 \
--env HTTP_PORT="8000" \
--env INDEX_API_URL="${{ vars.INDEX_API_URL }}" \
--env LETSENCRYPT_EMAIL="${{ vars.LETSENCRYPT_EMAIL }}" \
--env LETSENCRYPT_HOST="${{ vars.DOMAIN }}" \
--env NETWORK_ACCESS="external" \
--env VIRTUAL_HOST="${{ vars.DOMAIN }}" \
"$IMAGE"
- name: Verify
run: |
sleep 10
STATE=$(docker inspect --format '{{.State.Status}}' "explorer")
if [ "$STATE" != "running" ]; then
echo "Container is not running (state: $STATE)"
docker logs --tail=50 "explorer"
exit 1
fi
echo "Container is running"
- name: Send Discord notification
if: always()
run: |
STATUS="${{ job.status }}"
RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
if [ "$STATUS" == "success" ]; then
COLOR=3066993
TITLE="Deployment Successful"
else
COLOR=15158332
TITLE="Deployment Failed"
fi
curl -s -H "Content-Type: application/json" \
-X POST "${{ secrets.DISCORD_DEPLOYS_WEBHOOK }}" \
-d "{
\"username\": \"GitHub Actions\",
\"embeds\": [{
\"title\": \"$TITLE\",
\"color\": $COLOR,
\"fields\": [
{ \"name\": \"Service\", \"value\": \"Explorer\", \"inline\": true },
{ \"name\": \"Commit\", \"value\": \"\`${GITHUB_SHA:0:7}\`\", \"inline\": true },
{ \"name\": \"Workflow\", \"value\": \"[View logs]($RUN_URL)\", \"inline\": true }
],
\"timestamp\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",
\"footer\": { \"text\": \"Edge Explorer\" }
}]
}"