Skip to content

Commit

Permalink
Merge pull request #38 from lordgrim18/build
Browse files Browse the repository at this point in the history
feat(actions): add ecr build workflow
  • Loading branch information
lordgrim18 authored Jul 10, 2024
2 parents aa39f89 + 52a29d0 commit 90ce16f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/CI_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: CI

on:
push:
branches: [ "main" ]
branches: [ "prod" ]
pull_request:
branches: [ "main" ]
branches: [ "main", "prod" ]

jobs:
test:
Expand Down Expand Up @@ -35,9 +35,6 @@ jobs:
run: |
touch .env
echo "${{ secrets.DEV_ENV_FILE }}" > .env
- name: Verify .env file creation
run: ls -la
- name: Run tests
run: npm test
67 changes: 67 additions & 0 deletions .github/workflows/ECR_workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Deploy to Amazon ECR

on:
push:
branches: [ "main" ]

env:
AWS_REGION: ${{ secrets.AWS_REGION }}
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }}

permissions:
contents: read

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}

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

- name: Pull the latest image from Amazon ECR
id: pull-latest
run: |
# Retrieve the latest image tag
latest_image=$(aws ecr describe-images --repository-name $ECR_REPOSITORY --query 'sort_by(imageDetails,&imagePushedAt)[-1].imageTags[0]' --output text --region $AWS_REGION)
echo "Latest image tag: $latest_image"
# Pull the latest image
docker pull $ECR_REGISTRY/$ECR_REPOSITORY:$latest_image
# Retag the pulled image
new_tag="new-tag-according-to-your-needs"
docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$latest_image $ECR_REGISTRY/$ECR_REPOSITORY:$new_tag
# Push the renamed image to ECR
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$new_tag
- name: Build, tag, and push new image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
# Build a new Docker image
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
# Tag the new image as latest
docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:latest
# Push the new image and the latest tag to ECR
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT

0 comments on commit 90ce16f

Please sign in to comment.