Skip to content

Commit

Permalink
ci: update deploy script
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashchoudhary07 committed May 22, 2024
1 parent d4b18a9 commit dc42ba7
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions .github/workflows/deploy-to-ec2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,54 @@ name: Deploy to EC2
on:
push:
branches:
- main
- develop
- feat/aws-deploy # Remove before merting this PR to dev

jobs:
build-and-push:
runs-on: ubuntu-latest
timeout-minutes: 10
environment: staging

steps:
- name: Checkout Repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up AWS CLI
uses: aws-actions/configure-aws-credentials@v2
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Login to Amazon ECR Public
id: login-ecr
run: aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${{ secrets.AWS_ECR_URI }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build Docker image
run: docker build -t skill-tree-backend -f skill-tree/Dockerfile .
- name: Tag Docker image
run: docker tag skill-tree-backend:latest ${{ secrets.AWS_ECR_URI }}/skill-tree-backend:latest

- name: Push Docker image to ECR Public
run: docker push ${{ secrets.AWS_ECR_URI }}/skill-tree-backend:latest
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
platforms: linux/arm64
push: true
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}:${{ github.sha }}
${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max

deploy:
needs: build-and-push
runs-on: ubuntu-latest
steps:
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.AWS_EC2_HOST }}
username: ${{ secrets.AWS_EC2_USERNAME }}
key: ${{ secrets.AWS_EC2_SSH_PRIVATE_KEY }}
- name: Pull Image from docker hub
run: docker pull ${{ secrets.AWS_ECR_URI }}/skill-tree-backend:latest
- name: Delete old container
run: docker rm -f skill-tree-backend
- name: Run docker container
run: docker run -d -p 8080:8080 --name skill-tree-backend -e RDS_PUBLIC_KEY=${{secrets.RDS_PUBLIC_KEY}} -e API_V1_PREFIX=/api/v1 ${{ secrets.AWS_ECR_URI }}/skill-tree-backend

script: |
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}:latest
docker stop ${{ github.event.repository.name }}-${{vars.ENV}} || true
docker rm ${{ github.event.repository.name }}-${{vars.ENV}} || true
docker run -d -p ${{vars.PORT}}:${{vars.PORT}} \
--name ${{ github.event.repository.name }}-${{vars.ENV}} \
--network=${{vars.DOCKER_NETWORK}} \
-e RDS_PUBLIC_KEY=${{secrets.RDS_PUBLIC_KEY}} \
-e API_V1_PREFIX=/api/v1 \
${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}

0 comments on commit dc42ba7

Please sign in to comment.