Repository navigation
138 lines (117 loc) · 4.49 KB
/
Copy pathdeploy.yml
File metadata and controls
138 lines (117 loc) · 4.49 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
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: Build and Deploy Work Site to EC2
on:
push:
branches:
- master
permissions:
contents: read
packages: read
deployments: write
statuses: write
id-token: write
jobs:
build-and-deploy:
name: Build Docker Image and Deploy Work Site
runs-on: ubuntu-latest
environment:
name: EC2 Work (production)
url: https://work.gotpop.io
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Start deployment
uses: bobheadxi/deployments@v1
id: deployment
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
env: EC2 Work (production)
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build and push Docker image to ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: gotpop-work
IMAGE_TAG: ${{ github.sha }}
run: |
echo "Building Docker image for work site..."
docker build \
--build-arg STORYBLOK_ACCESS_TOKEN=${{ secrets.STORYBLOK_ACCESS_TOKEN }} \
--build-arg STORYBLOK_CONTENT_PREFIX=${{ secrets.STORYBLOK_CONTENT_PREFIX }} \
--build-arg GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} \
-t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \
-t $ECR_REGISTRY/$ECR_REPOSITORY:latest \
.
echo "Pushing to work ECR..."
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
echo "✅ Work site image pushed successfully!"
echo "Image: $ECR_REGISTRY/$ECR_REPOSITORY:latest"
- name: Deploy to EC2
uses: appleboy/ssh-action@v1.0.3
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: gotpop-work
AWS_REGION: ${{ secrets.AWS_REGION }}
with:
host: ${{ secrets.EC2_HOST_WORK }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY_WORK }}
envs: ECR_REGISTRY,ECR_REPOSITORY,AWS_REGION
script: |
echo "=== Work Site Deployment Started ==="
# Login to ECR from EC2
echo "Authenticating with work ECR..."
aws ecr get-login-password --region $AWS_REGION | \
docker login --username AWS --password-stdin $ECR_REGISTRY
# Pull new image
echo "Pulling latest work site image from ECR..."
docker pull $ECR_REGISTRY/$ECR_REPOSITORY:latest
# Stop and remove old container
echo "Stopping old work container..."
docker stop gotpop-work || true
docker rm gotpop-work || true
# Run new container
echo "Starting new work container..."
docker run -d \
-p 3000:3000 \
--name gotpop-work \
--restart unless-stopped \
--env-file /var/www/gotpop-work/.env \
$ECR_REGISTRY/$ECR_REPOSITORY:latest
# Comprehensive cleanup
echo "Cleaning up Docker resources..."
docker image prune -a -f
docker container prune -f
docker volume prune -f
# Clean yarn cache on host (if it exists)
echo "Cleaning yarn cache..."
rm -rf /usr/local/share/.cache/yarn/* ~/.cache/yarn/* 2>/dev/null || true
# Show status
echo "=== Work Site Deployment Complete ==="
echo "Container status:"
docker ps --filter name=gotpop-work
echo ""
echo "Recent logs:"
docker logs gotpop-work --tail 20
echo ""
echo "Disk usage:"
df -h | grep -E '/$|Filesystem'
- name: Update deployment status
uses: bobheadxi/deployments@v1
if: always()
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
env: EC2 Work (production)
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
env_url: https://work.gotpop.io