Skip to content

Commit

Permalink
Merge branch 'master' into feature/JASPER-157
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronaldo Macapobre committed Nov 19, 2024
2 parents 7be233a + 4c637b9 commit a4834e3
Show file tree
Hide file tree
Showing 99 changed files with 7,576 additions and 734 deletions.
13 changes: 11 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@
"ms-dotnettools.csdevkit",
"ms-dotnettools.csharp",
"ms-dotnettools.vscode-dotnet-runtime",
"amazonwebservices.aws-toolkit-vscode"
"amazonwebservices.aws-toolkit-vscode",
"hashicorp.terraform"
],
"settings": {
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
},
"editor.formatOnSave": true
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[csharp]": {
"editor.defaultFormatter": "ms-dotnettools.csharp"
},
"[terraform]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "hashicorp.terraform"
}
}
}
},
Expand Down
36 changes: 36 additions & 0 deletions .github/workflows/actions/build-lambdas/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Build Lambda
description: Builds all Lambda functions

inputs:
working_directory:
description: The working directory where the code will be built.
required: true
node_version:
description: The node version that will be used.
required: true

runs:
using: composite

steps:
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.node_version }}

- run: npm ci
shell: bash
working-directory: ${{ inputs.working_directory }}

- run: npm run lint
shell: bash
working-directory: ${{ inputs.working_directory }}
continue-on-error: false

- run: npm run build
shell: bash
working-directory: ${{ inputs.working_directory }}

- run: npm run test --if-present
shell: bash
working-directory: ${{ inputs.working_directory }}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Deploy to AWS
description: Deploy image to AWS Instance
name: Deploy App
description: Deploy web or API image to AWS ECS

inputs:
environment:
Expand Down Expand Up @@ -41,11 +41,11 @@ runs:
id: vars
shell: bash
run: |
echo "task_definition_name=${{ inputs.app_name }}-${{ inputs.tier_name }}-task-definition-${{ inputs.environment }}" >> $GITHUB_OUTPUT
echo "task_definition_name=${{ inputs.app_name }}-${{ inputs.tier_name }}-td-${{ inputs.environment }}" >> $GITHUB_OUTPUT
echo "container_name=${{ inputs.app_name }}-${{ inputs.tier_name }}-container-${{ inputs.environment }}" >> $GITHUB_OUTPUT
echo "ecs_cluster_name=${{ inputs.app_name }}-ecs-cluster-${{ inputs.environment }}" >> $GITHUB_OUTPUT
echo "ecs_service_name=${{ inputs.app_name }}-ecs-${{ inputs.tier_name }}-service-${{ inputs.environment }}" >> $GITHUB_OUTPUT
echo "full_ecr_repo_url=${{ inputs.aws_account }}.dkr.ecr.${{ inputs.region }}.amazonaws.com/${{ inputs.app_name }}-ecr-repo-${{ inputs.environment }}" >> $GITHUB_OUTPUT
echo "ecs_cluster_name=${{ inputs.app_name }}-app-cluster-${{ inputs.environment }}" >> $GITHUB_OUTPUT
echo "ecs_service_name=${{ inputs.app_name }}-${{ inputs.tier_name }}-ecs-service-${{ inputs.environment }}" >> $GITHUB_OUTPUT
echo "full_ecr_repo_url=${{ inputs.aws_account }}.dkr.ecr.${{ inputs.region }}.amazonaws.com/${{ inputs.app_name }}-app-repo-${{ inputs.environment }}" >> $GITHUB_OUTPUT
- name: Log in to the GHCR
uses: docker/login-action@v2
Expand All @@ -66,7 +66,25 @@ runs:
- name: Login to Amazon ECR
uses: aws-actions/amazon-ecr-login@v2

