Skip to content

Commit 2ba7e2d

Browse files
committed
fix Workflow file for production deployment
1 parent aae8afb commit 2ba7e2d

1 file changed

Lines changed: 122 additions & 128 deletions

File tree

.github/workflows/deploy.prod.yaml

Lines changed: 122 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,127 @@
11
name: Deployment (Prod)
22

33
on:
4-
push:
5-
branches:
6-
- main
4+
push:
5+
branches:
6+
- main
77

88
jobs:
9-
build:
10-
name: Build, Test, and Deploy
11-
environment: prod
12-
runs-on: ubuntu-latest
13-
steps:
14-
- name: Checkout code
15-
uses: actions/checkout@v2
16-
with:
17-
submodules: true
18-
fetch-depth: 0
19-
20-
# Setup and cache dependencies
21-
- name: Setup Node.js
22-
uses: actions/setup-node@v3
23-
with:
24-
node-version: "18"
25-
cache: "yarn"
26-
27-
- name: Install Node Dependencies
28-
run: yarn install --frozen-lockfile
29-
30-
- name: Install Foundry
31-
uses: foundry-rs/foundry-toolchain@v1
32-
with:
33-
version: stable
34-
35-
- name: Run Forge Install Script
36-
run: chmod +x setup.sh && ./setup.sh
37-
38-
# Run all tests and checks
39-
- name: Run Forge Tests
40-
run: cd chain && forge test --summary
41-
42-
- name: Run ESLint
43-
run: yarn lint:check
44-
45-
- name: Check Formatting
46-
run: yarn format:check
47-
48-
- name: Deploy
49-
shell: bash
50-
env:
51-
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
52-
HOST: ${{ secrets.LIGHTSAIL_INSTANCE_PUBLIC_IP_PROD }}
53-
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
54-
ETHERSCAN_L2_API_KEY: ${{ secrets.ETHERSCAN_L2_API_KEY }}
55-
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
56-
DATABASE_URL: ${{ secrets.DATABASE_URL }}
57-
RPC_URL: ${{ secrets.RPC_URL }}
58-
59-
run: |
60-
# Generate timestamp for deployment
61-
DEPLOY_TIME=$(date +%s)
62-
echo "DEPLOY_TIME: $DEPLOY_TIME"
63-
64-
# Save SSH key and set permissions
65-
echo "$SSH_PRIVATE_KEY" > deploy_key
66-
chmod 600 deploy_key
67-
68-
# Create a temp directory for deployment
69-
DEPLOY_DIR="/tmp/deploy-${DEPLOY_TIME}"
70-
mkdir -p $DEPLOY_DIR
71-
72-
# Copy necessary files to temp directory
73-
echo "Preparing deployment files..."
74-
cp -r . $DEPLOY_DIR/
75-
76-
# Sync files to server
77-
echo "Syncing files to server..."
78-
rsync -az --delete \
79-
--exclude='node_modules' \
80-
--exclude='.git' \
81-
--exclude='deploy_key' \
82-
--include='chain/out' \
83-
--include='chain/out/**' \
84-
-e "ssh -i deploy_key -o StrictHostKeyChecking=no" \
85-
$DEPLOY_DIR/ \
86-
ubuntu@"$HOST":/home/ubuntu/app-${DEPLOY_TIME}
87-
88-
# Execute deployment on server
89-
ssh -i deploy_key -o StrictHostKeyChecking=no ubuntu@"$HOST" "
90-
sudo su && \
91-
cd /home/ubuntu/app-${DEPLOY_TIME} && \
92-
echo 'Building image on host...' && \
93-
# Source the functions
94-
source ./scripts/docker_container_utils.sh && \
95-
docker build -t ocp-prod:${DEPLOY_TIME} -f Dockerfile.prod . && \
96-
97-
# Initial cleanup
98-
echo 'Cleaning up old resources...' && \
99-
docker ps -q --filter 'publish=8081' | xargs -r docker rm -f && \
100-
docker ps -q --filter 'publish=8082' | xargs -r docker rm -f && \
101-
docker container prune -f && \
102-
docker image prune -f && \
103-
104-
# Start new container
105-
echo 'Starting new container...' && \
106-
CONTAINER_NAME=ocp-prod-${DEPLOY_TIME} && \
107-
108-
# Run container
109-
docker run --name \$CONTAINER_NAME -d \
110-
--health-cmd='curl -f http://localhost:8080/health || exit 1' \
111-
--health-interval='2s' \
112-
--health-retries='3' \
113-
--health-timeout='5s' \
114-
--restart always \
115-
-e DOCKER_ENV='true' \
116-
-e NODE_ENV='production' \
117-
-e SENTRY_DSN='${SENTRY_DSN}' \
118-
-e DATABASE_URL='${DATABASE_URL}' \
119-
-e RPC_URL='${RPC_URL}' \
120-
-e PORT=8080 \
121-
-e PRIVATE_KEY='${PRIVATE_KEY}' \
122-
-e ETHERSCAN_L2_API_KEY='${ETHERSCAN_L2_API_KEY}' \
123-
-v '/home/ubuntu/global-bundle.pem:/global-bundle.pem' \
124-
ocp-prod:${DEPLOY_TIME} && \
125-
126-
# Wait for container to be healthy
127-
wait_for_health \"\$CONTAINER_NAME\" && \
128-
if [ \$? -eq 0 ]; then
129-
handle_container_switch \"\$CONTAINER_NAME\" \"${DEPLOY_TIME}\" \"prod\"
130-
else
131-
handle_failed_deployment \"\$CONTAINER_NAME\" \"${DEPLOY_TIME}\" \"prod\"
132-
fi
133-
"
9+
build:
10+
name: Build, Test, and Deploy
11+
environment: prod
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v2
17+
with:
18+
submodules: true
19+
fetch-depth: 0
20+
21+
- name: Setup Node.js
22+
uses: actions/setup-node@v3
23+
with:
24+
node-version: "18"
25+
cache: "yarn"
26+
27+
- name: Cache Yarn files (manual fallback)
28+
uses: actions/cache@v3
29+
with:
30+
path: ~/.cache/yarn
31+
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
32+
restore-keys: |
33+
${{ runner.os }}-yarn-
34+
35+
- name: Install Node Dependencies
36+
run: yarn install --frozen-lockfile --network-concurrency 5 --no-progress --verbose
37+
38+
- name: Install Foundry
39+
uses: foundry-rs/foundry-toolchain@v1
40+
with:
41+
version: stable
42+
43+
- name: Run Forge Install Script
44+
run: chmod +x setup.sh && ./setup.sh
45+
46+
- name: Run Forge Tests
47+
run: cd chain && forge test --summary
48+
49+
- name: Run ESLint
50+
run: yarn lint:check
51+
52+
- name: Check Formatting
53+
run: yarn format:check
54+
55+
- name: Deploy
56+
shell: bash
57+
env:
58+
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
59+
HOST: ${{ secrets.LIGHTSAIL_INSTANCE_PUBLIC_IP_PROD }}
60+
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
61+
ETHERSCAN_L2_API_KEY: ${{ secrets.ETHERSCAN_L2_API_KEY }}
62+
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
63+
DATABASE_URL: ${{ secrets.DATABASE_URL }}
64+
RPC_URL: ${{ secrets.RPC_URL }}
65+
run: |
66+
DEPLOY_TIME=$(date +%s)
67+
echo "DEPLOY_TIME: $DEPLOY_TIME"
68+
69+
echo "$SSH_PRIVATE_KEY" > deploy_key
70+
chmod 600 deploy_key
71+
72+
DEPLOY_DIR="/tmp/deploy-${DEPLOY_TIME}"
73+
mkdir -p $DEPLOY_DIR
74+
75+
echo "Preparing deployment files..."
76+
cp -r . $DEPLOY_DIR/
77+
78+
echo "Syncing files to server..."
79+
rsync -az --delete \
80+
--exclude='node_modules' \
81+
--exclude='.git' \
82+
--exclude='deploy_key' \
83+
--include='chain/out' \
84+
--include='chain/out/**' \
85+
-e "ssh -i deploy_key -o StrictHostKeyChecking=no" \
86+
$DEPLOY_DIR/ \
87+
ubuntu@"$HOST":/home/ubuntu/app-${DEPLOY_TIME}
88+
89+
ssh -i deploy_key -o StrictHostKeyChecking=no ubuntu@"$HOST" "
90+
sudo su && \
91+
cd /home/ubuntu/app-${DEPLOY_TIME} && \
92+
echo 'Building image on host...' && \
93+
source ./scripts/docker_container_utils.sh && \
94+
docker build -t ocp-prod:${DEPLOY_TIME} -f Dockerfile.prod . && \
95+
96+
echo 'Cleaning up old resources...' && \
97+
docker ps -q --filter 'publish=8081' | xargs -r docker rm -f && \
98+
docker ps -q --filter 'publish=8082' | xargs -r docker rm -f && \
99+
docker container prune -f && \
100+
docker image prune -f && \
101+
102+
echo 'Starting new container...' && \
103+
CONTAINER_NAME=ocp-prod-${DEPLOY_TIME} && \
104+
docker run --name \$CONTAINER_NAME -d \
105+
--health-cmd='curl -f http://localhost:8080/health || exit 1' \
106+
--health-interval='2s' \
107+
--health-retries='3' \
108+
--health-timeout='5s' \
109+
--restart always \
110+
-e DOCKER_ENV='true' \
111+
-e NODE_ENV='production' \
112+
-e SENTRY_DSN='${SENTRY_DSN}' \
113+
-e DATABASE_URL='${DATABASE_URL}' \
114+
-e RPC_URL='${RPC_URL}' \
115+
-e PORT=8080 \
116+
-e PRIVATE_KEY='${PRIVATE_KEY}' \
117+
-e ETHERSCAN_L2_API_KEY='${ETHERSCAN_L2_API_KEY}' \
118+
-v '/home/ubuntu/global-bundle.pem:/global-bundle.pem' \
119+
ocp-prod:${DEPLOY_TIME} && \
120+
121+
wait_for_health \"\$CONTAINER_NAME\" && \
122+
if [ \$? -eq 0 ]; then
123+
handle_container_switch \"\$CONTAINER_NAME\" \"${DEPLOY_TIME}\" \"prod\"
124+
else
125+
handle_failed_deployment \"\$CONTAINER_NAME\" \"${DEPLOY_TIME}\" \"prod\"
126+
fi
127+
"

0 commit comments

Comments
 (0)