Skip to content

Commit

Permalink
Test deployment to dev environment with review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sshrihar committed Sep 18, 2024
1 parent 91118c9 commit 97cfc71
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/assets/dev-taskdef.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
region: eu-west-1
hostport: 80
containerport: 80
role: frontend-application
environment: dev
iac: terraform-workspace-aws-dev-apps-eu-west-1-apps-docs-dev-polygon-technology
team_name: documentation
docker_file: Dockerfile.review
docker_build_params: TARGET_BRANCH_REGEX=*dev*
memory: 1024
cpu: 512
11 changes: 11 additions & 0 deletions .github/assets/staging-taskdef.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
region: eu-west-1
hostport: 80
containerport: 80
role: frontend-application
environment: staging
iac: terraform-workspace-aws-test-applications-eu-west-1-apps-docs-staging-polygon-technology
team_name: documentation
docker_file: Dockerfile.review
docker_build_params: TARGET_BRANCH_REGEX=*staging*
memory: 1024
cpu: 512
62 changes: 62 additions & 0 deletions .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
on:
workflow_call:
inputs:
environment:
required: false
type: string
default: "dev"
core_app:
required: false
type: string
description: "Core app name"
default: "core_app_name"

jobs:
set-env-variable:
runs-on: ubuntu-latest
outputs:
ACCOUNT_NUMBER: ${{ steps.set-env-var.outputs.ACCOUNT_NUMBER }}
APP_NAME: ${{ steps.set-env-var.outputs.APP_NAME }}
CLUSTER_NAME: ${{ steps.set-env-var.outputs.CLUSTER_NAME }}
DOCKER_BUILD_PARAMS: ${{ steps.set-env-var.outputs.DOCKER_BUILD_PARAMS }}
steps:
- name: Checkout Code Repository
uses: actions/checkout@v3

- uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install pipenv
run: pip install pipenv

- name: Build Site
run: |
# python build_branches.py '*${{ inputs.environment }}*'
python build_branches.py '*kmurphy*'

- name: Set Environment Variable
id: set-env-var
run: |
if [ "${{ inputs.environment }}" == "dev" ]; then
echo "ACCOUNT_NUMBER=058264511034" >> $GITHUB_OUTPUT
echo "APP_NAME=${{ inputs.core_app }}-dev" >> $GITHUB_OUTPUT
echo "CLUSTER_NAME=frontend-dev-ecs-cluster" >> $GITHUB_OUTPUT
elif [ "${{ inputs.environment }}" == "staging" ]; then
echo "ACCOUNT_NUMBER=070528468658" >> $GITHUB_OUTPUT
echo "APP_NAME=${{ inputs.core_app }}-staging" >> $GITHUB_OUTPUT
echo "CLUSTER_NAME=frontend-staging-ecs-cluster" >> $GITHUB_OUTPUT
fi
deploy:
uses: 0xPolygon/pipelines/.github/workflows/ecs_deploy_docker_taskdef.yaml@main
needs: set-env-variable
with:
app_name: ${{ needs.set-env-variable.outputs.APP_NAME }}
taskdef_file_vars: .github/assets/${{ inputs.environment }}-taskdef.yaml
aws_region: eu-west-1
environment: ${{ inputs.environment }}
cluster_name: ${{ needs.set-env-variable.outputs.CLUSTER_NAME }}
account_number: "${{ needs.set-env-variable.outputs.ACCOUNT_NUMBER }}"
docker_file: Dockerfile.review
secrets: inherit
14 changes: 14 additions & 0 deletions .github/workflows/dev-deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Docs Dev Deployment
on:
push:
branches:
- SPEC-170-dev-deployment-update-for-review
workflow_dispatch:

jobs:
deploy:
uses: ./.github/workflows/build_and_deploy.yml
with:
environment: "dev"
core_app: "docs"
secrets: inherit
14 changes: 14 additions & 0 deletions .github/workflows/staging-deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Docs Dev Deployment
on:
push:
branches:
- staging*
workflow_dispatch:

jobs:
deploy:
uses: ./.github/workflows/build_and_deploy.yml
with:
environment: "staging"
core_app: "docs"
secrets: inherit
8 changes: 8 additions & 0 deletions Dockerfile.review
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM nginx:alpine

COPY nginx.conf /etc/nginx/nginx.conf
COPY app /app

WORKDIR /app
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
122 changes: 122 additions & 0 deletions build_branches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import subprocess
import os
import shutil
import sys

