From dcd575f1ea3b1c5e84eb4e50ed9c48da423389c7 Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Mon, 4 Nov 2024 12:16:16 -0800 Subject: [PATCH 01/17] Migrate to reusable workflow --- .github/workflows/cd-prod.yml | 49 -------------------- .github/workflows/ci.yml | 20 +++++++- .github/workflows/{cd-dev.yml => deploy.yml} | 24 +++++----- 3 files changed, 32 insertions(+), 61 deletions(-) delete mode 100644 .github/workflows/cd-prod.yml rename .github/workflows/{cd-dev.yml => deploy.yml} (78%) diff --git a/.github/workflows/cd-prod.yml b/.github/workflows/cd-prod.yml deleted file mode 100644 index 78d2c16..0000000 --- a/.github/workflows/cd-prod.yml +++ /dev/null @@ -1,49 +0,0 @@ -name: Deploy Space2Stats API Prod - -on: - push: - branches: - - main - -permissions: - id-token: write - contents: read - -jobs: - build: - environment: "Space2Stats API Prod" - runs-on: ubuntu-latest - - steps: - - name: Check out repository code - uses: actions/checkout@v2 - - - name: Install AWS CDK - run: npm install -g aws-cdk - - - name: Configure AWS credentials - uses: aws-actions/configure-aws-credentials@v1 - with: - role-to-assume: arn:aws:iam::017820688988:role/Space2Stats-Deploy-Role - aws-region: ${{ vars.CDK_DEFAULT_REGION }} - - - name: Install CDK dependencies - working-directory: ./space2stats_api/cdk - run: | - pip install -r requirements-cdk.txt - - - name: Deploy CDK stack - working-directory: ./space2stats_api/cdk - env: - STAGING: prod - PGHOST: ${{ secrets.PGHOST }} - PGPORT: ${{ secrets.PGPORT }} - PGDATABASE: ${{ secrets.PGDATABASE }} - PGUSER: ${{ secrets.PGUSER }} - PGPASSWORD: ${{ secrets.PGPASSWORD }} - PGTABLENAME: ${{ secrets.PGTABLENAME }} - CDK_CERTIFICATE_ARN: ${{ vars.CDK_CERTIFICATE_ARN }} - CDK_DEFAULT_ACCOUNT: ${{ vars.CDK_DEFAULT_ACCOUNT }} - CDK_DEFAULT_REGION: ${{ vars.CDK_DEFAULT_REGION }} - CDK_DOMAIN_NAME: ${{ vars.CDK_DOMAIN_NAME }} - run: cdk deploy --require-approval never \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0328da9..d438960 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,4 +44,22 @@ jobs: PGUSER: myuser PGPASSWORD: mypassword PGTABLENAME: space2stats - S3_BUCKET_NAME: test-bucket \ No newline at end of file + S3_BUCKET_NAME: test-bucket + + deploy-to-dev: + uses: "./.github/workflows/deploy.yml" + needs: test + if: ${{ github.event_name == 'pull_request' && github.base_ref == 'main' }} + with: + environment: Space2Stats API Dev + stage: dev + secrets: inherit + + deploy-to-production: + uses: "./.github/workflows/deploy.yml" + needs: test + if: ${{ github.event_name == 'push' && github.ref_name == 'main' }} + with: + environment: Space2Stats API Prod + stage: prod + secrets: inherit \ No newline at end of file diff --git a/.github/workflows/cd-dev.yml b/.github/workflows/deploy.yml similarity index 78% rename from .github/workflows/cd-dev.yml rename to .github/workflows/deploy.yml index e332a40..d5829e1 100644 --- a/.github/workflows/cd-dev.yml +++ b/.github/workflows/deploy.yml @@ -1,17 +1,19 @@ -name: Deploy Space2Stats API Staging +name: Deploy on: - pull_request: - branches: - - main - -permissions: - id-token: write - contents: read + workflow_call: + inputs: + environment: + type: string + required: true + stage: + type: string + required: true jobs: build: - environment: "Space2Stats API Dev" + concurrency: ${{ inputs.environment }} + environment: ${{ inputs.environment }} runs-on: ubuntu-latest steps: @@ -35,7 +37,7 @@ jobs: - name: Deploy CDK stack to staging working-directory: ./space2stats_api/cdk env: - STAGE: dev + STAGE: ${{ inputs.stage }} PGHOST: ${{ secrets.PGHOST }} PGPORT: ${{ secrets.PGPORT }} PGDATABASE: ${{ secrets.PGDATABASE }} @@ -46,4 +48,4 @@ jobs: CDK_DEFAULT_ACCOUNT: ${{ vars.CDK_DEFAULT_ACCOUNT }} CDK_DEFAULT_REGION: ${{ vars.CDK_DEFAULT_REGION }} CDK_DOMAIN_NAME: ${{ vars.CDK_DOMAIN_NAME }} - run: cdk deploy --require-approval never \ No newline at end of file + run: cdk deploy --require-approval never From 000fc2d9b0a496071567ab3d3b508c2abe15ef66 Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 11:26:28 -0800 Subject: [PATCH 02/17] Add tooling for PR preview URL --- .github/workflows/ci.yml | 17 ++++++++++++++-- .github/workflows/deploy.yml | 13 +++++++++++- space2stats_api/cdk/aws_stack.py | 35 +++++++++++++++++++------------- space2stats_api/cdk/settings.py | 3 ++- 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d438960..bdc3b36 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,7 +52,7 @@ jobs: if: ${{ github.event_name == 'pull_request' && github.base_ref == 'main' }} with: environment: Space2Stats API Dev - stage: dev + stage: pr-${{ github.event.pull_request.number }} secrets: inherit deploy-to-production: @@ -62,4 +62,17 @@ jobs: with: environment: Space2Stats API Prod stage: prod - secrets: inherit \ No newline at end of file + secrets: inherit + + post-url-to-slack: + needs: deploy-to-dev + steps: + - name: Create or update comment with URL + uses: peter-evans/create-or-update-comment@v4 + with: + issue-number: ${{ github.event.pull_request.number }} + body: | + 🚀 PR deployed to ${{ needs.deploy-to-dev.outputs.get_api_url }} + edit-mode: replace + + # TODO: When PR is merged, destroy CDK Stack for PR Preview \ No newline at end of file diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index d5829e1..a761f42 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -9,12 +9,18 @@ on: stage: type: string required: true + outputs: + api_url: + description: "The URL of the API Gateway" + value: ${{ jobs.build.outputs.api_url }} jobs: build: concurrency: ${{ inputs.environment }} environment: ${{ inputs.environment }} runs-on: ubuntu-latest + outputs: + api_url: ${{ steps.get_api_url.outputs.api_url }} steps: - name: Check out repository code @@ -48,4 +54,9 @@ jobs: CDK_DEFAULT_ACCOUNT: ${{ vars.CDK_DEFAULT_ACCOUNT }} CDK_DEFAULT_REGION: ${{ vars.CDK_DEFAULT_REGION }} CDK_DOMAIN_NAME: ${{ vars.CDK_DOMAIN_NAME }} - run: cdk deploy --require-approval never + run: cdk deploy --require-approval never --outputs-file outputs.json + + - name: Get API Urls + id: get_api_url + run: | + echo "api_url=$(jq -r '."Space2Stats-${{ inputs.stage }}".ApiGatewayUrl' outputs.json)" >> $GITHUB_OUTPUT diff --git a/space2stats_api/cdk/aws_stack.py b/space2stats_api/cdk/aws_stack.py index b6b666d..659762e 100644 --- a/space2stats_api/cdk/aws_stack.py +++ b/space2stats_api/cdk/aws_stack.py @@ -1,4 +1,4 @@ -from aws_cdk import Duration, Stack +from aws_cdk import Duration, Stack, CfnOutput from aws_cdk import aws_apigatewayv2 as apigatewayv2 from aws_cdk import aws_apigatewayv2_integrations as integrations from aws_cdk import aws_certificatemanager as acm @@ -48,13 +48,6 @@ def __init__( self, "Certificate", deployment_settings.CDK_CERTIFICATE_ARN ) - domain_name = apigatewayv2.DomainName( - self, - "DomainName", - domain_name=deployment_settings.CDK_DOMAIN_NAME, - certificate=certificate, - ) - http_api = apigatewayv2.HttpApi( self, "Space2StatsHttpApi", @@ -63,10 +56,24 @@ def __init__( ), ) - apigatewayv2.ApiMapping( - self, - "ApiMapping", - api=http_api, - domain_name=domain_name, - stage=http_api.default_stage, + CfnOutput( + self, + 'ApiGatewayUrl', + key='ApiGatewayUrl', + value=http_api.url, ) + + if deployment_settings.CDK_DOMAIN_NAME: + domain_name = apigatewayv2.DomainName( + self, + "DomainName", + domain_name=deployment_settings.CDK_DOMAIN_NAME, + certificate=certificate, + ) + apigatewayv2.ApiMapping( + self, + "ApiMapping", + api=http_api, + domain_name=domain_name, + stage=http_api.default_stage, + ) diff --git a/space2stats_api/cdk/settings.py b/space2stats_api/cdk/settings.py index 9589b6d..b1dd360 100644 --- a/space2stats_api/cdk/settings.py +++ b/space2stats_api/cdk/settings.py @@ -1,3 +1,4 @@ +from typing import Optional from pydantic_settings import BaseSettings @@ -14,5 +15,5 @@ class DeploymentSettings(BaseSettings): CDK_DEFAULT_ACCOUNT: str CDK_DEFAULT_REGION: str CDK_CERTIFICATE_ARN: str - CDK_DOMAIN_NAME: str + CDK_DOMAIN_NAME: Optional[str] STAGE: str = "dev" From d052675d2cb1dd4d049a6f9eeaafb8fff4029fae Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 11:29:11 -0800 Subject: [PATCH 03/17] Add runs-on --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bdc3b36..7635e01 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -66,6 +66,7 @@ jobs: post-url-to-slack: needs: deploy-to-dev + runs-on: ubuntu-latest steps: - name: Create or update comment with URL uses: peter-evans/create-or-update-comment@v4 From 7a71a4f743c70a64c9b5136959450d8dfffa34b8 Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 11:44:03 -0800 Subject: [PATCH 04/17] Pre-commit --- space2stats_api/cdk/aws_stack.py | 8 ++++---- space2stats_api/cdk/settings.py | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/space2stats_api/cdk/aws_stack.py b/space2stats_api/cdk/aws_stack.py index 659762e..66aaf2f 100644 --- a/space2stats_api/cdk/aws_stack.py +++ b/space2stats_api/cdk/aws_stack.py @@ -1,4 +1,4 @@ -from aws_cdk import Duration, Stack, CfnOutput +from aws_cdk import CfnOutput, Duration, Stack from aws_cdk import aws_apigatewayv2 as apigatewayv2 from aws_cdk import aws_apigatewayv2_integrations as integrations from aws_cdk import aws_certificatemanager as acm @@ -57,9 +57,9 @@ def __init__( ) CfnOutput( - self, - 'ApiGatewayUrl', - key='ApiGatewayUrl', + self, + "ApiGatewayUrl", + key="ApiGatewayUrl", value=http_api.url, ) diff --git a/space2stats_api/cdk/settings.py b/space2stats_api/cdk/settings.py index b1dd360..83044aa 100644 --- a/space2stats_api/cdk/settings.py +++ b/space2stats_api/cdk/settings.py @@ -1,4 +1,5 @@ from typing import Optional + from pydantic_settings import BaseSettings From 1761fd39be8d980fb4762a170c136ad62d4bf50a Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 11:47:46 -0800 Subject: [PATCH 05/17] Refactor triggers --- .github/workflows/ci.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7635e01..783c657 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,10 @@ name: Run Tests -on: [push, pull_request] +on: + push: + branches: + - main + pull_request: jobs: test: @@ -49,7 +53,7 @@ jobs: deploy-to-dev: uses: "./.github/workflows/deploy.yml" needs: test - if: ${{ github.event_name == 'pull_request' && github.base_ref == 'main' }} + if: ${{ github.event_name == 'pull_request' }} with: environment: Space2Stats API Dev stage: pr-${{ github.event.pull_request.number }} From 5a8132f0d41c91c15b8cf560a2a82f59898afcea Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 11:52:03 -0800 Subject: [PATCH 06/17] Bump version --- .github/workflows/deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index a761f42..e95aa76 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -30,7 +30,7 @@ jobs: run: npm install -g aws-cdk - name: Configure AWS credentials - uses: aws-actions/configure-aws-credentials@v1 + uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::017820688988:role/Space2Stats-Deploy-Role aws-region: ${{ vars.CDK_DEFAULT_REGION }} From 7613c5b311ba1942567d09b6629646d5b3eeb8a6 Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 11:55:26 -0800 Subject: [PATCH 07/17] Add permissions --- .github/workflows/deploy.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index e95aa76..7de1b5f 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -14,6 +14,10 @@ on: description: "The URL of the API Gateway" value: ${{ jobs.build.outputs.api_url }} +permissions: + id-token: write + contents: read + jobs: build: concurrency: ${{ inputs.environment }} From df98917a999e763d46c040686fe0743fb84ac720 Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 12:08:19 -0800 Subject: [PATCH 08/17] Fix working dir --- .github/workflows/deploy.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 7de1b5f..914f66d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -62,5 +62,6 @@ jobs: - name: Get API Urls id: get_api_url + working-directory: ./space2stats_api/cdk run: | echo "api_url=$(jq -r '."Space2Stats-${{ inputs.stage }}".ApiGatewayUrl' outputs.json)" >> $GITHUB_OUTPUT From 6289be1c8d47b5cd641fe0005f496a8000990c97 Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 12:19:23 -0800 Subject: [PATCH 09/17] Fix URL output --- .github/workflows/ci.yml | 81 ++++++++++++++++++++---------------- .github/workflows/deploy.yml | 2 +- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 783c657..e84facd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,44 +11,44 @@ jobs: runs-on: ubuntu-latest steps: - - name: Checkout code - uses: actions/checkout@v4 + - name: Checkout code + uses: actions/checkout@v4 - - name: Set up Python - uses: actions/setup-python@v5 - with: - python-version: 3.11 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: 3.11 - - name: Install Poetry - run: | - python -m pip install --upgrade pip - python -m pip install poetry + - name: Install Poetry + run: | + python -m pip install --upgrade pip + python -m pip install poetry - - name: Install dependencies - working-directory: ./space2stats_api/src - run: | - poetry install --with test + - name: Install dependencies + working-directory: ./space2stats_api/src + run: | + poetry install --with test - - name: install lib postgres - uses: nyurik/action-setup-postgis@v2 + - name: install lib postgres + uses: nyurik/action-setup-postgis@v2 - - name: Run pre-commit - working-directory: ./space2stats_api/src - run: | - poetry run pre-commit run --all-files + - name: Run pre-commit + working-directory: ./space2stats_api/src + run: | + poetry run pre-commit run --all-files - - name: Run tests - working-directory: ./space2stats_api/src - run: | - poetry run python -m pytest --benchmark-skip tests - env: - PGHOST: localhost - PGPORT: 5432 - PGDATABASE: mydatabase - PGUSER: myuser - PGPASSWORD: mypassword - PGTABLENAME: space2stats - S3_BUCKET_NAME: test-bucket + - name: Run tests + working-directory: ./space2stats_api/src + run: | + poetry run python -m pytest --benchmark-skip tests + env: + PGHOST: localhost + PGPORT: 5432 + PGDATABASE: mydatabase + PGUSER: myuser + PGPASSWORD: mypassword + PGTABLENAME: space2stats + S3_BUCKET_NAME: test-bucket deploy-to-dev: uses: "./.github/workflows/deploy.yml" @@ -71,13 +71,24 @@ jobs: post-url-to-slack: needs: deploy-to-dev runs-on: ubuntu-latest - steps: + permissions: + pull-requests: write + + steps: + - name: Find Comment + uses: peter-evans/find-comment@v3 + id: find-comment + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: "github-actions[bot]" + body-includes: "🚀 PR deployed to" + - name: Create or update comment with URL uses: peter-evans/create-or-update-comment@v4 with: issue-number: ${{ github.event.pull_request.number }} body: | - 🚀 PR deployed to ${{ needs.deploy-to-dev.outputs.get_api_url }} + 🚀 PR deployed to ${{ needs.deploy-to-dev.outputs.api_url }} edit-mode: replace - # TODO: When PR is merged, destroy CDK Stack for PR Preview \ No newline at end of file + # TODO: When PR is merged, destroy CDK Stack for PR Preview diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 914f66d..fb95c39 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -60,7 +60,7 @@ jobs: CDK_DOMAIN_NAME: ${{ vars.CDK_DOMAIN_NAME }} run: cdk deploy --require-approval never --outputs-file outputs.json - - name: Get API Urls + - name: Get API URL id: get_api_url working-directory: ./space2stats_api/cdk run: | From 4e7b3638c61ab29ef0bea59aaccd32da7ff11ea6 Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 12:24:56 -0800 Subject: [PATCH 10/17] Fix comment find --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e84facd..ce5407d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -81,7 +81,7 @@ jobs: with: issue-number: ${{ github.event.pull_request.number }} comment-author: "github-actions[bot]" - body-includes: "🚀 PR deployed to" + body-includes: "PR deployed to" - name: Create or update comment with URL uses: peter-evans/create-or-update-comment@v4 From d164bfe08093b244a6e134818f495b02711d6f72 Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 13:27:43 -0800 Subject: [PATCH 11/17] Add tooling to tear down PR preview --- .github/workflows/ci.yml | 18 +++++++++++- .github/workflows/destroy.yml | 55 +++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/destroy.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ce5407d..432108b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -91,4 +91,20 @@ jobs: 🚀 PR deployed to ${{ needs.deploy-to-dev.outputs.api_url }} edit-mode: replace - # TODO: When PR is merged, destroy CDK Stack for PR Preview + destroy-pr-preview: + if: ${{ github.event.action == 'closed' }} + uses: "./.github/workflows/deploy.yml" + + post-cleanup-message-to-slack: + needs: destroy-pr-preview + runs-on: ubuntu-latest + permissions: + pull-requests: write + + steps: + - name: Create or update comment with URL + uses: peter-evans/create-or-update-comment@v4 + with: + issue-number: ${{ github.event.pull_request.number }} + body: | + Removed PR Preview Environment diff --git a/.github/workflows/destroy.yml b/.github/workflows/destroy.yml new file mode 100644 index 0000000..a4d83b7 --- /dev/null +++ b/.github/workflows/destroy.yml @@ -0,0 +1,55 @@ +name: Destroy Preview Environment + +on: + workflow_call: + inputs: + environment: + type: string + required: true + stage: + type: string + required: true + +permissions: + id-token: write + contents: read + +jobs: + build: + concurrency: ${{ inputs.environment }} + environment: ${{ inputs.environment }} + runs-on: ubuntu-latest + + steps: + - name: Check out repository code + uses: actions/checkout@v2 + + - name: Install AWS CDK + run: npm install -g aws-cdk + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: arn:aws:iam::017820688988:role/Space2Stats-Deploy-Role + aws-region: ${{ vars.CDK_DEFAULT_REGION }} + + - name: Install CDK dependencies + working-directory: ./space2stats_api/cdk + run: | + pip install -r requirements-cdk.txt + + - name: Deploy CDK stack to staging + working-directory: ./space2stats_api/cdk + env: + STAGE: ${{ inputs.stage }} + PGHOST: ${{ secrets.PGHOST }} + PGPORT: ${{ secrets.PGPORT }} + PGDATABASE: ${{ secrets.PGDATABASE }} + PGUSER: ${{ secrets.PGUSER }} + PGPASSWORD: ${{ secrets.PGPASSWORD }} + PGTABLENAME: ${{ secrets.PGTABLENAME }} + CDK_CERTIFICATE_ARN: ${{ vars.CDK_CERTIFICATE_ARN }} + CDK_DEFAULT_ACCOUNT: ${{ vars.CDK_DEFAULT_ACCOUNT }} + CDK_DEFAULT_REGION: ${{ vars.CDK_DEFAULT_REGION }} + CDK_DOMAIN_NAME: ${{ vars.CDK_DOMAIN_NAME }} + run: cdk destroy --require-approval never From 360d07f4ad12bf0325e2f69320b1e59621842d76 Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 13:30:53 -0800 Subject: [PATCH 12/17] Fix commenting --- .github/workflows/ci.yml | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 432108b..08700e5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -81,20 +81,22 @@ jobs: with: issue-number: ${{ github.event.pull_request.number }} comment-author: "github-actions[bot]" - body-includes: "PR deployed to" + body-includes: "PR Deployment Details:" - name: Create or update comment with URL uses: peter-evans/create-or-update-comment@v4 with: issue-number: ${{ github.event.pull_request.number }} + comment-id: ${{ steps.find-comment.outputs.comment-id }} body: | + PR Deployment Details: 🚀 PR deployed to ${{ needs.deploy-to-dev.outputs.api_url }} edit-mode: replace destroy-pr-preview: if: ${{ github.event.action == 'closed' }} uses: "./.github/workflows/deploy.yml" - + post-cleanup-message-to-slack: needs: destroy-pr-preview runs-on: ubuntu-latest @@ -102,9 +104,19 @@ jobs: pull-requests: write steps: + - name: Find Comment + uses: peter-evans/find-comment@v3 + id: find-comment + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: "github-actions[bot]" + body-includes: "PR Deployment Details:" + - name: Create or update comment with URL uses: peter-evans/create-or-update-comment@v4 with: issue-number: ${{ github.event.pull_request.number }} + comment-id: ${{ steps.find-comment.outputs.comment-id }} body: | - Removed PR Preview Environment + Removed PR Preview Environment. + edit-mode: append From 3a7587097a4598da0690da25657dd16b54338cad Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 13:34:49 -0800 Subject: [PATCH 13/17] Fix --- .github/workflows/ci.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 08700e5..3967873 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,18 +51,18 @@ jobs: S3_BUCKET_NAME: test-bucket deploy-to-dev: + if: ${{ github.event_name == 'pull_request' }} uses: "./.github/workflows/deploy.yml" needs: test - if: ${{ github.event_name == 'pull_request' }} with: environment: Space2Stats API Dev stage: pr-${{ github.event.pull_request.number }} secrets: inherit deploy-to-production: + if: ${{ github.event_name == 'push' && github.ref_name == 'main' }} uses: "./.github/workflows/deploy.yml" needs: test - if: ${{ github.event_name == 'push' && github.ref_name == 'main' }} with: environment: Space2Stats API Prod stage: prod @@ -96,6 +96,10 @@ jobs: destroy-pr-preview: if: ${{ github.event.action == 'closed' }} uses: "./.github/workflows/deploy.yml" + with: + environment: Space2Stats API Dev + stage: pr-${{ github.event.pull_request.number }} + secrets: inherit post-cleanup-message-to-slack: needs: destroy-pr-preview From 675823044021e9e307f20d2c4aaf3fb1073a240a Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 14:05:12 -0800 Subject: [PATCH 14/17] Run tests on all pushes --- .github/workflows/ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3967873..4544323 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,8 +2,6 @@ name: Run Tests on: push: - branches: - - main pull_request: jobs: From ec0f7f405a23006426ee08ac57fef88fcfa7b579 Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 14:29:23 -0800 Subject: [PATCH 15/17] Refactor --- .github/workflows/ci.yml | 51 ++-------------------------- .github/workflows/deploy.yml | 62 ++++++++++++++++++++++------------- .github/workflows/destroy.yml | 51 ++++++++++++++++++++-------- 3 files changed, 79 insertions(+), 85 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4544323..235d004 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,6 +55,7 @@ jobs: with: environment: Space2Stats API Dev stage: pr-${{ github.event.pull_request.number }} + pr_number: ${{ github.event.pull_request.number }} secrets: inherit deploy-to-production: @@ -66,59 +67,11 @@ jobs: stage: prod secrets: inherit - post-url-to-slack: - needs: deploy-to-dev - runs-on: ubuntu-latest - permissions: - pull-requests: write - - steps: - - name: Find Comment - uses: peter-evans/find-comment@v3 - id: find-comment - with: - issue-number: ${{ github.event.pull_request.number }} - comment-author: "github-actions[bot]" - body-includes: "PR Deployment Details:" - - - name: Create or update comment with URL - uses: peter-evans/create-or-update-comment@v4 - with: - issue-number: ${{ github.event.pull_request.number }} - comment-id: ${{ steps.find-comment.outputs.comment-id }} - body: | - PR Deployment Details: - 🚀 PR deployed to ${{ needs.deploy-to-dev.outputs.api_url }} - edit-mode: replace - destroy-pr-preview: if: ${{ github.event.action == 'closed' }} uses: "./.github/workflows/deploy.yml" with: environment: Space2Stats API Dev stage: pr-${{ github.event.pull_request.number }} - secrets: inherit - post-cleanup-message-to-slack: - needs: destroy-pr-preview - runs-on: ubuntu-latest - permissions: - pull-requests: write - - steps: - - name: Find Comment - uses: peter-evans/find-comment@v3 - id: find-comment - with: - issue-number: ${{ github.event.pull_request.number }} - comment-author: "github-actions[bot]" - body-includes: "PR Deployment Details:" - - - name: Create or update comment with URL - uses: peter-evans/create-or-update-comment@v4 - with: - issue-number: ${{ github.event.pull_request.number }} - comment-id: ${{ steps.find-comment.outputs.comment-id }} - body: | - Removed PR Preview Environment. - edit-mode: append + secrets: inherit diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index fb95c39..4c6c9b2 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -6,25 +6,23 @@ on: environment: type: string required: true - stage: + stage: type: string required: true - outputs: - api_url: - description: "The URL of the API Gateway" - value: ${{ jobs.build.outputs.api_url }} + pr-number: + type: number + required: false permissions: id-token: write contents: read + pull-requests: write jobs: build: concurrency: ${{ inputs.environment }} environment: ${{ inputs.environment }} runs-on: ubuntu-latest - outputs: - api_url: ${{ steps.get_api_url.outputs.api_url }} steps: - name: Check out repository code @@ -38,30 +36,50 @@ jobs: with: role-to-assume: arn:aws:iam::017820688988:role/Space2Stats-Deploy-Role aws-region: ${{ vars.CDK_DEFAULT_REGION }} - + - name: Install CDK dependencies working-directory: ./space2stats_api/cdk run: | - pip install -r requirements-cdk.txt + pip install -r requirements-cdk.txt - name: Deploy CDK stack to staging working-directory: ./space2stats_api/cdk env: - STAGE: ${{ inputs.stage }} - PGHOST: ${{ secrets.PGHOST }} - PGPORT: ${{ secrets.PGPORT }} - PGDATABASE: ${{ secrets.PGDATABASE }} - PGUSER: ${{ secrets.PGUSER }} - PGPASSWORD: ${{ secrets.PGPASSWORD }} - PGTABLENAME: ${{ secrets.PGTABLENAME }} - CDK_CERTIFICATE_ARN: ${{ vars.CDK_CERTIFICATE_ARN }} - CDK_DEFAULT_ACCOUNT: ${{ vars.CDK_DEFAULT_ACCOUNT }} - CDK_DEFAULT_REGION: ${{ vars.CDK_DEFAULT_REGION }} - CDK_DOMAIN_NAME: ${{ vars.CDK_DOMAIN_NAME }} + STAGE: ${{ inputs.stage }} + PGHOST: ${{ secrets.PGHOST }} + PGPORT: ${{ secrets.PGPORT }} + PGDATABASE: ${{ secrets.PGDATABASE }} + PGUSER: ${{ secrets.PGUSER }} + PGPASSWORD: ${{ secrets.PGPASSWORD }} + PGTABLENAME: ${{ secrets.PGTABLENAME }} + CDK_CERTIFICATE_ARN: ${{ vars.CDK_CERTIFICATE_ARN }} + CDK_DEFAULT_ACCOUNT: ${{ vars.CDK_DEFAULT_ACCOUNT }} + CDK_DEFAULT_REGION: ${{ vars.CDK_DEFAULT_REGION }} + CDK_DOMAIN_NAME: ${{ vars.CDK_DOMAIN_NAME }} run: cdk deploy --require-approval never --outputs-file outputs.json - name: Get API URL - id: get_api_url + id: get-api-url working-directory: ./space2stats_api/cdk run: | - echo "api_url=$(jq -r '."Space2Stats-${{ inputs.stage }}".ApiGatewayUrl' outputs.json)" >> $GITHUB_OUTPUT + echo "api-url=$(jq -r '."Space2Stats-${{ inputs.stage }}".ApiGatewayUrl' outputs.json)" >> $GITHUB_OUTPUT + + - name: Find Comment + uses: peter-evans/find-comment@v3 + id: find-comment + if: ${{ inputs.pr-number }} + with: + issue-number: ${{ inputs.pr-number }} + comment-author: "github-actions[bot]" + body-includes: "PR Deployment Details:" + + - name: Create or update comment with URL + uses: peter-evans/create-or-update-comment@v4 + if: ${{ inputs.pr-number }} + with: + issue-number: ${{ inputs.pr-number }} + comment-id: ${{ steps.find-comment.outputs.comment-id }} + body: | + PR Deployment Details: + 🚀 PR deployed to ${{ steps.get-api-url.outputs.api-url }} + edit-mode: replace diff --git a/.github/workflows/destroy.yml b/.github/workflows/destroy.yml index a4d83b7..d4b59bc 100644 --- a/.github/workflows/destroy.yml +++ b/.github/workflows/destroy.yml @@ -6,13 +6,17 @@ on: environment: type: string required: true - stage: + stage: type: string required: true + pr-number: + type: number + required: false permissions: id-token: write contents: read + pull-requests: write jobs: build: @@ -32,24 +36,43 @@ jobs: with: role-to-assume: arn:aws:iam::017820688988:role/Space2Stats-Deploy-Role aws-region: ${{ vars.CDK_DEFAULT_REGION }} - + - name: Install CDK dependencies working-directory: ./space2stats_api/cdk run: | - pip install -r requirements-cdk.txt + pip install -r requirements-cdk.txt - name: Deploy CDK stack to staging working-directory: ./space2stats_api/cdk env: - STAGE: ${{ inputs.stage }} - PGHOST: ${{ secrets.PGHOST }} - PGPORT: ${{ secrets.PGPORT }} - PGDATABASE: ${{ secrets.PGDATABASE }} - PGUSER: ${{ secrets.PGUSER }} - PGPASSWORD: ${{ secrets.PGPASSWORD }} - PGTABLENAME: ${{ secrets.PGTABLENAME }} - CDK_CERTIFICATE_ARN: ${{ vars.CDK_CERTIFICATE_ARN }} - CDK_DEFAULT_ACCOUNT: ${{ vars.CDK_DEFAULT_ACCOUNT }} - CDK_DEFAULT_REGION: ${{ vars.CDK_DEFAULT_REGION }} - CDK_DOMAIN_NAME: ${{ vars.CDK_DOMAIN_NAME }} + STAGE: ${{ inputs.stage }} + PGHOST: ${{ secrets.PGHOST }} + PGPORT: ${{ secrets.PGPORT }} + PGDATABASE: ${{ secrets.PGDATABASE }} + PGUSER: ${{ secrets.PGUSER }} + PGPASSWORD: ${{ secrets.PGPASSWORD }} + PGTABLENAME: ${{ secrets.PGTABLENAME }} + CDK_CERTIFICATE_ARN: ${{ vars.CDK_CERTIFICATE_ARN }} + CDK_DEFAULT_ACCOUNT: ${{ vars.CDK_DEFAULT_ACCOUNT }} + CDK_DEFAULT_REGION: ${{ vars.CDK_DEFAULT_REGION }} + CDK_DOMAIN_NAME: ${{ vars.CDK_DOMAIN_NAME }} run: cdk destroy --require-approval never + + - name: Find Comment + uses: peter-evans/find-comment@v3 + id: find-comment + if: ${{ inputs.pr-number }} + with: + issue-number: ${{ inputs.pr-number }} + comment-author: "github-actions[bot]" + body-includes: "PR Deployment Details:" + + - name: Create or update comment with URL + uses: peter-evans/create-or-update-comment@v4 + if: ${{ inputs.pr-number }} + with: + issue-number: ${{ inputs.pr-number }} + comment-id: ${{ steps.find-comment.outputs.comment-id }} + body: | + Removed PR Preview Environment. + edit-mode: append From f223e770fed9d716b70fcc54a82c3f8fa507e4db Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 14:50:41 -0800 Subject: [PATCH 16/17] Fix --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 235d004..6974e8a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,7 +55,7 @@ jobs: with: environment: Space2Stats API Dev stage: pr-${{ github.event.pull_request.number }} - pr_number: ${{ github.event.pull_request.number }} + pr-number: ${{ github.event.pull_request.number }} secrets: inherit deploy-to-production: From 5288c891b3c3f537f3abff1f8ed719e0f24552be Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Tue, 5 Nov 2024 14:52:54 -0800 Subject: [PATCH 17/17] Expand events --- .github/workflows/ci.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6974e8a..3be9e3a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,6 +3,11 @@ name: Run Tests on: push: pull_request: + types: + - opened + - synchronize + - reopened + - closed jobs: test: