Skip to content

Commit

Permalink
feat: add build and push image GHA (#20)
Browse files Browse the repository at this point in the history
* feat: add build and push image GHA

* add type
  • Loading branch information
kpplis authored Sep 5, 2024
1 parent 4475c15 commit 9b3e35a
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/build-and-push-image-to-ecr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build and push image from the PR to ECR

on:
workflow_call:
inputs:
APPLICATION_NAME:
description: The name of the application
required: true
type: string
secrets:
AWS_ACCESS_KEY_ID:
required: true
AWS_SECRET_ACCESS_KEY:
required: true

jobs:
build-and-push-image-to-ecr:
runs-on: ubuntu-latest
steps:
- name: Checkout current git repository
uses: actions/checkout@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-central-1
- name: Create ECR repository if it doesn't exist
run: |
aws ecr describe-repositories --repository-names ${{ inputs.APPLICATION_NAME }} || \
aws ecr create-repository --repository-name ${{ inputs.APPLICATION_NAME }}
LIFECYCLE_POLICY='{"rules":[{"rulePriority":1,"description":"Keep last 500 images","selection":{"tagStatus":"any","countType":"imageCountMoreThan","countNumber":500},"action":{"type":"expire"}}]}'
aws ecr put-lifecycle-policy --repository-name ${{ inputs.APPLICATION_NAME }} --lifecycle-policy-text "$LIFECYCLE_POLICY"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build and push to repository
uses: docker/build-push-action@v6
with:
file: Containerfile
platforms: linux/amd64
provenance: false
push: true
tags: |
${{ steps.login-ecr.outputs.registry }}/${{ inputs.APPLICATION_NAME }}:preview
${{ steps.login-ecr.outputs.registry }}/${{ inputs.APPLICATION_NAME }}:${{ github.event.pull_request.head.sha }}

0 comments on commit 9b3e35a

Please sign in to comment.