- name: Pull Docker image from GHCR
- name: Check ECR Image exists
id: ecr-check
shell: bash
run: |
IMAGE_TAG=${{ inputs.image_name }}-${{ inputs.short_sha}}
REPOSITORY_NAME=${{ inputs.app_name }}-app-repo-${{ inputs.environment }}
IMAGE_EXISTS=$(aws ecr describe-images --repository-name $REPOSITORY_NAME --query "imageDetails[?contains(imageTags, '$IMAGE_TAG')]" --output text)
if [ -z "$IMAGE_EXISTS" ]; then
echo "Image with tag $IMAGE_TAG does not exist."
echo "exists=false" >> $GITHUB_OUTPUT
else
echo "Image with tag $IMAGE_TAG already exists."
echo "exists=true" >> $GITHUB_OUTPUT
fi
- name: Push if Docker image does not exist
if: steps.ecr-check.outputs.exists == 'false'
shell: bash
run: |
docker pull ${{ inputs.github_image_repo }}/${{ inputs.image_name }}:${{ inputs.short_sha}}
Expand Down
97 changes: 97 additions & 0 deletions .github/workflows/actions/deploy-lambda/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: Deploy Lambda
description: Deploy image to a Lambda function to AWS

inputs:
environment:
description: The environment to which the image will be deployed.
required: true
aws_account:
description: The AWS Account ID.
required: true
region:
description: The AWS Region of the AWS Account.
required: true
app_name:
description: The application name.
required: true
lambda_name:
description: The lambda function name name.
required: true
aws_role_arn:
description: The AWS Role ARN to assume.
required: true
ghcr_token:
description: The token to use to login to the GHCR.
required: true
github_image_repo:
description: The GCHR repo where images are stored.
required: true
image_name:
description: The name of the image to be deployed.
required: true
short_sha:
description: The short SHA used to tag image in GCHR.
required: true

runs:
using: composite

steps:
- name: Set reusable variables
id: vars
shell: bash
run: |
echo "full_ecr_repo_url=${{ inputs.aws_account }}.dkr.ecr.${{ inputs.region }}.amazonaws.com/${{ inputs.app_name }}-lambda-repo-${{ inputs.environment }}" >> $GITHUB_OUTPUT
echo "container_name=${{ inputs.app_name }}-${{ inputs.tier_name }}-container-${{ inputs.environment }}" >> $GITHUB_OUTPUT
- name: Log in to the GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ inputs.ghcr_token }}

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-skip-session-tagging: true
aws-region: ${{ inputs.region }}
role-to-assume: ${{ inputs.aws_role_arn }}
role-duration-seconds: 1800
role-session-name: ci-deployment

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

- name: Check ECR Image exists
id: ecr-check
shell: bash
run: |
IMAGE_TAG=${{ inputs.image_name }}-${{ inputs.short_sha }}
REPOSITORY_NAME=${{ inputs.app_name }}-lambda-repo-${{ inputs.environment }}
IMAGE_EXISTS=$(aws ecr describe-images --repository-name $REPOSITORY_NAME --query "imageDetails[?contains(imageTags, '$IMAGE_TAG')]" --output text)
if [ -z "$IMAGE_EXISTS" ]; then
echo "Image with tag $IMAGE_TAG does not exist."
echo "exists=false" >> $GITHUB_OUTPUT
else
echo "Image with tag $IMAGE_TAG already exists."
echo "exists=true" >> $GITHUB_OUTPUT
fi
- name: Push if Docker image does not exist
if: steps.ecr-check.outputs.exists == 'false'
shell: bash
run: |
docker pull ${{ inputs.github_image_repo }}/${{ inputs.image_name }}:${{ inputs.short_sha}}
docker tag ${{ inputs.github_image_repo }}/${{ inputs.image_name }}:${{ inputs.short_sha}} ${{ steps.vars.outputs.full_ecr_repo_url }}:${{ inputs.image_name }}-${{ inputs.short_sha }}
docker push ${{ steps.vars.outputs.full_ecr_repo_url }}:${{ inputs.image_name }}-${{ inputs.short_sha }}
- name: Update Lambda Function
shell: bash
run: |
aws lambda update-function-code \
--function-name ${{ inputs.app_name }}-${{ inputs.lambda_name }}-lambda-${{ inputs.environment }} \
--image-uri ${{ steps.vars.outputs.full_ecr_repo_url }}:${{ inputs.image_name }}-${{ inputs.short_sha }}
6 changes: 1 addition & 5 deletions .github/workflows/aws-template-terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ on:
CHANGE_FOLDER_NAME:
required: true
type: string
TEST_BUCKET_NAME:
required: true
type: string
APPLY_TF_CODE:
required: true
default: false
Expand Down Expand Up @@ -88,6 +85,7 @@ jobs:
TF_VAR_environment: ${{ vars.ENVIRONMENT_NAME }}
TF_VAR_kms_key_name: ${{ vars.KMS_KEY_NAME }}
TF_VAR_vpc_id: ${{ vars.VPC_ID }}
TF_VAR_lambda_memory_size: ${{ vars.LAMBDA_MEMORY_SIZE }}
needs: [check_changes, scan]
steps:
- name: Checkout repository
Expand Down Expand Up @@ -117,7 +115,6 @@ jobs:
id: plan
env:
CONTEXT_FOLDER: ${{ inputs.CONTEXT_FOLDER }}
#TF_VAR_test_s3_bucket_name: ${{ inputs.TEST_BUCKET_NAME }}
run: |
terraform plan -no-color -input=false -var-file=${{ inputs.ENVIRONMENT_NAME }}.tfvars
continue-on-error: true
Expand All @@ -129,7 +126,6 @@ jobs:
if: inputs.APPLY_TF_CODE == true
env:
CONTEXT_FOLDER: ${{ inputs.CONTEXT_FOLDER }}
#TF_VAR_test_s3_bucket_name: ${{ inputs.TEST_BUCKET_NAME }}
run: |
terraform apply --auto-approve -input=false -var-file=${{ inputs.ENVIRONMENT_NAME }}.tfvars
working-directory: ${{ inputs.CONTEXT_FOLDER }}
28 changes: 28 additions & 0 deletions .github/workflows/build-and-test-lambdas.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Build and Test Lambdas

