Skip to content

Commit

Permalink
Refactor build and push image to Amazon ECR
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewsrc committed Feb 23, 2024
1 parent 20dcd64 commit 0e917d9
Showing 1 changed file with 5 additions and 61 deletions.
66 changes: 5 additions & 61 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,66 +44,14 @@ jobs:
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}

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

- name: Build, tag, and push image to Amazon ECR (Lambda Functions)
id: build-image-lambda
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
id: build-image
run: |
# Build a docker container and
# push it to ECR so that it can
# be deployed to Lambda Functions.
AWS_ECR_REPOSITORY_NAME=lambda-repo # Replace with your ECR repository name
AWS_ECR_REPOSITORY_URL=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$AWS_ECR_REPOSITORY_NAME
TAG=$(git rev-parse HEAD) # Get current short commit hash
# Login to AWS ECR
echo "Logging in to AWS ECR..."
aws ecr get-login-password \
--region $AWS_REGION | docker login \
--username AWS \
--password-stdin $AWS_ECR_REPOSITORY_URL
# Build the Docker image
echo "Building Docker image..."
docker build --platform linux/amd64 -t $AWS_ECR_REPOSITORY_NAME -f ./lambda_functions/docker/Dockerfile .
# Check if the ECR repository exists
echo "Checking if ECR repository exists..."
if aws ecr describe-repositories \
--repository-names $AWS_ECR_REPOSITORY_NAME \
--region $AWS_REGION > /dev/null 2>&1; then
echo "ECR repository exists, deleting..."
aws ecr delete-repository \
--repository-name $AWS_ECR_REPOSITORY_NAME \
--region $AWS_REGION \
--force
fi
# Create the ECR repository
echo "Creating ECR repository..."
aws ecr create-repository \
--repository-name $AWS_ECR_REPOSITORY_NAME \
--region $AWS_REGION \
--image-scanning-configuration scanOnPush=true \
--image-tag-mutability MUTABLE \
--no-cli-pager
# Tag the Docker image
echo "Tagging Docker image..."
docker tag $AWS_ECR_REPOSITORY_NAME:latest $AWS_ECR_REPOSITORY_URL:$TAG
# Push the Docker image
echo "Pushing Docker image..."
docker push $AWS_ECR_REPOSITORY_URL:$TAG
echo $AWS_ECR_REPOSITORY_URL:$TAG
echo "image=$AWS_ECR_REPOSITORY_NAME/$AWS_ECR_REPOSITORY_URL:$TAG" >> $GITHUB_OUTPUT
make deploy-lambda
echo "tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
terraform:
name: 'Terraform (IaC)'
Expand Down Expand Up @@ -197,20 +145,16 @@ jobs:
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}

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

- name: Build, tag, and push image to Amazon ECR (ECS)
id: build-image-ecs
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
# Build a docker container and
# push it to ECR so that it can
# be deployed to ECS.
make deploy-ecs
echo "tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
- name: Pass values to .aws/task-definition-actions.json placeholders
env:
Expand Down

0 comments on commit 0e917d9

Please sign in to comment.