Skip to content

Commit

Permalink
feat :: ecr reusable workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaisqls committed Jul 22, 2023
1 parent deacb60 commit b2813ad
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
57 changes: 57 additions & 0 deletions .github/workflows/check_and_create_ecr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Check and Create ECR Repository

on:
workflow_call:
inputs:
service_name:
description: 'The name of the Service'
required: true
service_Type:
description: 'Type of service'
required: true
environment:
description: 'Environment classification for servers'
required: true

env:
ECR_REPOSITORY_NAME: ${{ inputs.service_name }}-${{ inputs.service_type }}-${{ inputs.environment }}

jobs:
check_and_create_ecr:
runs-on: ubuntu-latest

- name: Check if ECR repository exists
id: ecr_exists
shell: bash
run: |
set +e
aws ecr describe-repositories --repository-names ${{ env.ECR_REPOSITORY_NAME }}
exit_code=$?
set -e
echo "ecr_exists=$($exit_code != 0 ? 'false' : 'true')" >> $GITHUB_OUTPUT
- name: Create ECR repository if not exists
if: steps.ecr_exists.outputs.ecr_exists == 'false'
shell: bash
run: |
aws ecr create-repository --repository-name ${{ env.ECR_REPOSITORY_NAME }}
- name: Repository Dispatch
if: steps.ecr_exists.outputs.ecr_exists == 'false'
uses: peter-evans/repository-dispatch@v1
with:
token: ${{ inputs.github_token }}
repository: team-xquare/xquare-gitops-repo
event-type: add_ecr
client-payload: |
{
"repository_name" : "${{ env.ECR_REPOSITORY_NAME }}",
"service_name" : "${{ inputs.service_name }}",
"service_type" : "${{ inputs.service_type }}",
"environment" : "${{ inputs.environment }}"
}
- name: Return repository name
id: ecr_name
shell: bash
run: echo "repository_name=${{ env.ECR_REPOSITORY_NAME }}" >> $GITHUB_OUTPUT
16 changes: 8 additions & 8 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ branding:
description: 'Tasks to deploy to xquare server'
inputs:
service_type:
description: 'Type of project (ex. be, fe)'
description: 'Type of service (ex. be, fe)'
required: true
environment:
description: 'Environment classification for servers (prod/stag)'
Expand Down Expand Up @@ -57,11 +57,12 @@ runs:
id: ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Create ECR Repository if not exists
shell: bash
id: ecr_exists
run: chmod +x $GITHUB_ACTION_PATH/create_ecr_if_not_exists.sh
$GITHUB_ACTION_PATH/create_ecr_if_not_exists.sh
- name: Create ECR if not exists
uses: ./.github/workflows/check_and_create_ecr.yaml
with:
service_name: ${{ steps.read-config.outputs.service_name }}
service_type: ${{ inputs.service_type }}
environment: ${{ inputs.environment }}

- name: Get the branch tag if exist
id: tag
Expand All @@ -84,8 +85,7 @@ runs:
id: docker
shell: bash
run: |
repo="${{ steps.ecr.outputs.registry }}/${{ steps.read-config.outputs.service_name }}-${{ inputs.service_type }}-${{ inputs.environment }}:${{ steps.tag.outputs.version_tag }}"
repo="${{ steps.ecr.outputs.registry }}/${{ steps.ecr_name.output.repository_name }}:${{ steps.tag.outputs.version_tag }}"
docker build -t $repo \
--build-arg CLOUD_CONFIG_USERNAME=${{ env.XQUARE_CLOUD_CONFIG_USERNAME }} \
--build-arg CLOUD_CONFIG_PASSWORD=${{ env.XQUARE_CLOUD_CONFIG_PASSWORD }} \
Expand Down
10 changes: 0 additions & 10 deletions create_ecr_if_not_exists.sh

This file was deleted.

0 comments on commit b2813ad

Please sign in to comment.