on:
pull_request:
branches:
- master
paths:
- "aws/**"

workflow_dispatch:

env:
WORKING_DIRECTORY: ./aws
NODE_VERSION: 20

jobs:
build-and-test:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Building Lambdas codebase
uses: ./.github/workflows/actions/build-lambdas
with:
working_directory: ${{ env.WORKING_DIRECTORY }}
node_version: ${{ env.NODE_VERSION }}
12 changes: 11 additions & 1 deletion .github/workflows/build-infra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@ on:
- prod

jobs:
build-initial:
uses: ./.github/workflows/aws-template-terraform.yml
with:
CONTEXT_FOLDER: "./infrastructure/cloud/environments/initial"
CHANGE_FOLDER_NAME: environments/${{ inputs.environment || 'dev' }}
ENVIRONMENT_NAME: ${{ inputs.environment || 'dev' }}
APPLY_TF_CODE: false
secrets: inherit

build:
needs: [build-initial]
uses: ./.github/workflows/aws-template-terraform.yml
with:
CONTEXT_FOLDER: "./infrastructure/cloud/environments/${{ inputs.environment || 'dev' }}"
CHANGE_FOLDER_NAME: environments/${{ inputs.environment || 'dev' }}
ENVIRONMENT_NAME: ${{ inputs.environment || 'dev' }}
TEST_BUCKET_NAME: jasper-test-bucket
APPLY_TF_CODE: false
secrets: inherit

6 changes: 3 additions & 3 deletions .github/workflows/publish-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
uses: actions/checkout@v4

- name: Deploy to ${{ env.ENVIRONMENT }}
uses: ./.github/workflows/actions/deploy-to-aws
uses: ./.github/workflows/actions/deploy-app
with:
environment: ${{ env.ENVIRONMENT }}
aws_account: ${{ vars.AWS_ACCOUNT }}
Expand Down Expand Up @@ -120,7 +120,7 @@ jobs:

# Uncomment when infra in AWS in TEST environment has been configured
# - name: Deploy to ${{ env.ENVIRONMENT }}
# uses: ./.github/workflows/actions/deploy-to-aws
# uses: ./.github/workflows/actions/deploy-app
# with:
# environment: ${{ env.ENVIRONMENT }}
# aws_account: ${{ vars.AWS_ACCOUNT }}
Expand Down Expand Up @@ -150,7 +150,7 @@ jobs:

# Uncomment when infra in AWS in PROD environment has been configured
# - name: Deploy to ${{ env.ENVIRONMENT }}
# uses: ./.github/workflows/actions/deploy-to-aws
# uses: ./.github/workflows/actions/deploy-app
# with:
# environment: ${{ env.ENVIRONMENT }}
# aws_account: ${{ vars.AWS_ACCOUNT }}
Expand Down
Loading

0 comments on commit a4834e3

Please sign in to comment.