update #124
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Push Docker Images | |
on: | |
push: | |
branches: | |
- '**' | |
tags: | |
- '**' | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
image: [ TEnv, TFlint, TerraformDocs ] # Specify the Docker images you want to build | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Get branch or tag name | |
id: image_tag | |
run: | | |
if [[ "${GITHUB_REF}" == "refs/heads/"* ]]; then | |
TAG=$(echo ${GITHUB_REF#refs/heads/} | tr '/' '-') | |
elif [[ "${GITHUB_REF}" == "refs/tags/"* ]]; then | |
TAG=$(echo ${GITHUB_REF#refs/tags/} | tr '/' '-') | |
else | |
echo "::error::Unable to determine branch or tag name." | |
exit 1 | |
fi | |
echo "TAG=$TAG" >> $GITHUB_OUTPUT | |
- 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: us-east-1 | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
with: | |
registry-type: public | |
- name: Build and push Docker images | |
env: | |
ECR_REGISTRY: "public.ecr.aws/h1a2u4u1" | |
IMAGE_TAG: ${{ steps.image_tag.outputs.TAG }} | |
run: | | |
LOWERCASE_IMAGE_NAME=$(echo ${{ matrix.image }} | tr '[:upper:]' '[:lower:]') | |
# Pull the latest image if it exists, to use as cache | |
docker pull $ECR_REGISTRY/$LOWERCASE_IMAGE_NAME:latest || true | |
# Build the image with cache from the latest tag | |
docker build \ | |
--cache-from $ECR_REGISTRY/$LOWERCASE_IMAGE_NAME:latest \ | |
-t $ECR_REGISTRY/$LOWERCASE_IMAGE_NAME:$IMAGE_TAG \ | |
./automation/${{ matrix.image }}/ | |
# Push the new image with the regular tag | |
docker push $ECR_REGISTRY/$LOWERCASE_IMAGE_NAME:$IMAGE_TAG | |
# Check if the branch is master | |
if [ "${{ github.ref }}" == "refs/heads/master" ]; then | |
# Tag the image as 'latest' | |
docker tag $ECR_REGISTRY/$LOWERCASE_IMAGE_NAME:$IMAGE_TAG $ECR_REGISTRY/$LOWERCASE_IMAGE_NAME:latest | |
# Push the 'latest' tag | |
docker push $ECR_REGISTRY/$LOWERCASE_IMAGE_NAME:latest | |
fi |