|
| 1 | +name: 'Deployment step 2: Deploy to Production' |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] |
| 6 | + |
| 7 | +env: |
| 8 | + ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true |
| 9 | + REGION: us-central1 |
| 10 | + SERVICE_NAME: policyengine-household-api |
| 11 | + IMAGE_NAME: ghcr.io/policyengine-household-api/policyengine-household-api |
| 12 | + PYTHON_VERSION: '3.12' |
| 13 | + |
| 14 | +jobs: |
| 15 | + lint-and-test: |
| 16 | + name: Lint and test |
| 17 | + runs-on: ubuntu-latest |
| 18 | + if: | |
| 19 | + (github.repository == 'PolicyEngine/policyengine-household-api') |
| 20 | + && (github.event.head_commit.message == 'Update PolicyEngine Household API') |
| 21 | + steps: |
| 22 | + - name: Checkout code |
| 23 | + uses: actions/checkout@v4 |
| 24 | + |
| 25 | + - name: Set up Python |
| 26 | + uses: actions/setup-python@v4 |
| 27 | + with: |
| 28 | + python-version: ${{ env.PYTHON_VERSION }} |
| 29 | + |
| 30 | + - name: Install dependencies |
| 31 | + run: make install |
| 32 | + |
| 33 | + - name: Run linter |
| 34 | + run: make format |
| 35 | + |
| 36 | + - name: Run tests |
| 37 | + run: make test |
| 38 | + |
| 39 | + build-docker: |
| 40 | + name: Build Docker image |
| 41 | + runs-on: ubuntu-latest |
| 42 | + if: | |
| 43 | + (github.repository == 'PolicyEngine/policyengine-household-api') |
| 44 | + && (github.event.head_commit.message == 'Update PolicyEngine Household API') |
| 45 | + needs: [lint-and-test] |
| 46 | + steps: |
| 47 | + - name: Checkout code |
| 48 | + uses: actions/checkout@v4 |
| 49 | + |
| 50 | + - name: Set up Docker Buildx |
| 51 | + uses: docker/setup-buildx-action@v3 |
| 52 | + |
| 53 | + - name: Log in to GitHub Container Registry |
| 54 | + uses: docker/login-action@v2 |
| 55 | + with: |
| 56 | + registry: ghcr.io |
| 57 | + username: ${{ github.actor }} |
| 58 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 59 | + |
| 60 | + - name: Extract metadata |
| 61 | + id: meta |
| 62 | + uses: docker/metadata-action@v5 |
| 63 | + with: |
| 64 | + images: ${{ env.IMAGE_NAME }} |
| 65 | + tags: | |
| 66 | + type=sha,prefix={{branch}}- |
| 67 | + type=raw,value=latest,enable={{is_default_branch}} |
| 68 | + type=raw,value=python${{ env.PYTHON_VERSION }},enable={{is_default_branch}} |
| 69 | +
|
| 70 | + - name: Build and push Docker image |
| 71 | + uses: docker/build-push-action@v5 |
| 72 | + with: |
| 73 | + context: . |
| 74 | + file: ./dockerfiles/production/Dockerfile.production |
| 75 | + push: true |
| 76 | + tags: ${{ steps.meta.outputs.tags }} |
| 77 | + labels: ${{ steps.meta.outputs.labels }} |
| 78 | + cache-from: type=gha |
| 79 | + cache-to: type=gha,mode=max |
| 80 | + |
| 81 | + - name: Verify image was pushed |
| 82 | + run: | |
| 83 | + echo "Verifying image was pushed to GitHub Container Registry..." |
| 84 | + docker pull ${{ env.IMAGE_NAME }}:${{ github.sha }} |
| 85 | + echo "Image successfully pushed and can be pulled" |
| 86 | +
|
| 87 | + # Deploy to App Engine using pre-built Docker image from GitHub Container Registry |
| 88 | + deploy: |
| 89 | + runs-on: ubuntu-latest |
| 90 | + if: | |
| 91 | + (github.repository == 'PolicyEngine/policyengine-household-api') |
| 92 | + && (github.event.head_commit.message == 'Update PolicyEngine Household API') |
| 93 | + needs: [lint-and-test, build-docker] |
| 94 | + steps: |
| 95 | + - name: Checkout code |
| 96 | + uses: actions/checkout@v4 |
| 97 | + |
| 98 | + - name: Authenticate to Google Cloud |
| 99 | + uses: google-github-actions/auth@v2 |
| 100 | + with: |
| 101 | + credentials_json: ${{ secrets.GCP_SA_KEY }} |
| 102 | + |
| 103 | + - name: Log in to GitHub Container Registry for image verification |
| 104 | + uses: docker/login-action@v2 |
| 105 | + with: |
| 106 | + registry: ghcr.io |
| 107 | + username: ${{ github.actor }} |
| 108 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 109 | + |
| 110 | + - name: Verify image exists before deployment |
| 111 | + env: |
| 112 | + IMAGE_NAME: ${{ env.IMAGE_NAME }} |
| 113 | + IMAGE_TAG: ${{ github.sha }} |
| 114 | + run: .github/scripts/verify-image.sh |
| 115 | + |
| 116 | + - name: Deploy to App Engine |
| 117 | + env: |
| 118 | + IMAGE_NAME: ${{ env.IMAGE_NAME }} |
| 119 | + IMAGE_TAG: ${{ github.sha }} |
| 120 | + SERVICE_ACCOUNT: github-deployment@policyengine-household-api.iam.gserviceaccount.com |
| 121 | + APP_YAML_PATH: ./gcp/policyengine_household_api/app.yaml |
| 122 | + AUTH0_ADDRESS_NO_DOMAIN: ${{ secrets.AUTH0_ADDRESS_NO_DOMAIN }} |
| 123 | + AUTH0_AUDIENCE_NO_DOMAIN: ${{ secrets.AUTH0_AUDIENCE_NO_DOMAIN }} |
| 124 | + USER_ANALYTICS_DB_USERNAME: ${{ secrets.USER_ANALYTICS_DB_USERNAME }} |
| 125 | + USER_ANALYTICS_DB_PASSWORD: ${{ secrets.USER_ANALYTICS_DB_PASSWORD }} |
| 126 | + USER_ANALYTICS_DB_CONNECTION_NAME: ${{ secrets.USER_ANALYTICS_DB_CONNECTION_NAME }} |
| 127 | + ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} |
| 128 | + run: .github/scripts/deploy-app-engine.sh |
| 129 | + |
| 130 | + - name: Set traffic to new version |
| 131 | + env: |
| 132 | + SERVICE_NAME: ${{ env.SERVICE_NAME }} |
| 133 | + VERSION: ${{ github.sha }} |
| 134 | + run: .github/scripts/set-traffic.sh |
| 135 | + |
| 136 | + - name: Verify deployment |
| 137 | + env: |
| 138 | + SERVICE_NAME: ${{ env.SERVICE_NAME }} |
| 139 | + run: .github/scripts/verify-deployment.sh |
0 commit comments