Skip to content

Commit

Permalink
Test for github branch information
Browse files Browse the repository at this point in the history
  • Loading branch information
sshrihar committed Sep 20, 2024
1 parent e182c32 commit 409ac0c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 43 deletions.
63 changes: 37 additions & 26 deletions .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ jobs:
with:
fetch-depth: 0

- 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)) \
Expand All @@ -35,36 +42,40 @@ jobs:
&& sudo apt update \
&& sudo apt install gh -y
- name: Authenticate GitHub CLI
run: gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
# (type -p wget >/dev/null || ( apt update && apt-get install wget -y)) \
# && mkdir -p -m 755 /etc/apt/keyrings \
# && wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
# && 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" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
# && apt update \
# && apt install gh -y

- name: List open PRs with branch names
run: |
gh pr list --json number,headRefName,baseRefName,title --jq '.[] | {PR: .number, Branch: .headRefName, BaseBranch: .baseRefName, Title: .title}'
# - name: Authenticate GitHub CLI
# run: gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"

# - uses: actions/setup-python@v4
# with:
# python-version: '3.11'
# - name: List open PRs with branch names
# run: |
# gh pr list --json number,headRefName,title --jq '.[] | {PR: .number, Branch: .headRefName, Title: .title}'
# branches=$(gh pr list --json number,headRefName --jq '.[] | select(.headRefName | startswith("hosted/")) | {PR: .number, Branch: .headRefName}')
# echo "Branches: $branches"


# - name: Install pipenv
# run: pip install pipenv

# - name: Build Site
# run: |
# python build_branches.py '*kmurphy*'
- name: Build Site
run: |
python build_branches.py ${{ secrets.GITHUB_TOKEN }}
# - 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
- 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
Expand Down
42 changes: 25 additions & 17 deletions build_branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import shutil
import sys


def install_mkdocs_with_pipenv():
"""
Builds a particular branch site.
Expand Down Expand Up @@ -45,25 +46,33 @@ def delete_folders(folder_paths):
except OSError as e:
print(f"Error deleting folder: {e}")

def process_branch_folders(branch_pattern):
def process_branch_folders(secret_token):
"""
Clones the branch specific code to branch/<branch-name> folder.
Clones the branch specific code to hosted/<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
:param secret_token: Github secret token to connect to github repo and get branch details
:return: All branch names identified using the pattern hosted/ with PR open
"""
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():
command = f"gh auth login --with-token {secret_token}".split()
subprocess.run(command, capture_output=False)
command = ["gh", "pr", "list", "--json", "number,headRefName", "--jq", '.[] | select(.headRefName | startswith("hosted/*")) | {PR: .number, Branch: .headRefName}'],
branches_details = subprocess.run(command, capture_output=True, text=True).stdout.strip().splitlines()
print(branches_details)
sys.exit(0)


# 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"

# command = """gh pr list --json number,headRefName --jq '.[] | select(.headRefName | startswith("hosted/")) | {PR: .number, Branch: .headRefName}'"""

for branch in subprocess.run(command.split(), 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)
Expand Down Expand Up @@ -115,8 +124,7 @@ def update_nginx_config(branches):
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)
secret_token = sys.argv[1]
current_dir = os.getcwd()
branches = process_branch_folders(pattern)
branches = process_branch_folders(secret_token)
update_nginx_config(branches)

0 comments on commit 409ac0c

Please sign in to comment.