diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml index eaba98a36..40b05931f 100644 --- a/.github/workflows/build_and_deploy.yml +++ b/.github/workflows/build_and_deploy.yml @@ -32,46 +32,72 @@ on: default: "frontend-dev-ecs-cluster" jobs: - build_site_data: - name: ${{ inputs.environment }} deployment + check_files_changed: runs-on: ubuntu-latest environment: ${{ inputs.environment }} permissions: id-token: write contents: write - env: - AWS_REGION: ${{ inputs.region }} - ECR_REPOSITORY: ${{ inputs.core_app }}-${{ inputs.environment }}-ecr - ECS_SERVICE: ${{ inputs.core_app }}-${{ inputs.environment }}-ecs-service - ECS_CLUSTER: frontend-${{ inputs.environment }}-ecs-cluster - ECS_TASK_DEFINITION: ${{ inputs.task_definition }} - APP_NAME: ${{ inputs.core_app }}-${{ inputs.environment }} + outputs: + FILES_CHANGED: ${{ steps.changes.outputs.FILES_CHANGED }} steps: + - name: Install GitHub CLI + run: | + (type -p wget >/dev/null || (sudo apt update && sudo apt-get install wget -y)) \ + && sudo mkdir -p -m 755 /etc/apt/keyrings \ + && wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \ + && sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \ + && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \ + && sudo apt update \ + && sudo apt install gh -y + + - name: Authenticate GitHub CLI + run: gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}" + - name: Checkout Code Repository uses: actions/checkout@v3 with: - fetch-depth: 0 + fetch-depth: 2 - name: Get list of changed files id: changes run: | changed_files=$(git diff --name-only HEAD^ HEAD) - echo "Changed files: $changed_files" - echo "FILES_CHANGED=$changed_files" >> "$GITHUB_OUTPUT" + num_changed_files=$(echo "$changed_files" | wc -l) + echo "FILES_CHANGED=$num_changed_files" >> "$GITHUB_OUTPUT" - - name: Stop if no files changed - if: ${{ steps.changes.outputs.FILES_CHANGED == '' }} + check_pr_open: + runs-on: ubuntu-latest + environment: ${{ inputs.environment }} + needs: [check_files_changed] + if: needs.check_files_changed.outputs.FILES_CHANGED != '' && startsWith(github.ref, 'refs/heads/hosted/') + permissions: + id-token: write + contents: write + outputs: + PR_OPEN: ${{ steps.check_pr.outputs.PR_OPEN }} + steps: + - name: Check for open PR + id: check_pr run: | - echo "No files were changed, skipping build." - exit 0 - - - name: Configure AWS credentials - uses: aws-actions/configure-aws-credentials@v4 - with: - aws-region: ${{ env.AWS_REGION }} - role-to-assume: arn:aws:iam::${{ inputs.account_number }}:role/${{ env.APP_NAME }}-GithubActionsRole - role-session-name: GithubActionsSession + BRANCH_NAME=$(echo "${GITHUB_REF}" | cut -d'/' -f4) + pr_number=$(gh pr list --head "hosted/$BRANCH_NAME" --state open --json number --jq '.[0].number') + echo "PR_OPEN=$pr_number" >> "$GITHUB_OUTPUT" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + build_and_deploy: + runs-on: ubuntu-latest + environment: ${{ inputs.environment }} + needs: [check_files_changed, check_pr_open] + if: | + needs.check_files_changed.outputs.FILES_CHANGED != '' && github.ref == 'refs/heads/dev' || + needs.check_pr_open.outputs.PR_OPEN != '' && needs.check_files_changed.outputs.FILES_CHANGED != '' && + startsWith(github.ref, 'refs/heads/hosted/') + permissions: + id-token: write + contents: write + steps: - uses: actions/setup-python@v4 with: python-version: '3.11' @@ -79,22 +105,24 @@ jobs: - name: Install pipenv run: pip install pipenv - - name: Install GitHub CLI - run: | - (type -p wget >/dev/null || (sudo apt update && sudo apt-get install wget -y)) \ - && sudo mkdir -p -m 755 /etc/apt/keyrings \ - && wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \ - && sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \ - && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \ - && sudo apt update \ - && sudo apt install gh -y + - name: Checkout Code Repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 - name: Authenticate GitHub CLI run: gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}" - name: Build Site run: | - python build_branches.py + python build_branches.py + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-region: ${{ inputs.region }} + role-to-assume: arn:aws:iam::${{ inputs.account_number }}:role/${{ inputs.core_app }}-${{ inputs.environment }}-GithubActionsRole + role-session-name: GithubActionsSession - name: Login to Amazon ECR id: login-ecr @@ -105,7 +133,7 @@ jobs: env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} IMAGE_TAG: ${{ github.sha }}-${{ github.run_number }} - ECR_REPOSITORY: ${{ env.APP_NAME }}-ecr + ECR_REPOSITORY: ${{ inputs.core_app }}-${{ inputs.environment }}-ecr run: | docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f Dockerfile.review . docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG @@ -115,16 +143,16 @@ jobs: id: task-def uses: aws-actions/amazon-ecs-render-task-definition@v1 with: - task-definition: ${{ env.ECS_TASK_DEFINITION }} - container-name: ${{ env.APP_NAME }} + task-definition: ${{ inputs.task_definition }} + container-name: ${{ inputs.core_app }}-${{ inputs.environment }} image: ${{ steps.build-image.outputs.image }} - name: Deploy Amazon ECS task definition uses: aws-actions/amazon-ecs-deploy-task-definition@v1 with: task-definition: ${{ steps.task-def.outputs.task-definition }} - service: ${{ env.ECS_SERVICE }} - cluster: ${{ env.ECS_CLUSTER }} + service: ${{ inputs.core_app }}-${{ inputs.environment }}-ecs-service + cluster: frontend-${{ inputs.environment }}-ecs-cluster wait-for-service-stability: true - name: Cloudflare Cache Purge @@ -132,4 +160,4 @@ jobs: with: cf_zone: ${{ secrets.CLOUDFLARE_ZONE }} cf_auth: ${{ secrets.CLOUDFLARE_AUTH_KEY }} - hosts: ${{ env.APP_NAME }}.polygon.technology \ No newline at end of file + hosts: ${{ inputs.core_app }}-${{ inputs.environment }}.polygon.technology \ No newline at end of file diff --git a/.github/workflows/dev_deployment.yml b/.github/workflows/dev_deployment.yml deleted file mode 100644 index 7f8c664c5..000000000 --- a/.github/workflows/dev_deployment.yml +++ /dev/null @@ -1,92 +0,0 @@ -name: Docs Dev Deployment -on: - #schedule: - # - cron: '0 */12 * * *' - push: - branches: ['dev'] - pull_request: - branches: ['dev'] - workflow_dispatch: - -jobs: - deploy_staging: - name: Dev Deployment - permissions: - id-token: write - contents: write - environment: dev - runs-on: ubuntu-latest - env: - AWS_REGION: eu-west-1 - ECR_REPOSITORY: docs-dev-ecr - ECS_SERVICE: docs-dev-ecs-service - ECS_CLUSTER: frontend-dev-ecs-cluster - ECS_TASK_DEFINITION: .github/assets/dev-taskdef.json - CONTAINER_NAME: docs-dev - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - #ref: dev - fetch-depth: 0 - submodules: true - - - name: Verify branch - run: | - echo "Current branch: $(git rev-parse --abbrev-ref HEAD)" - echo "GitHub ref: ${{ github.ref }}" - - - name: Configure AWS credentials - uses: aws-actions/configure-aws-credentials@v1-node16 - with: - aws-region: ${{ env.AWS_REGION }} - role-to-assume: arn:aws:iam::058264511034:role/docs-dev-GithubActionsRole - role-session-name: GithubActionsSession - - - name: Login to Amazon ECR - id: login-ecr - uses: aws-actions/amazon-ecr-login@v1 - - - name: Use Python - uses: actions/setup-python@v4 - with: - python-version: '3.12' - - - name: pip Install - run: pip install -r requirements.txt --no-cache-dir - - - name: Build mkdocs - run: mkdocs build - - - name: Build, tag, and push image to Amazon ECR - id: build-image - env: - ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} - IMAGE_TAG: ${{ github.sha }}-${{ github.run_number }} - run: | - docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f .github/assets/Dockerfile.nginx . - docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG - echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT - - - name: Fill in the new image ID in the Amazon ECS task definition - id: task-def - uses: aws-actions/amazon-ecs-render-task-definition@v1 - with: - task-definition: ${{ env.ECS_TASK_DEFINITION }} - container-name: ${{ env.CONTAINER_NAME }} - image: ${{ steps.build-image.outputs.image }} - - - name: Deploy Amazon ECS task definition - uses: aws-actions/amazon-ecs-deploy-task-definition@v1 - with: - task-definition: ${{ steps.task-def.outputs.task-definition }} - service: ${{ env.ECS_SERVICE }} - cluster: ${{ env.ECS_CLUSTER }} - wait-for-service-stability: true - - - name: Cloudflare Cache Purge - uses: nathanvaughn/actions-cloudflare-purge@master - with: - cf_zone: ${{ secrets.CLOUDFLARE_ZONE }} - cf_auth: ${{ secrets.CLOUDFLARE_AUTH_KEY }} - hosts: docs-dev.polygon.technology diff --git a/.github/workflows/pr-deployment.yml b/.github/workflows/hosted-dev-deployment.yml similarity index 57% rename from .github/workflows/pr-deployment.yml rename to .github/workflows/hosted-dev-deployment.yml index 418142f31..849749a59 100644 --- a/.github/workflows/pr-deployment.yml +++ b/.github/workflows/hosted-dev-deployment.yml @@ -1,14 +1,14 @@ -name: hosted branch pr deployment +name: Hosted PR and Dev Deployment on: pull_request: types: [opened, edited, reopened] push: branches: - dev + - hosted/** workflow_dispatch: jobs: deploy: - if: startsWith(github.head_ref, 'hosted/') || github.ref == 'refs/heads/dev' uses: ./.github/workflows/build_and_deploy.yml - secrets: inherit \ No newline at end of file + secrets: inherit diff --git a/build_branches.py b/build_branches.py index 0a820819c..8cd8c2008 100755 --- a/build_branches.py +++ b/build_branches.py @@ -4,6 +4,9 @@ import subprocess +HOSTED_SITE_DOMAIN = "docs-dev.polygon.technology" + + def install_mkdocs_with_pipenv(): """ Builds a particular branch site. @@ -72,15 +75,15 @@ def clone_data_to_branch_folder(branch_name, remote_url, parent_dir, pr_number=N os.chdir(parent_dir) -def update_pr_description(pr_number): +def update_pr_description(pr_number:str): """ Updates PR description by adding the url to access the hosted environment under dev if it does not already exist in the definition :param pr_number: PR number for the branch hosting website """ - command = ["gh", "pr", "view", pr_number, "--json", "body", "--jq", "'.body'"] + command = ["gh", "pr", "view", pr_number, "--json", "body", "--jq", ".body"] pr_description = subprocess.run(command, capture_output=True, text=True).stdout.strip() - hosted_url = f"docs-dev.polygon.technology/{pr_number}" + hosted_url = f"{HOSTED_SITE_DOMAIN}/{pr_number}" if hosted_url not in pr_description: new_pr_description = f"Hosted url: [{hosted_url}](https://{hosted_url})\n" + pr_description command = ["gh", "pr", "edit", pr_number, "--body", new_pr_description] @@ -106,7 +109,7 @@ def process_branch_folders(): clone_data_to_branch_folder("dev", remote_url, parent_dir, "dev") pr_numbers = [] for branch_data in branches_data: - if not branch_data["headRefName"].startswith("hosted/"): + if not branch_data["headRefName"].startswith("hosted/") or not branch_data.get("number"): continue pr_number = str(branch_data["number"]) clone_data_to_branch_folder(branch_data["headRefName"], remote_url, parent_dir, pr_number) @@ -132,6 +135,7 @@ def update_nginx_config(pr_numbers): }} """ nginx_location_blocks += location_block + print(f"Hosted site: https://{HOSTED_SITE_DOMAIN}/{pr_number}") with open(config_file, "r+") as f: content = f.read()