Pull Data and Deploy Report #478
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: Pull Data and Deploy Report | |
| on: | |
| push: | |
| branches: ["main"] | |
| workflow_dispatch: | |
| schedule: | |
| # Every other day at 00:00 UTC (1st, 3rd, 5th … of the month) | |
| - cron: "0 0 */2 * *" | |
| permissions: | |
| contents: write | |
| jobs: | |
| update-and-deploy: | |
| runs-on: ubuntu-latest | |
| env: | |
| DB_HOST: ${{ secrets.DB_HOST }} | |
| DB_USER: ${{ secrets.DB_USER }} | |
| DB_PASSWORD: ${{ secrets.DB_PASSWORD }} | |
| DB_PORT: ${{ secrets.DB_PORT }} | |
| DB_NAME: ${{ secrets.DB_NAME }} | |
| steps: | |
| - name: Get GitHub action IP | |
| id: ip | |
| uses: haythem/public-ip@v1.3 | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Python 3.12 | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.12" | |
| - name: Setting environment variables | |
| run: | | |
| echo "AWS_DEFAULT_REGION=us-east-1" >> $GITHUB_ENV | |
| - name: Add GitHub Actions IP to Security group | |
| run: | | |
| aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 3306 --cidr ${{ steps.ip.outputs.ipv4 }}/32 > /dev/null | |
| env: | |
| AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| AWS_DEFAULT_REGION: ${{ env.AWS_DEFAULT_REGION }} | |
| - name: Install dependencies | |
| run: | | |
| pip install -r ./.scripts/requirements.txt | |
| - name: Pull data from database | |
| run: | | |
| python3 .scripts/pull_data.py | |
| - name: Commit data changes | |
| run: | | |
| echo "Updated at $(date)" >> updated_at.txt | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| git add . | |
| git commit -m "Updated data at $(date)" || echo "No data changes" | |
| git push || echo "No changes to push" | |
| # ── Build & deploy the Commonsense Survey Explorer ("report") image ────── | |
| # Regenerates the visualize data from the freshly pulled raw data, bakes | |
| # it into a container, pushes to ECR, and triggers an ECS redeploy. The | |
| # task definition itself is owned by the commonsense-platform repo and is | |
| # never edited here — we just push :latest and force a new deployment. | |
| - name: Build visualize data | |
| run: | | |
| pip install "scipy==1.16.3" | |
| cd .scripts/visualize | |
| python3 update-data.py | |
| - name: Login to Amazon ECR | |
| id: login-ecr | |
| uses: aws-actions/amazon-ecr-login@v2 | |
| env: | |
| AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| AWS_DEFAULT_REGION: ${{ env.AWS_DEFAULT_REGION }} | |
| AWS_REGION: us-east-1 | |
| - name: Build, tag, and push report image to Amazon ECR | |
| env: | |
| ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
| ECR_REPOSITORY: commonsense-report | |
| IMAGE_TAG: ${{ github.sha }} | |
| run: | | |
| docker build -f .scripts/visualize/Dockerfile \ | |
| -t $ECR_REGISTRY/$ECR_REPOSITORY:latest \ | |
| -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
| docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest | |
| docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
| - name: Trigger ECS redeploy | |
| env: | |
| AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| AWS_DEFAULT_REGION: ${{ env.AWS_DEFAULT_REGION }} | |
| run: | | |
| aws ecs update-service \ | |
| --cluster common-sense-cluster \ | |
| --service common-sense-service \ | |
| --force-new-deployment > /dev/null | |
| - name: Remove GitHub Actions IP from security group | |
| if: always() | |
| run: | | |
| aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 3306 --cidr ${{ steps.ip.outputs.ipv4 }}/32 | |
| env: | |
| AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| AWS_DEFAULT_REGION: ${{ env.AWS_DEFAULT_REGION }} |