-
Notifications
You must be signed in to change notification settings - Fork 0
58 lines (50 loc) · 2.01 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
name: Deploy to ECR
on:
push:
branches: [main]
paths-ignore:
- "**.md"
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[skip deploy]')"
env:
IMAGE_TAG: latest
ECR_REGISTRY: 900017126942.dkr.ecr.ap-northeast-2.amazonaws.com
ECR_REPOSITORY: team3-server
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: "17"
distribution: "temurin"
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2
- name: Login to ECR
uses: aws-actions/amazon-ecr-login@v1
- name: Remove all images from ECR
run: |
aws ecr list-images --repository-name $ECR_REPOSITORY --query 'imageIds[*]' --output json | jq -r '.[] | .imageDigest' | xargs -I {} aws ecr batch-delete-image --repository-name $ECR_REPOSITORY --image-ids imageDigest={}
- name: Docker build, tag, and push image to ECR
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
docker pull 900017126942.dkr.ecr.ap-northeast-2.amazonaws.com/team3-server:latest
docker stop my-container || true
docker rm my-container || true
docker run -d --name my-container --network my_network -p 80:8080 900017126942.dkr.ecr.ap-northeast-2.amazonaws.com/team3-server:latest