feat: add env to build #12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to AWS | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- 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: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to Amazon ECR | |
uses: aws-actions/amazon-ecr-login@v2 | |
- name: Build and push socket Docker image | |
run: | | |
docker buildx build --platform linux/amd64 \ | |
--tag ${{ secrets.ECR_SOCKET_REPO_URI }}:latest \ | |
--file ./socket/Dockerfile \ | |
./socket --push | |
- name: Build and push server Docker image | |
run: | | |
docker buildx build --platform linux/amd64 \ | |
--tag ${{ secrets.ECR_SERVER_REPO_URI }}:latest \ | |
--file ./server/Dockerfile \ | |
./server --push | |
- name: Deploy to EC2 instance | |
env: | |
EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }} | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
AWS_REGION: ${{ secrets.AWS_REGION }} | |
run: | | |
echo "$EC2_SSH_KEY" | base64 --decode > /tmp/id_rsa | |
chmod 600 /tmp/id_rsa | |
ssh -o StrictHostKeyChecking=no -i /tmp/id_rsa ec2-user@${{ secrets.EC2_INSTANCE_IP }} << 'EOF' | |
export AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }} | |
export AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
export AWS_REGION=${{ secrets.AWS_REGION }} | |
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin ${{ secrets.ECR_SOCKET_REPO_URI }} | |
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin ${{ secrets.ECR_SERVER_REPO_URI }} | |
docker pull ${{ secrets.ECR_SOCKET_REPO_URI }}:latest | |
docker pull ${{ secrets.ECR_SERVER_REPO_URI }}:latest | |
docker stop socket-app || true | |
docker rm socket-app || true | |
docker run -d --name socket-app -p 3000:3000 ${{ secrets.ECR_SOCKET_REPO_URI }}:latest | |
docker stop server-app || true | |
docker rm server-app || true | |
docker run -d --name server-app -p 5000:5000 \ | |
-e PORT=5000 \ | |
-e MONGO_URI=${{ secrets.MONGO_URI }} \ | |
-e JWT_KEY=${{ secrets.JWT_KEY }} \ | |
${{ secrets.ECR_SERVER_REPO_URI }}:latest | |
EOF |