Skip to content

Trigger prod image build #198

Trigger prod image build

Trigger prod image build #198

Workflow file for this run

name: Build and Push Docker Images
on:
push:
branches:
- main
- develop
permissions:
contents: write
jobs:
build-and-deploy:
runs-on: ubuntu-latest
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Define environment and image tag
run: |
SHORT_SHA="${GITHUB_SHA::7}"
if [ "${GITHUB_REF_NAME}" = "main" ]; then
echo "DEPLOY_ENV=prod" >> $GITHUB_ENV
echo "IMAGE_TAG=prod-${SHORT_SHA}" >> $GITHUB_ENV
else
echo "DEPLOY_ENV=test" >> $GITHUB_ENV
echo "IMAGE_TAG=test-${SHORT_SHA}" >> $GITHUB_ENV
fi
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install backend dependencies
run: |
cd services/backend
pip install -r requirements.txt
- name: Run backend tests
run: |
cd services/backend
export PYTHONPATH=$PYTHONPATH:.
pytest tests/
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push Backend
uses: docker/build-push-action@v6
with:
context: ./services/backend
file: ./services/backend/Dockerfile
platforms: linux/amd64
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/task-tracker:${{ env.IMAGE_TAG }}
- name: Scan Backend image
uses: aquasecurity/trivy-action@master
with:
image-ref: '${{ secrets.DOCKERHUB_USERNAME }}/task-tracker:${{ env.IMAGE_TAG }}'
format: 'table'
exit-code: '1'
ignore-unfixed: true
vuln-type: 'os,library'
severity: 'CRITICAL,HIGH'
- name: Build and push Consumer
uses: docker/build-push-action@v6
with:
context: ./services/consumer
file: ./services/consumer/Dockerfile
platforms: linux/amd64
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/consumer:${{ env.IMAGE_TAG }}
- name: Build and push Frontend
uses: docker/build-push-action@v6
with:
context: ./services/frontend
file: ./services/frontend/Dockerfile
platforms: linux/amd64
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/task-tracker-frontend:${{ env.IMAGE_TAG }}
- name: Update Kustomize image tags
run: |
if [ "${DEPLOY_ENV}" = "test" ]; then
BACKEND_FILE="kustomize/overlays/test/backend/kustomization.yaml"
CONSUMER_FILE="kustomize/overlays/test/consumer/kustomization.yaml"
FRONTEND_FILE="kustomize/overlays/test/frontend/kustomization.yaml"
else
BACKEND_FILE="kustomize/overlays/prod/backend/kustomization.yaml"
CONSUMER_FILE="kustomize/overlays/prod/consumer/kustomization.yaml"
FRONTEND_FILE="kustomize/overlays/prod/frontend/kustomization.yaml"
fi
sed -i "s/newTag:.*/newTag: ${IMAGE_TAG}/" "$BACKEND_FILE"
sed -i "s/newTag:.*/newTag: ${IMAGE_TAG}/" "$CONSUMER_FILE"
sed -i "s/newTag:.*/newTag: ${IMAGE_TAG}/" "$FRONTEND_FILE"
- name: Commit updated image tags
run: |
git config user.name "github-actions"
git config user.email "github-actions@github.com"
git add kustomize/overlays/${DEPLOY_ENV}/backend/kustomization.yaml
git add kustomize/overlays/${DEPLOY_ENV}/consumer/kustomization.yaml
git add kustomize/overlays/${DEPLOY_ENV}/frontend/kustomization.yaml
git commit -m "Update ${DEPLOY_ENV} image tags to ${IMAGE_TAG} [skip ci]" || echo "No changes to commit"
git push origin ${GITHUB_REF_NAME}