Skip to content

Commit

Permalink
Merge pull request #44 from software-architecture-fiap/div-build-depl…
Browse files Browse the repository at this point in the history
…oy-prod

refactor: replace build-and-deploy workflow with a new release workflow
  • Loading branch information
carolinebrasil authored Dec 7, 2024
2 parents 2d92698 + b83c31d commit 1fd1d06
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 128 deletions.
128 changes: 0 additions & 128 deletions .github/workflows/build-and-deploy.yml

This file was deleted.

155 changes: 155 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
name: release
on:
push:
branches:
- main

env:
AWS_REGION: us-east-1
ECR_REPOSITORY: app/web
EKS_CLUSTER_NAME: EKS-lanchonete-cluster

permissions:
contents: read

jobs:
build-and-deploy:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest

outputs:
image: ${{ steps.build.outputs.image }}

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Get version from CHANGELOG.md
id: get-version
run: |
if [[ ! -f CHANGELOG.md ]]; then
echo "CHANGELOG.md file not found!"
exit 1
fi
# Extract the latest version from the CHANGELOG.md
VERSION=$(grep -oP '^## \[\K[0-9]+\.[0-9]+\.[0-9]+' CHANGELOG.md | head -n 1)
if [[ -z $VERSION ]]; then
echo "Version not found in CHANGELOG.md!"
exit 1
fi
echo "Current version is: $VERSION"
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: Check PR labels
id: check-labels
run: |
PR_LABELS=$(gh pr view ${{ github.event.pull_request.number }} --json labels --jq '.labels[].name')
echo "PR labels: $PR_LABELS"
# Set version increment type based on label
if [[ "$PR_LABELS" == *"major"* ]]; then
echo "Incrementing major version."
VERSION_TYPE="major"
elif [[ "$PR_LABELS" == *"minor"* ]]; then
echo "Incrementing minor version."
VERSION_TYPE="minor"
elif [[ "$PR_LABELS" == *"patch"* ]]; then
echo "Incrementing patch version."
VERSION_TYPE="patch"
else
echo "No version label found. Defaulting to patch."
VERSION_TYPE="patch"
fi
echo "VERSION_TYPE=$VERSION_TYPE" >> $GITHUB_ENV

- name: Add version for production
run: |
# Get the current version
VERSION=${{ env.VERSION }}
VERSION_TYPE=${{ env.VERSION_TYPE }}
# Split parts
IFS='.' read -r -a VERSION_PARTS <<< "$VERSION"
MAJOR=${VERSION_PARTS[0]}
MINOR=${VERSION_PARTS[1]}
PATCH=${VERSION_PARTS[2]}
if [[ "$VERSION_TYPE" == "major" ]]; then
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
elif [[ "$VERSION_TYPE" == "minor" ]]; then
MINOR=$((MINOR + 1))
PATCH=0
else
PATCH=$((PATCH + 1))
fi
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
echo "New version is: $NEW_VERSION"
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
- name: Set image tag for production
run: echo "tag=${{ env.NEW_VERSION }}" >> $GITHUB_ENV

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }}
aws-region: ${{ env.AWS_REGION }}

- name: Login to ECR registry
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
with:
registry-type: private
mask-password: true

- name: Build and push the container image
id: build
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
GITHUB_OUTPUT: $GITHUB_ENV
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:${{ env.NEW_VERSION }} .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:${{ env.NEW_VERSION }}
- name: update CHANGELOG.md
run: |
sed -i "1i ## [${{ env.NEW_VERSION }}] - $(date +'%d-%m-%Y')\n\n### Added\n" CHANGELOG.md
echo $NEW_VERSION > VERSION
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
- name: Commit and push updated version in CHANGELOG.md
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add CHANGELOG.md VERSION
git commit -m "ci: bump version to ${{ env.NEW_VERSION }}"
git push origin ${{ github.ref }}
- name: Setup Kubeconfig
run: aws eks --region $AWS_REGION update-kubeconfig --name $EKS_CLUSTER_NAME

- name: Deploy app in Kubernetes cluster
run: |
source $GITHUB_ENV
ENVIRONMENT=production
KUSTOMIZE_DIR=$(pwd)/infra/kubernetes/production
# Check if namespace exists, if not create it
kubectl get namespace $ENVIRONMENT || kubectl create namespace $ENVIRONMENT
IMAGE_TAG=${{ env.NEW_VERSION }}
kustomize edit set image web=${{ steps.build.outputs.image }}
echo "Deploying resources in $ENVIRONMENT environment"
cd $KUSTOMIZE_DIR
echo "Using image version: ${{ env.NEW_VERSION }}"
kubectl apply -k $KUSTOMIZE_DIR -n $ENVIRONMENT && echo "Resources deployed successfully!" || (echo "Failed to deploy resources!" && exit 1)

0 comments on commit 1fd1d06

Please sign in to comment.