Skip to content

Commit 60f4ed0

Browse files
Merge branch 'main' of https://github.com/0xPolygon/polygon-docs into cdk/ares_components
2 parents 24a7287 + 7165de9 commit 60f4ed0

File tree

73 files changed

+1737
-3036
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1737
-3036
lines changed

.github/assets/dev-taskdef.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
"networkMode": "awsvpc",
5454
"memory": "1024",
5555
"cpu": "512",
56-
"executionRoleArn": "arn:aws:iam::605436358845:role/docs-dev-TaskRole",
56+
"executionRoleArn": "arn:aws:iam::058264511034:role/docs-dev-TaskRole",
5757
"family": "docs-dev-taskdefinition",
58-
"taskRoleArn": "arn:aws:iam::605436358845:role/docs-dev-TaskRole",
58+
"taskRoleArn": "arn:aws:iam::058264511034:role/docs-dev-TaskRole",
5959
"runtimePlatform": {
6060
"operatingSystemFamily": "LINUX"
6161
},
@@ -81,7 +81,7 @@
8181
},
8282
{
8383
"key": "IAC",
84-
"value": "terraform-workspace-aws-dev-applications-eu-west-1-apps-docs-dev-polygon-technology"
84+
"value": "terraform-workspace-aws-dev-apps-eu-west-1-apps-docs-dev-polygon-technology"
8585
},
8686
{
8787
"key": "Team",
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
on:
2+
workflow_call:
3+
inputs:
4+
environment:
5+
required: false
6+
type: string
7+
default: "dev"
8+
core_app:
9+
required: false
10+
type: string
11+
description: "Core app name"
12+
default: "docs"
13+
account_number:
14+
required: false
15+
type: string
16+
description: "AWS Account number for deployment"
17+
default: "058264511034"
18+
region:
19+
required: false
20+
type: string
21+
description: "AWS region for deployment"
22+
default: "eu-west-1"
23+
task_definition:
24+
required: false
25+
type: string
26+
description: "Task Definition path for deployment"
27+
default: ".github/assets/dev-taskdef.json"
28+
cluster_name:
29+
required: false
30+
type: string
31+
description: "Cluster name for deployment"
32+
default: "frontend-dev-ecs-cluster"
33+
34+
jobs:
35+
build_and_deploy:
36+
runs-on: ubuntu-latest
37+
environment: ${{ inputs.environment }}
38+
permissions:
39+
id-token: write
40+
contents: write
41+
pull-requests: write
42+
steps:
43+
- uses: actions/setup-python@v4
44+
with:
45+
python-version: '3.11'
46+
47+
- name: Install pipenv
48+
run: pip install pipenv
49+
50+
- name: Checkout Code Repository
51+
uses: actions/checkout@v3
52+
with:
53+
fetch-depth: 0
54+
55+
- name: Authenticate GitHub CLI
56+
run: gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
57+
58+
- name: Build Site
59+
run: |
60+
python build_branches.py
61+
62+
- name: Configure AWS credentials
63+
uses: aws-actions/configure-aws-credentials@v4
64+
with:
65+
aws-region: ${{ inputs.region }}
66+
role-to-assume: arn:aws:iam::${{ inputs.account_number }}:role/${{ inputs.core_app }}-${{ inputs.environment }}-GithubActionsRole
67+
role-session-name: GithubActionsSession
68+
69+
- name: Login to Amazon ECR
70+
id: login-ecr
71+
uses: aws-actions/amazon-ecr-login@v1
72+
73+
- name: Build, tag, and push image to Amazon ECR
74+
id: build-image
75+
env:
76+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
77+
IMAGE_TAG: ${{ github.sha }}-${{ github.run_number }}
78+
ECR_REPOSITORY: ${{ inputs.core_app }}-${{ inputs.environment }}-ecr
79+
run: |
80+
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f Dockerfile.review .
81+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
82+
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
83+
84+
- name: Fill in the new image ID in the Amazon ECS task definition
85+
id: task-def
86+
uses: aws-actions/amazon-ecs-render-task-definition@v1
87+
with:
88+
task-definition: ${{ inputs.task_definition }}
89+
container-name: ${{ inputs.core_app }}-${{ inputs.environment }}
90+
image: ${{ steps.build-image.outputs.image }}
91+
92+
- name: Deploy Amazon ECS task definition
93+
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
94+
with:
95+
task-definition: ${{ steps.task-def.outputs.task-definition }}
96+
service: ${{ inputs.core_app }}-${{ inputs.environment }}-ecs-service
97+
cluster: frontend-${{ inputs.environment }}-ecs-cluster
98+
wait-for-service-stability: true
99+
100+
- name: Cloudflare Cache Purge
101+
uses: nathanvaughn/actions-cloudflare-purge@master
102+
with:
103+
cf_zone: ${{ secrets.CLOUDFLARE_ZONE }}
104+
cf_auth: ${{ secrets.CLOUDFLARE_AUTH_KEY }}
105+
hosts: ${{ inputs.core_app }}-${{ inputs.environment }}.polygon.technology

.github/workflows/dev_deployment.yml

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Hosted PR and Dev Deployment
2+
on:
3+
pull_request:
4+
types: [opened, reopened, synchronize]
5+
push:
6+
branches:
7+
- dev
8+
workflow_dispatch:
9+
10+
jobs:
11+
deploy:
12+
if: github.event_name == 'push' || startsWith(github.head_ref, 'hosted/')
13+
uses: ./.github/workflows/build_and_deploy.yml
14+
secrets: inherit

Dockerfile.review

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM nginx:alpine
2+
3+
COPY nginx.conf /etc/nginx/nginx.conf
4+
COPY app /app
5+
6+
WORKDIR /app
7+
EXPOSE 80
8+
CMD ["nginx", "-g", "daemon off;"]

build_branches.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import ast
2+
import os
3+
import shutil
4+
import subprocess
5+
6+
7+
HOSTED_SITE_DOMAIN = "docs-dev.polygon.technology"
8+
9+
10+
def install_mkdocs_with_pipenv():
11+
"""
12+
Builds a particular branch site.
13+
Having a varying set of requirements can be handled by having each branch
14+
build their dependencies and then running mkdocs build.
15+
"""
16+
folder = os.getcwd()
17+
subprocess.run(["pipenv", "install", "--site-packages"], cwd=folder)
18+
subprocess.run(["pipenv", "install", "-r", "requirements.txt"], cwd=folder)
19+
subprocess.run(["pipenv", "run", "mkdocs", "build"], cwd=folder)
20+
21+
def copy_folder(source_dir, target_dir):
22+
"""
23+
Copies contents from source directory to target directory
24+
:param source_dir: Source directory from which contents are to be copied
25+
:param target_dir: Target Directory where the contents are copied to.
26+
"""
27+
os.makedirs(target_dir, exist_ok=True)
28+
29+
for item in os.listdir(source_dir):
30+
source_path = os.path.join(source_dir, item)
31+
target_path = os.path.join(target_dir, item)
32+
33+
if os.path.isdir(source_path):
34+
shutil.copytree(source_path, target_path, dirs_exist_ok=True)
35+
else:
36+
if os.path.exists(target_path):
37+
os.remove(target_path)
38+
shutil.copy2(source_path, target_path)
39+
40+
def delete_folders(folder_paths):
41+
"""
42+
Cleans existing folders for app and branches before executing the builds
43+
:param folder_paths: List of folders to be deleted under the current working directory
44+
"""
45+
for folder_path in folder_paths:
46+
try:
47+
shutil.rmtree(folder_path)
48+
print(f"Folder {folder_path} deletion successful.")
49+
except OSError as e:
50+
print(f"Error deleting folder: {e}")
51+
52+
def clone_data_to_branch_folder(branch_name, remote_url, parent_dir, pr_number=None):
53+
"""
54+
Clones data to branch folder in branch/<PR Number> or branch/dev folder
55+
:param branch_name: Branch to clone and build
56+
:param remote_url: Remote url for the git repository
57+
:param parent_dir: Parent directory to get context of where data is stored
58+
:param pr_number: PR number for the branch to host data into the folder
59+
"""
60+
common_dir = "branch"
61+
target_path = os.path.join(common_dir, pr_number)
62+
os.makedirs(target_path, exist_ok=True)
63+
os.chdir(target_path)
64+
subprocess.run(["git", "init"])
65+
subprocess.run(["git", "remote", "add", "origin", remote_url])
66+
print(f"Checking out branch {branch_name}")
67+
subprocess.run(["git", "fetch", "--depth", "1", "origin", branch_name])
68+
subprocess.run([
69+
"git", "checkout", "-b", branch_name, "--track",
70+
f"origin/{branch_name}"
71+
])
72+
install_mkdocs_with_pipenv()
73+
source_dir = os.path.join(os.getcwd(), "site")
74+
copy_folder(source_dir, os.path.join(parent_dir, "app", pr_number))
75+
os.chdir(parent_dir)
76+
77+
78+
def update_pr_description(pr_number:str):
79+
"""
80+
Updates PR description by adding the url to access the hosted environment under dev
81+
if it does not already exist in the definition
82+
:param pr_number: PR number for the branch hosting website
83+
"""
84+
command = ["gh", "pr", "view", pr_number, "--json", "body", "--jq", ".body"]
85+
pr_description = subprocess.run(command, capture_output=True, text=True).stdout.strip()
86+
hosted_url = f"{HOSTED_SITE_DOMAIN}/{pr_number}"
87+
if hosted_url not in pr_description:
88+
new_pr_description = f"Hosted url: [{hosted_url}](https://{hosted_url})\n" + pr_description
89+
command = ["gh", "pr", "edit", pr_number, "--body", new_pr_description]
90+
subprocess.run(command)
91+
92+
93+
def process_branch_folders():
94+
"""
95+
Clones the branch specific code to hosted/<branch-name> folder.
96+
It then executes the build command and copy the built site to apps folder
97+
under the same branch name
98+
:return: PR numbers in str list where the site data is copied to
99+
"""
100+
delete_folders(["branch", "app"])
101+
102+
command = ["gh", "pr", "list", "--json", "number,headRefName"]
103+
command_run_result = subprocess.run(command, capture_output=True, text=True).stdout.strip()
104+
branches_data = ast.literal_eval(command_run_result)
105+
remote_url = subprocess.run(["git", "remote", "get-url", "origin"],
106+
capture_output=True,
107+
text=True).stdout.strip()
108+
parent_dir = os.getcwd()
109+
clone_data_to_branch_folder("dev", remote_url, parent_dir, "dev")
110+
pr_numbers = []
111+
for branch_data in branches_data:
112+
if not branch_data["headRefName"].startswith("hosted/") or not branch_data.get("number"):
113+
continue
114+
pr_number = str(branch_data["number"])
115+
clone_data_to_branch_folder(branch_data["headRefName"], remote_url, parent_dir, pr_number)
116+
update_pr_description(pr_number)
117+
pr_numbers.append(pr_number)
118+
119+
return pr_numbers
120+
121+
def update_nginx_config(pr_numbers):
122+
"""
123+
Updates nginx.conf file with branches built information to host multiple versions
124+
of software at the same time.
125+
:param pr_numbers: pr numbers a str list of open pr numbers to be hosted
126+
"""
127+
config_file = os.path.join(os.getcwd(), "nginx.conf")
128+
nginx_location_blocks = ""
129+
130+
for pr_number in pr_numbers:
131+
location_block = f"""location /{pr_number} {{
132+
alias /app/{pr_number};
133+
try_files $uri $uri/ /index.html;
134+
error_page 404 /404.html;
135+
}}
136+
"""
137+
nginx_location_blocks += location_block
138+
print(f"Hosted site: https://{HOSTED_SITE_DOMAIN}/{pr_number}")
139+
140+
with open(config_file, "r+") as f:
141+
content = f.read()
142+
content = content.replace("#REPLACE_APPS", nginx_location_blocks)
143+
f.seek(0)
144+
f.write(content)
145+
f.truncate()
146+
147+
print("NGINX configuration updated successfully!")
148+
149+
if __name__ == "__main__":
150+
current_dir = os.getcwd()
151+
open_prs = process_branch_folders()
152+
update_nginx_config(open_prs)

0 commit comments

Comments
 (0)