Skip to content

Commit 7c78c53

Browse files
authored
Merge pull request 0xPolygon#738 from 0xPolygon/hosted/deployment-update
Hosted/deployment update
2 parents 5168ccc + ceaefc2 commit 7c78c53

File tree

4 files changed

+78
-138
lines changed

4 files changed

+78
-138
lines changed

.github/workflows/build_and_deploy.yml

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,69 +32,97 @@ on:
3232
default: "frontend-dev-ecs-cluster"
3333

3434
jobs:
35-
build_site_data:
36-
name: ${{ inputs.environment }} deployment
35+
check_files_changed:
3736
runs-on: ubuntu-latest
3837
environment: ${{ inputs.environment }}
3938
permissions:
4039
id-token: write
4140
contents: write
42-
env:
43-
AWS_REGION: ${{ inputs.region }}
44-
ECR_REPOSITORY: ${{ inputs.core_app }}-${{ inputs.environment }}-ecr
45-
ECS_SERVICE: ${{ inputs.core_app }}-${{ inputs.environment }}-ecs-service
46-
ECS_CLUSTER: frontend-${{ inputs.environment }}-ecs-cluster
47-
ECS_TASK_DEFINITION: ${{ inputs.task_definition }}
48-
APP_NAME: ${{ inputs.core_app }}-${{ inputs.environment }}
41+
outputs:
42+
FILES_CHANGED: ${{ steps.changes.outputs.FILES_CHANGED }}
4943
steps:
44+
- name: Install GitHub CLI
45+
run: |
46+
(type -p wget >/dev/null || (sudo apt update && sudo apt-get install wget -y)) \
47+
&& sudo mkdir -p -m 755 /etc/apt/keyrings \
48+
&& wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
49+
&& sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
50+
&& 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 \
51+
&& sudo apt update \
52+
&& sudo apt install gh -y
53+
54+
- name: Authenticate GitHub CLI
55+
run: gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
56+
5057
- name: Checkout Code Repository
5158
uses: actions/checkout@v3
5259
with:
53-
fetch-depth: 0
60+
fetch-depth: 2
5461

5562
- name: Get list of changed files
5663
id: changes
5764
run: |
5865
changed_files=$(git diff --name-only HEAD^ HEAD)
59-
echo "Changed files: $changed_files"
60-
echo "FILES_CHANGED=$changed_files" >> "$GITHUB_OUTPUT"
66+
num_changed_files=$(echo "$changed_files" | wc -l)
67+
echo "FILES_CHANGED=$num_changed_files" >> "$GITHUB_OUTPUT"
6168
62-
- name: Stop if no files changed
63-
if: ${{ steps.changes.outputs.FILES_CHANGED == '' }}
69+
check_pr_open:
70+
runs-on: ubuntu-latest
71+
environment: ${{ inputs.environment }}
72+
needs: [check_files_changed]
73+
if: needs.check_files_changed.outputs.FILES_CHANGED != '' && startsWith(github.ref, 'refs/heads/hosted/')
74+
permissions:
75+
id-token: write
76+
contents: write
77+
outputs:
78+
PR_OPEN: ${{ steps.check_pr.outputs.PR_OPEN }}
79+
steps:
80+
- name: Check for open PR
81+
id: check_pr
6482
run: |
65-
echo "No files were changed, skipping build."
66-
exit 0
67-
68-
- name: Configure AWS credentials
69-
uses: aws-actions/configure-aws-credentials@v4
70-
with:
71-
aws-region: ${{ env.AWS_REGION }}
72-
role-to-assume: arn:aws:iam::${{ inputs.account_number }}:role/${{ env.APP_NAME }}-GithubActionsRole
73-
role-session-name: GithubActionsSession
83+
BRANCH_NAME=$(echo "${GITHUB_REF}" | cut -d'/' -f4)
84+
pr_number=$(gh pr list --head "hosted/$BRANCH_NAME" --state open --json number --jq '.[0].number')
85+
echo "PR_OPEN=$pr_number" >> "$GITHUB_OUTPUT"
86+
env:
87+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7488

89+
build_and_deploy:
90+
runs-on: ubuntu-latest
91+
environment: ${{ inputs.environment }}
92+
needs: [check_files_changed, check_pr_open]
93+
if: |
94+
needs.check_files_changed.outputs.FILES_CHANGED != '' && github.ref == 'refs/heads/dev' ||
95+
needs.check_pr_open.outputs.PR_OPEN != '' && needs.check_files_changed.outputs.FILES_CHANGED != '' &&
96+
startsWith(github.ref, 'refs/heads/hosted/')
97+
permissions:
98+
id-token: write
99+
contents: write
100+
steps:
75101
- uses: actions/setup-python@v4
76102
with:
77103
python-version: '3.11'
78104

79105
- name: Install pipenv
80106
run: pip install pipenv
81107

82-
- name: Install GitHub CLI
83-
run: |
84-
(type -p wget >/dev/null || (sudo apt update && sudo apt-get install wget -y)) \
85-
&& sudo mkdir -p -m 755 /etc/apt/keyrings \
86-
&& wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
87-
&& sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
88-
&& 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 \
89-
&& sudo apt update \
90-
&& sudo apt install gh -y
108+
- name: Checkout Code Repository
109+
uses: actions/checkout@v3
110+
with:
111+
fetch-depth: 0
91112

