Skip to content

[minor] #287 workflow trigger on pr #171

[minor] #287 workflow trigger on pr

[minor] #287 workflow trigger on pr #171

Workflow file for this run

name: Deployment
on:
pull_request:
types: [synchronize, closed]
branches:
- main
env:
ECR_URL: ${{ secrets.ECR_URL }}
jobs:
pushNewTag:
name: Push new tag
runs-on: ubuntu-latest
outputs:
NEW_VERSION: ${{ steps.increment_version.outputs.NEW_VERSION }}
ECR_URL: ${{ steps.increment_version.outputs.ECR_URL }}
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0 # get all history so we can checkout any branch
- name: Get latest tag
id: latesttag
run: |
LATEST_TAG=$(git describe --tags --abbrev=0)
echo "LATEST_TAG=$LATEST_TAG" >> $GITHUB_ENV
# Increment version number(ex) 5.0.1 -> 5.0.2)
# PR title contains "[patch]" -> 5.0.1 -> 5.0.2
# PR title contains "[minor]" -> 5.0.1 -> 5.1.0
# PR title contains "[major]" -> 5.0.1 -> 6.0.0
- name: Increment version based on commit message with commit hash
env:
PR_TITLE: ${{ github.event.pull_request.title }}
id: increment_version
run: |
current_version=${LATEST_TAG#"v"}
echo "CURRENT_VERSION=$current_version" >> $GITHUB_ENV
IFS='.' read -ra version_parts <<< "$current_version"
major=${version_parts[0]}
minor=${version_parts[1]}
patch=${version_parts[2]}
patch=$(echo $patch | cut -d'-' -f1)
echo The Title of your PR is $PR_TITLE
short_commit_hash=$(git rev-parse --short HEAD)
if [[ $PR_TITLE == *"[major]"* ]]; then
major=$(( major + 1 ))
minor=0
patch=0
elif [[ $PR_TITLE == *"[minor]"* ]]; then
minor=$(( minor + 1 ))
patch=0
else
patch=$(( patch + 1 ))
fi
new_version="$major.$minor.$patch-$short_commit_hash"
echo "Output new_version: [$new_version]"
echo "Output ecr_url: [${{ secrets.ECR_URL }}]"
echo "NEW_VERSION=$new_version" >> $GITHUB_OUTPUT
echo "ECR_URL=${{ secrets.ECR_URL }}" >> $GITHUB_OUTPUT
- name: Create and push new tag to Github
run: |
git config --global user.name 'GitHub Actions'
git config --global user.email 'actions@github.com'
git tag v${{ steps.increment_version.outputs.NEW_VERSION }}
git push origin v${{ steps.increment_version.outputs.NEW_VERSION }}
pushImageToEcr:
needs: pushNewTag
name: Push image to AWS-ECR
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Corretto openJDK 17
uses: actions/setup-java@v3 # check specific version in https://github.com/actions/setup-java
with:
distribution: 'corretto' # using Amazon openJDK
java-version: '17'
- name: Gradle caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Grant execute permission for gradlew
run: |
chmod +x ./gradlew
- name: Build project and create Dockerfiles
run: ./gradlew build --daemon --parallel -Pversion=${{ needs.pushNewTag.outputs.NEW_VERSION }}
- name: Build docker images
run: docker-compose -f docker-compose-prod.yaml build
- 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-region: ap-northeast-2
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Grant execute permission for push_to_ecr.sh
run: chmod +x ./push_to_ecr.sh
- name: Tag Push images to ECR
env:
ECR_URL: ${{ env.ECR_URL }}
run: ./push_to_ecr.sh ${ECR_URL} ${{ needs.pushNewTag.outputs.NEW_VERSION }}
DeployToEKSCluster:
name: Deplyo to AWS EKS Cluster
needs: [pushImageToEcr,pushNewTag]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
name: Code checkout
- name: Install kubectl
uses: azure/setup-kubectl@v3.0 # default is latest stable
id: install
- 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-region: ap-northeast-2
- name: Update kube config
run: |
aws eks update-kubeconfig --name spring-chat-k8s-cluster --region ap-northeast-2
kubectl config current-context
kubectl get nodes -o wide
- name: Deploy to AWS EKS Cluster
run: |
echo "connect to ECR: ${{ secrets.ECR_URL }}"
echo "new version will be deployed ${{ needs.pushNewTag.outputs.NEW_VERSION }}"
cd k8s/onlychat/deployment
sh write_image_to_deploy.sh ${{ secrets.ECR_URL }} ap-northeast-2 ${{ needs.pushNewTag.outputs.NEW_VERSION }}
cd ..
kubectl apply -f eks/aws-auth.yaml
kubectl apply -f redis.yaml
kubectl apply -f ./volume/
kubectl apply -f ./namespace/
kubectl apply -f ./service/
kubectl apply -f ./deployment/
pushSlack:
needs: [pushImageToEcr, pushNewTag, DeployToEKSCluster]
if: always()
name: Slack Alert
runs-on: ubuntu-latest
steps:
- uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
fields: job,took,author
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}