3232 default : " frontend-dev-ecs-cluster"
3333
3434jobs :
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
0 commit comments