migration: kubernetes cloud migration from AWS-EKS to GCP-GKE #264
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: Deployment | |
on: | |
pull_request: | |
types: [synchronize, opened] | |
branches: | |
- main | |
env: | |
ECR_URL: ${{ secrets.ECR_URL }} | |
jobs: | |
pushNewTag: | |
name: 🏷️ Push new tag | |
runs-on: ubuntu-latest | |
outputs: | |
duration: ${{ steps.calculate_duration.outputs.duration }} | |
NEW_VERSION: ${{ steps.increment_version.outputs.NEW_VERSION }} | |
ECR_URL: ${{ steps.increment_version.outputs.ECR_URL }} | |
steps: | |
- name: Record start time | |
run: echo "START_TIME=$(date +%s)" >> $GITHUB_ENV | |
- 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 }} | |
- name: Record end time | |
run: echo "END_TIME=$(date +%s)" >> $GITHUB_ENV | |
- name: Calculate duration | |
id: calculate_duration | |
run: | | |
DURATION=$((END_TIME - START_TIME)) | |
MINUTES=$((DURATION / 60)) | |
SECONDS=$((DURATION % 60)) | |
echo "::set-output name=duration::${MINUTES}m ${SECONDS}s" | |
pushImageToEcr: | |
needs: pushNewTag | |
name: 🐳 Push image to AWS-ECR | |
runs-on: ubuntu-latest | |
outputs: | |
duration: ${{ steps.calculate_duration.outputs.duration }} | |
steps: | |
- name: Record start time | |
run: echo "START_TIME=$(date +%s)" >> $GITHUB_ENV | |
- 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 }} | |
- name: Record end time | |
run: echo "END_TIME=$(date +%s)" >> $GITHUB_ENV | |
- name: Calculate duration | |
id: calculate_duration | |
run: | | |
DURATION=$((END_TIME - START_TIME)) | |
MINUTES=$((DURATION / 60)) | |
SECONDS=$((DURATION % 60)) | |
echo "::set-output name=duration::${MINUTES}m ${SECONDS}s" | |
DeployToGKECluster: | |
name: 🚀 Deploy to GKE Cluster | |
needs: [pushImageToEcr, pushNewTag] | |
runs-on: ubuntu-latest | |
permissions: | |
contents: 'read' | |
id-token: 'write' | |
outputs: | |
duration: ${{ steps.calculate_duration.outputs.duration }} | |
steps: | |
- name: Record start time | |
run: echo "START_TIME=$(date +%s)" >> $GITHUB_ENV | |
- uses: actions/checkout@v3 | |
name: Code checkout | |
- name: Configure google cloud credentials | |
id: auth | |
uses: google-github-actions/auth@v2 | |
with: | |
credentials_json: ${{ secrets.GCP_CREDENTIALS }} | |
- name: Set up gcloud cli | |
uses: google-github-actions/setup-gcloud@v2 | |
- name: Set GKE cluster context | |
uses: google-github-actions/get-gke-credentials@v2 | |
with: | |
cluster_name: cluster | |
location: asia-east1-a | |
- 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: Deploy to GKE 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 ./configmap/ | |
kubectl apply -f ./hpa/ | |
kubectl apply -f ./eks/ | |
kubectl apply -f redis.yaml | |
kubectl apply -f ./volume/ | |
kubectl apply -f ./namespace/ | |
kubectl apply -f ./service/ | |
/bin/bash deployment/scripts/deploy-scheduler.sh | |
# kubectl apply -f ./deployment/ | |
- name: Record end time | |
run: echo "END_TIME=$(date +%s)" >> $GITHUB_ENV | |
- name: Calculate duration | |
id: calculate_duration | |
run: | | |
DURATION=$((END_TIME - START_TIME)) | |
MINUTES=$((DURATION / 60)) | |
SECONDS=$((DURATION % 60)) | |
echo "::set-output name=duration::${MINUTES}m ${SECONDS}s" | |
pushSlack: | |
needs: [ pushImageToEcr, pushNewTag, DeployToGKECluster ] | |
if: always() | |
name: 📢 Slack Alert | |
runs-on: ubuntu-latest | |
steps: | |
- name: Send Slack Notification | |
uses: 8398a7/action-slack@v3 | |
with: | |
status: custom | |
fields: 'result' | |
custom_payload: | | |
{ | |
"attachments": [ | |
{ | |
"color": '${{ job.status }}' === 'success' ? 'good' : '${{ job.status }}' === 'failure' ? 'danger' : 'warning', | |
"fields": [ | |
{ | |
"title": ":rocket: New Version Deployed", | |
"value": "<${{ github.event.pull_request.html_url }}|${{ needs.pushNewTag.outputs.NEW_VERSION }}>", | |
"short": false | |
}, | |
{ | |
"title": "PR Title and Author", | |
"value": "<${{ github.event.pull_request.html_url }}|${{ github.event.pull_request.title }}> \nBy <https://github.com/${{ github.event.pull_request.user.login }}|@${{ github.event.pull_request.user.login }}>", | |
"short": false | |
}, | |
{ | |
"title": "Deployment Workflow Status", | |
"value": ":label: Push New Tag\t\t\tDuration: ${{ needs.pushNewTag.outputs.DURATION }} sec\t\tResult: ${{ needs.pushNewTag.result }}\n:whale: Push Image To ECR\tDuration: ${{ needs.pushImageToEcr.outputs.DURATION }} sec\t\tResult: ${{ needs.pushImageToEcr.result }}\n:rocket: Deploy To GKE\t\t\tDuration: ${{ needs.DeployToEKSCluster.outputs.DURATION }} sec\t\tResult: ${{ needs.DeployToEKSCluster.result }}", | |
"short": false | |
} | |
] | |
} | |
] | |
} | |
env: | |
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |