Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS staging delpoy script #121

Merged
merged 19 commits into from
Jun 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }}
Loading