Skip to content

Commit

Permalink
Reusable workflows added
Browse files Browse the repository at this point in the history
  • Loading branch information
sshrihar committed Jan 17, 2024
1 parent bb2e22a commit 4cf3fe4
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
76 changes: 76 additions & 0 deletions .github/workflows/reusable/docker_build_deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
on:
workflow_call:
inputs:
app_name:
required: true
type: string
taskdef_file:
required: true
type: string
account_number:
required: true
type: string
docker_file:
required: true
type: string
cluster_name:
required: true
type: string
aws_region:
required: false
default: "eu-west-1"
type: string
environment:
required: false
default: "staging"
type: string

jobs:
deploy_workflow:
name: Deploy ${{ inputs.app_name }}
permissions:
id-token: write
contents: write
environment: ${{ inputs.environment }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: ${{ inputs.aws_region }}
role-to-assume: arn:aws:iam::${{ inputs.account_number }}:role/${{ inputs.app_name }}-GithubActionsRole
role-session-name: GithubActionsSession

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
ECR_REPOSITORY: ${{ inputs.app_name }}-ecr
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f ${{ inputs.docker_file }} .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ inputs.taskdef_file }}
container-name: ${{ inputs.app_name }}
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ inputs.app_name }}-ecs-service
cluster: ${{ inputs.cluster_name }}
wait-for-service-stability: true
87 changes: 87 additions & 0 deletions .github/workflows/reusable/npm_build_deploy_default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
on:
workflow_call:
inputs:
app_name:
required: true
type: string
taskdef_file:
required: true
type: string
account_number:
required: true
type: string
docker_file:
required: true
type: string
cluster_name:
required: true
type: string
aws_region:
required: false
default: "eu-west-1"
type: string
environment:
required: false
default: "staging"
type: string

jobs:
deploy_workflow:
name: Deploy ${{ inputs.app_name }}
permissions:
id-token: write
contents: write
environment: ${{ inputs.environment }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: ${{ inputs.aws_region }}
role-to-assume: arn:aws:iam::${{ inputs.account_number }}:role/${{ inputs.app_name }}-GithubActionsRole
role-session-name: GithubActionsSession

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Setup node
uses: actions/setup-node@v2
with:
node-version: 18.16.1

- name: INSTALL
run: npm install

- name: Build
run: npm run build

- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
ECR_REPOSITORY: ${{ inputs.app_name }}-ecr
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ inputs.taskdef_file }}
container-name: ${{ inputs.app_name }}
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ inputs.app_name }}-ecs-service
cluster: ${{ inputs.cluster_name }}
wait-for-service-stability: true

0 comments on commit 4cf3fe4

Please sign in to comment.