updated README #3
This file contains hidden or 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 Scraper and API to AWS | |
| on: | |
| push: | |
| branches: | |
| - main | |
| permissions: | |
| id-token: write | |
| contents: read | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| role-to-assume: ${{ secrets.AWS_ROLE_ARN }} | |
| aws-region: ${{ secrets.AWS_REGION || 'us-west-2' }} | |
| - name: Login to Amazon ECR | |
| id: login-ecr | |
| uses: aws-actions/amazon-ecr-login@v2 | |
| - name: Ensure .env exists for image builds | |
| run: | | |
| if [ ! -f .env ]; then | |
| touch .env | |
| fi | |
| - name: Set scraper image tags | |
| id: scraper-image | |
| env: | |
| ECR_REPOSITORY: ${{ secrets.SCRAPER_ECR_REPOSITORY || 'scraper' }} | |
| run: | | |
| echo "ecr_image_uri=${{ steps.login-ecr.outputs.registry }}/${ECR_REPOSITORY}:${{ github.sha }}" >> "$GITHUB_OUTPUT" | |
| echo "ecr_image_latest=${{ steps.login-ecr.outputs.registry }}/${ECR_REPOSITORY}:latest" >> "$GITHUB_OUTPUT" | |
| - name: Build and push scraper image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: web_scraper/Dockerfile | |
| platforms: linux/amd64 | |
| push: true | |
| tags: | | |
| ${{ steps.scraper-image.outputs.ecr_image_uri }} | |
| ${{ steps.scraper-image.outputs.ecr_image_latest }} | |
| - name: Set API image tags | |
| id: api-image | |
| env: | |
| ECR_REPOSITORY: ${{ secrets.API_ECR_REPOSITORY || 'api' }} | |
| run: | | |
| echo "ecr_image_uri=${{ steps.login-ecr.outputs.registry }}/${ECR_REPOSITORY}:${{ github.sha }}" >> "$GITHUB_OUTPUT" | |
| echo "ecr_image_latest=${{ steps.login-ecr.outputs.registry }}/${ECR_REPOSITORY}:latest" >> "$GITHUB_OUTPUT" | |
| - name: Build and push API image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: api/dockerfiles/prod/Dockerfile | |
| platforms: linux/amd64 | |
| push: true | |
| tags: | | |
| ${{ steps.api-image.outputs.ecr_image_uri }} | |
| ${{ steps.api-image.outputs.ecr_image_latest }} | |
| - name: Deploy CloudFormation stack | |
| env: | |
| AWS_REGION: ${{ secrets.AWS_REGION || 'us-west-2' }} | |
| STACK_NAME: ${{ secrets.SCRAPER_STACK_NAME }} | |
| VPC_ID: ${{ secrets.SCRAPER_VPC_ID }} | |
| SUBNET_IDS: ${{ secrets.SCRAPER_SUBNET_IDS }} | |
| SECURITY_GROUP_ID: ${{ secrets.SCRAPER_SECURITY_GROUP_ID }} | |
| SECRET_ARN: ${{ secrets.SCRAPER_SECRET_ARN }} | |
| SCHEDULE_EXPRESSION: ${{ secrets.SCRAPER_SCHEDULE_EXPRESSION }} | |
| USE_FARGATE_SPOT: ${{ secrets.SCRAPER_USE_FARGATE_SPOT }} | |
| ASSIGN_PUBLIC_IP: ${{ secrets.SCRAPER_ASSIGN_PUBLIC_IP }} | |
| ECR_IMAGE_URI: ${{ steps.scraper-image.outputs.ecr_image_uri }} | |
| run: | | |
| set -euo pipefail | |
| AWS_REGION="${AWS_REGION:-us-west-2}" | |
| STACK_NAME="${STACK_NAME:-troutlytics-scraper-schedule}" | |
| SCHEDULE_EXPRESSION="${SCHEDULE_EXPRESSION:-rate(20 minutes)}" | |
| USE_FARGATE_SPOT="${USE_FARGATE_SPOT:-true}" | |
| ASSIGN_PUBLIC_IP="${ASSIGN_PUBLIC_IP:-ENABLED}" | |
| aws cloudformation deploy \ | |
| --stack-name "$STACK_NAME" \ | |
| --template-file aws_config/scheduled-scraper-fargate.yaml \ | |
| --capabilities CAPABILITY_NAMED_IAM \ | |
| --region "$AWS_REGION" \ | |
| --parameter-overrides \ | |
| ECRImageUri="$ECR_IMAGE_URI" \ | |
| VpcId="$VPC_ID" \ | |
| SubnetIds="$SUBNET_IDS" \ | |
| SecurityGroupId="$SECURITY_GROUP_ID" \ | |
| SecretArn="$SECRET_ARN" \ | |
| ScheduleExpression="$SCHEDULE_EXPRESSION" \ | |
| UseFargateSpot="$USE_FARGATE_SPOT" \ | |
| AssignPublicIp="$ASSIGN_PUBLIC_IP" | |
| - name: Update API Lambda image | |
| env: | |
| AWS_REGION: ${{ secrets.AWS_REGION || 'us-west-2' }} | |
| API_LAMBDA_NAME: ${{ secrets.API_LAMBDA_NAME }} | |
| API_IMAGE_URI: ${{ steps.api-image.outputs.ecr_image_uri }} | |
| run: | | |
| set -euo pipefail | |
| if [ -z "${API_LAMBDA_NAME:-}" ]; then | |
| echo "API_LAMBDA_NAME is not set; skipping Lambda update." | |
| exit 0 | |
| fi | |
| aws lambda update-function-code \ | |
| --function-name "$API_LAMBDA_NAME" \ | |
| --image-uri "$API_IMAGE_URI" \ | |
| --region "$AWS_REGION" |