Skip to content

Commit

Permalink
Merge pull request #738 from 0xPolygon/hosted/deployment-update
Browse files Browse the repository at this point in the history
Hosted/deployment update
  • Loading branch information
sshrihar authored Sep 24, 2024
2 parents 5168ccc + ceaefc2 commit 7c78c53
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 138 deletions.
106 changes: 67 additions & 39 deletions .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,69 +32,97 @@ 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'

- 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
Expand All @@ -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
Expand All @@ -115,21 +143,21 @@ 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
uses: nathanvaughn/actions-cloudflare-purge@master
with:
cf_zone: ${{ secrets.CLOUDFLARE_ZONE }}
cf_auth: ${{ secrets.CLOUDFLARE_AUTH_KEY }}
hosts: ${{ env.APP_NAME }}.polygon.technology
hosts: ${{ inputs.core_app }}-${{ inputs.environment }}.polygon.technology
92 changes: 0 additions & 92 deletions .github/workflows/dev_deployment.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
secrets: inherit
12 changes: 8 additions & 4 deletions build_branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import subprocess


HOSTED_SITE_DOMAIN = "docs-dev.polygon.technology"


def install_mkdocs_with_pipenv():
"""
Builds a particular branch site.
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit 7c78c53

Please sign in to comment.