Skip to content

Auto stash before merge of "main" and "origin/main" #41

Auto stash before merge of "main" and "origin/main"

Auto stash before merge of "main" and "origin/main" #41

name: Build-and-Deploy CI/CD
on:
push:
branches: [ 'main', 'dev' ]
workflow_dispatch:
env:
AWS_REGION: ap-northeast-2
ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }}
ECR_REPOSITORY: kumo-server
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup JDK 17
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'oracle'
- name: Setup Gradle 7.6
uses: gradle/gradle-build-action@v2
with:
gradle-version: 7.6
- name: make application.yml
run: |
mkdir ./src/main/resources
cd ./src/main/resources
touch ./application.yml
echo "${{ secrets.YML }}" > ./application.yml
shell: bash
- name: make application-dev.yml
run: |
cd ./src/main/resources
touch ./application-dev.yml
echo "${{ secrets.YML_DEV }}" > ./application-dev.yml
shell: bash
- name: Build Project
run: gradle build
############################################
### Start Pushing container image on ECR ###
############################################
- 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: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
#######################################################
# DEV # DEV # DEV # DEV # DEV # DEV # DEV # DEV # DEV #
#######################################################
- name: Build, tag, and push server-dev image to Amazon ECR
if: contains(github.ref, 'dev')
id: build-image-dev
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
DATE=$(date "+%y.%m.%d")
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:dev-$DATE ./
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:dev-latest ./
docker push $ECR_REGISTRY/$ECR_REPOSITORY:dev-$DATE
docker push $ECR_REGISTRY/$ECR_REPOSITORY:dev-latest
#########################################################
# MAIN # MAIN # MAIN # MAIN # MAIN # MAIN # MAIN # MAIN #
#########################################################
- name: Build, tag, and push server-main image to Amazon ECR
if: contains(github.ref, 'main')
id: build-image-main
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
DATE=$(date "+%y.%m.%d")
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:main-$DATE ./
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:main-latest ./
docker push $ECR_REGISTRY/$ECR_REPOSITORY:main-$DATE
docker push $ECR_REGISTRY/$ECR_REPOSITORY:main-latest
deploy:
needs: build
runs-on: ubuntu-latest
steps:
#######################################################
# DEV # DEV # DEV # DEV # DEV # DEV # DEV # DEV # DEV #
#######################################################
- name: Deploy on Dev Server
if: contains(github.ref, 'dev')
uses: appleboy/ssh-action@v0.1.10
with:
host: ${{ secrets.AWS_HOST_DEV }}
username: ${{ secrets.AWS_SSH_USERNAME }}
key: ${{ secrets.AWS_SSH_KEY }}
script_stop: true
script: |
password=$(aws ecr get-login-password --region ${{ env.AWS_REGION }})
docker login --username AWS --password $password ${{ env.ECR_REGISTRY }}
docker pull ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:dev-latest
docker stop ${{ env.ECR_REPOSITORY }}-dev
docker run --rm -d -p 8081:8080 --name kumo-server-dev ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:dev-latest
#########################################################
# MAIN # MAIN # MAIN # MAIN # MAIN # MAIN # MAIN # MAIN #
#########################################################
- name: Deploy on Product Server
if: contains(github.ref, 'main')
uses: appleboy/ssh-action@v0.1.10
with:
host: ${{ secrets.AWS_HOST_DEV }}
username: ${{ secrets.AWS_SSH_USERNAME }}
key: ${{ secrets.AWS_SSH_KEY }}
script_stop: true
script: |
password=$(aws ecr get-login-password --region ${{ env.AWS_REGION }})
docker login --username AWS --password $password ${{ env.ECR_REGISTRY }}
docker pull ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:main-latest
docker stop ${{ env.ECR_REPOSITORY }}-main
docker run --rm -d -p 8080:8080 --name kumo-server-main ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:main-latest