def install_mkdocs_with_pipenv():
"""
Builds a particular branch site.
Having a varying set of requirements can be handled by having each branch
build their dependencies and then running mkdocs build.
"""
folder = os.getcwd()
subprocess.run(["pipenv", "install", "--site-packages"], cwd=folder)
subprocess.run(["pipenv", "install", "-r", "requirements.txt"], cwd=folder)
subprocess.run(["pipenv", "run", "mkdocs", "build"], cwd=folder)

def copy_folder(source_dir, target_dir):
"""
Copies contents from source directory to target directory
:param source_dir: Source directory from which contents are to be copied
:param target_dir: Target Directory where the contents are copied to.
"""
os.makedirs(target_dir, exist_ok=True)

for item in os.listdir(source_dir):
source_path = os.path.join(source_dir, item)
target_path = os.path.join(target_dir, item)

if os.path.isdir(source_path):
shutil.copytree(source_path, target_path, dirs_exist_ok=True)
else:
if os.path.exists(target_path):
os.remove(target_path)
shutil.copy2(source_path, target_path)

def delete_folders(folder_paths):
"""
Cleans existing folders for app and branches before executing the builds
:param folder_paths: List of folders to be deleted under the current working directory
"""
for folder_path in folder_paths:
try:
shutil.rmtree(folder_path)
print(f"Folder {folder_path} deletion successful.")
except OSError as e:
print(f"Error deleting folder: {e}")

def process_branch_folders(branch_pattern):
"""
Clones the branch specific code to branch/<branch-name> folder.
It then executes the build command and copy the built site to apps folder
under the same branch name
:param branch_pattern: Pattern to identify remote branch to pull and build.
:return: All branch names identified using the pattern
"""
delete_folders(["branch", "app"])
remote_url = subprocess.run(["git", "remote", "get-url", "origin"],
capture_output=True,
text=True).stdout.strip()
all_branches = []
parent_dir = os.getcwd()
common_dir = "branch"

for branch in subprocess.run(["git", "branch", "-r", "--list", branch_pattern, "|", "grep", "-v", "'\->'"],
capture_output=True,
text=True).stdout.splitlines():
branch_name = branch.replace("origin/", "").strip().split(" ", 1)[0]
all_branches.append(branch_name)
target_path = os.path.join(common_dir, branch_name)
os.makedirs(target_path, exist_ok=True)
os.chdir(target_path)
subprocess.run(["git", "init"])
subprocess.run(["git", "remote", "add", "origin", remote_url])
print(f"Checking out branch {branch_name}")
subprocess.run(["git", "fetch", "--depth", "1", "origin", branch_name])
subprocess.run([
"git", "checkout", "-b", branch_name, "--track",
f"origin/{branch_name}"
])
install_mkdocs_with_pipenv()
source_dir = os.path.join(os.getcwd(), "site")
copy_folder(source_dir, os.path.join(parent_dir, "app", branch_name))
os.chdir(parent_dir)

return all_branches

def update_nginx_config(branches):
"""
Updates nginx.conf file with branches built information to host multiple versions
of software at the same time.
:param branches: Branches built for hosting.
"""
config_file = os.path.join(os.getcwd(), "nginx.conf")
nginx_location_blocks = ""

for folder in branches:
location_block = f"""location /{folder} {{
alias /app/{folder};
try_files $uri $uri/ /index.html;
error_page 404 /404.html;
}}
"""
nginx_location_blocks += location_block

first_folder = branches[0]

with open(config_file, "r+") as f:
content = f.read()
content = content.replace("REPLACE_APPS", nginx_location_blocks)
content = content.replace("REPLACE_FIRST_APP", first_folder)
f.seek(0)
f.write(content)
f.truncate()

print("NGINX configuration updated successfully!")

if __name__ == "__main__":
pattern = sys.argv[1] # Argument passed from command line to identify branch pattern
print("Branch pattern:", pattern)
current_dir = os.getcwd()
branches = process_branch_folders(pattern)
update_nginx_config(branches)
28 changes: 28 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
worker_processes auto;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

sendfile on;
keepalive_timeout 65;

server {
listen 80;

REPLACE_APPS

location = / {
return 301 /REPLACE_FIRST_APP;
}

# Catch-all location block for anything else
location / {
try_files $uri $uri/ =404;
}
}
}

0 comments on commit 97cfc71

Please sign in to comment.