diff --git a/.github/workflows/CI-CD.yml b/.github/workflows/CI-CD.yml new file mode 100644 index 0000000..9fafd01 --- /dev/null +++ b/.github/workflows/CI-CD.yml @@ -0,0 +1,59 @@ +name: CI/CD Pipeline + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + # Log in to Amazon ECR + - name: Log in to Amazon ECR + run: | + aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} + aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com + + # Build the Docker image + - name: Build Docker image + run: docker build -t ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/fantasybballstats:latest . + + # Push the Docker image to Amazon ECR + - name: Push Docker image to ECR + run: docker push ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/fantasybballstats:latest + + deploy: + runs-on: ubuntu-latest + needs: build + + steps: + # SSH to EC2 instance and pull the new image from ECR + - name: SSH and Deploy to EC2 + uses: appleboy/ssh-action@v0.1.7 + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USER }} + key: ${{ secrets.EC2_PEM_KEY }} + script: | + aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} + aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com + docker pull ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/fantasybballstats:latest + docker stop fantasy_scraper_container || true + docker rm fantasy_scraper_container || true + docker run -d -p 5000:5000 --name fantasy_scraper_container ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/fantasybballstats-new:latest + + # restart nginx to update website + sudo systemctl restart nginx