Skip to content

Merge pull request #45 from lordgrim18/build #7

Merge pull request #45 from lordgrim18/build

Merge pull request #45 from lordgrim18/build #7

Workflow file for this run

name: Deploy doker image to Amazon ECR
on:
push:
branches: [ "prod" ]
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: Rename image with latest latest tag in Amazon ECR
id: pull-latest
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
# Retrieve all image tags
all_tags=$(aws ecr describe-images --repository-name $ECR_REPOSITORY --query 'imageDetails[*].imageTags' --output text --region $AWS_REGION)
echo "All image tags: $all_tags"
# Extract version tags and sort them
sorted_tags=$(echo $all_tags | tr ' ' '\n' | grep -E '^[0-9]+\.[0-9]+$' | sort -V)
echo "Sorted version tags: $sorted_tags"
# Identify the latest and second latest tags
numbered_latest_tag=$(echo "$sorted_tags" | tail -n 1)
echo "Latest tag: $numbered_latest_tag"
# Increment the second latest tag to get the new tag
IFS='.' read -r major minor <<< "$numbered_latest_tag"
new_tag="$major.$((minor + 1))"
echo "New tag: $new_tag"
# Pull the latest image
docker pull $ECR_REGISTRY/$ECR_REPOSITORY:latest
# Retag the pulled image
docker tag $ECR_REGISTRY/$ECR_REPOSITORY:latest $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 with the latest tag to ECR
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT