Skip to content

Commit

Permalink
AWS staging delpoy script (#121)
Browse files Browse the repository at this point in the history
* ci: update deploy script

* ci: update file path

* ci: update path

* ci: update port

* ci: update actions

* ci: remove quotes

* ci: add env variable

* ci: add export key

* ci: add quotes

* chore: add env

* chore: testing echo

* chore: put in quotes

* chore: remove brackets

* chore: add env

* chore: add quotes

* chore: use secret

* chore: remove env form ssh script

* feat: add conditional environment for action

---------

Co-authored-by: Amit Prakash <[email protected]>
  • Loading branch information
prakashchoudhary07 and iamitprakash authored Jun 4, 2024
1 parent e9bbb1f commit 0821c45
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions .github/workflows/deploy-to-ec2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,54 @@ 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: ${{ github.ref == 'refs/heads/main' && 'production' || '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: skill-tree/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 }}:8080 \
--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 0821c45

Please sign in to comment.