Skip to content

Give write permissions to the workflow #2

Give write permissions to the workflow

Give write permissions to the workflow #2

name: Publish container image for PRODUCTION environment to AWS ECR
on:
workflow_dispatch: # Manually trigger the workflow
push:
branches:
- feat/add-build-and-deploy-github-actions
permissions:
contents: write
jobs:
tag-and-publish-image:
runs-on: ubuntu-latest
env:
ENVIRONMENT: staging
IMAGE_NAME: ${{ vars.PUBLICECR_URI }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Get full history to check existing tags
- name: Set up Git
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Determine new tag
id: tag
run: |
YEAR=$(date +'%Y')
MONTH=$(date +'%m')
DAY=$(date +'%d')
LATEST_TAG=$(git tag --sort=-v:refname | grep -E "^${YEAR}\.${MONTH}\.${DAY}\.[0-9]+$" | head -n 1 || echo "")
if [[ -z "$LATEST_TAG" ]]; then
COUNTER=1
else
COUNTER=$(( ${LATEST_TAG##*.} + 1 ))
fi
NEW_TAG="${YEAR}.${MONTH}.${COUNTER}"
echo "NEW_TAG=${NEW_TAG}" >> $GITHUB_ENV
echo "New tag: $NEW_TAG"
- name: Create Git Tag
run: |
git tag $NEW_TAG
git push origin $NEW_TAG
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.PUBLICECR_UPLOAD_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.PUBLICECR_UPLOAD_SECRET_ACCESS_KEY }}
aws-region: ${{ vars.PUBLICECR_REGION }}
- name: Authenticate with AWS Public ECR
uses: aws-actions/amazon-ecr-login@v2
with:
registry-type: public
- name: Build and push Docker image
run: |
docker build --build-arg DEPLOYMENT_ENV=production -t ${{ vars.PUBLICECR_URI }}:${{ env.NEW_TAG }} .
docker push ${{ vars.PUBLICECR_URI }}:$${{ env.NEW_TAG }}