92113
- name: Authenticate GitHub CLI
93114
run: gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
94115

95116
- name: Build Site
96117
run: |
97-
python build_branches.py
118+
python build_branches.py
119+
120+
- name: Configure AWS credentials
121+
uses: aws-actions/configure-aws-credentials@v4
122+
with:
123+
aws-region: ${{ inputs.region }}
124+
role-to-assume: arn:aws:iam::${{ inputs.account_number }}:role/${{ inputs.core_app }}-${{ inputs.environment }}-GithubActionsRole
125+
role-session-name: GithubActionsSession
98126

99127
- name: Login to Amazon ECR
100128
id: login-ecr
@@ -105,7 +133,7 @@ jobs:
105133
env:
106134
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
107135
IMAGE_TAG: ${{ github.sha }}-${{ github.run_number }}
108-
ECR_REPOSITORY: ${{ env.APP_NAME }}-ecr
136+
ECR_REPOSITORY: ${{ inputs.core_app }}-${{ inputs.environment }}-ecr
109137
run: |
110138
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f Dockerfile.review .
111139
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
@@ -115,21 +143,21 @@ jobs:
115143
id: task-def
116144
uses: aws-actions/amazon-ecs-render-task-definition@v1
117145
with:
118-
task-definition: ${{ env.ECS_TASK_DEFINITION }}
119-
container-name: ${{ env.APP_NAME }}
146+
task-definition: ${{ inputs.task_definition }}
147+
container-name: ${{ inputs.core_app }}-${{ inputs.environment }}
120148
image: ${{ steps.build-image.outputs.image }}
121149

122150
- name: Deploy Amazon ECS task definition
123151
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
124152
with:
125153
task-definition: ${{ steps.task-def.outputs.task-definition }}
126-
service: ${{ env.ECS_SERVICE }}
127-
cluster: ${{ env.ECS_CLUSTER }}
154+
service: ${{ inputs.core_app }}-${{ inputs.environment }}-ecs-service
155+
cluster: frontend-${{ inputs.environment }}-ecs-cluster
128156
wait-for-service-stability: true
129157

130158
- name: Cloudflare Cache Purge
131159
uses: nathanvaughn/actions-cloudflare-purge@master
132160
with:
133161
cf_zone: ${{ secrets.CLOUDFLARE_ZONE }}
134162
cf_auth: ${{ secrets.CLOUDFLARE_AUTH_KEY }}
135-
hosts: ${{ env.APP_NAME }}.polygon.technology
163+
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
name: hosted branch pr deployment
1+
name: Hosted PR and Dev Deployment
22
on:
33
pull_request:
44
types: [opened, edited, reopened]
55
push:
66
branches:
77
- dev
8+
- hosted/**
89
workflow_dispatch:
910

1011
jobs:
1112
deploy:
12-
if: startsWith(github.head_ref, 'hosted/') || github.ref == 'refs/heads/dev'
1313
uses: ./.github/workflows/build_and_deploy.yml
14-
secrets: inherit
14+
secrets: inherit

build_branches.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import subprocess
55

66

7+
HOSTED_SITE_DOMAIN = "docs-dev.polygon.technology"
8+
9+
710
def install_mkdocs_with_pipenv():
811
"""
912
Builds a particular branch site.
@@ -72,15 +75,15 @@ def clone_data_to_branch_folder(branch_name, remote_url, parent_dir, pr_number=N
7275
os.chdir(parent_dir)
7376

7477

75-
def update_pr_description(pr_number):
78+
def update_pr_description(pr_number:str):
7679
"""
7780
Updates PR description by adding the url to access the hosted environment under dev
7881
if it does not already exist in the definition
7982
:param pr_number: PR number for the branch hosting website
8083
"""
81-
command = ["gh", "pr", "view", pr_number, "--json", "body", "--jq", "'.body'"]
84+
command = ["gh", "pr", "view", pr_number, "--json", "body", "--jq", ".body"]
8285
pr_description = subprocess.run(command, capture_output=True, text=True).stdout.strip()
83-
hosted_url = f"docs-dev.polygon.technology/{pr_number}"
86+
hosted_url = f"{HOSTED_SITE_DOMAIN}/{pr_number}"
8487
if hosted_url not in pr_description:
8588
new_pr_description = f"Hosted url: [{hosted_url}](https://{hosted_url})\n" + pr_description
8689
command = ["gh", "pr", "edit", pr_number, "--body", new_pr_description]
@@ -106,7 +109,7 @@ def process_branch_folders():
106109
clone_data_to_branch_folder("dev", remote_url, parent_dir, "dev")
107110
pr_numbers = []
108111
for branch_data in branches_data:
109-
if not branch_data["headRefName"].startswith("hosted/"):
112+
if not branch_data["headRefName"].startswith("hosted/") or not branch_data.get("number"):
110113
continue
111114
pr_number = str(branch_data["number"])
112115
clone_data_to_branch_folder(branch_data["headRefName"], remote_url, parent_dir, pr_number)
@@ -132,6 +135,7 @@ def update_nginx_config(pr_numbers):
132135
}}
133136
"""
134137
nginx_location_blocks += location_block
138+
print(f"Hosted site: https://{HOSTED_SITE_DOMAIN}/{pr_number}")
135139

136140
with open(config_file, "r+") as f:
137141
content = f.read()

0 commit comments

Comments
 (0)