Update cicd-dev.yml (#49) #14
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: cicd-dev | |
on: | |
push: | |
branches: [ "dev" ] | |
env: | |
AWS_REGION: ap-northeast-2 | |
ECR_REPOSITORY: dev_morandi_backend | |
ECR_REGISTRY: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-2.amazonaws.com/dev_morandi_backend | |
EC2_BASTION_HOST: ${{ secrets.RELEASE_BASTION_HOST }} | |
EC2_BACKEND_HOST: ${{ secrets.DEV_BACKEND_HOST }} # EC2 인스턴스의 Private IP | |
GITHUB_SHA: ${{ github.sha }} | |
permissions: | |
contents: read | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
env: | |
redis.enabled: "false" | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
# Gradle 빌드를 추가합니다. | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v2 | |
with: | |
distribution: 'adopt' | |
java-version: '17' | |
- name: Cleanup application.yml | |
run: rm -f src/main/resources/application.yml | |
# GitHub Secret에서 application-prod.yml 내용을 불러와 파일로 저장 | |
- name: Create application-dev.yml from GitHub Secret | |
run: | | |
mkdir -p src/main/resources | |
echo "${{ secrets.DEV_APPLICATION_YML }}" > src/main/resources/application.yml | |
- name: Build with Gradle | |
env: | |
ORG_GRADLE_OPTS: "-Duser.timezone=Asia/Seoul" | |
run: ./gradlew clean bootJar | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.ECR_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.ECR_SECRET_ACCESS_KEY }} | |
aws-region: ${{ env.AWS_REGION }} | |
mask-aws-account-id: true | |
- name: Login to Private ECR | |
run: aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin ${{ env.ECR_REGISTRY }} | |
- name: Build Docker Image | |
run: docker build -t ${{ env.ECR_REGISTRY }}:${{ github.sha }} . | |
- name: Push Docker Image to ECR | |
run: docker push ${{ env.ECR_REGISTRY }}:${{ github.sha }} | |
deploy: | |
name: Deploy to EC2 | |
needs: build-and-push | |
runs-on: ubuntu-latest | |
env: | |
redis.enabled: "true" | |
steps: | |
- name: appleboy SSH and Deploy to EC2 | |
uses: appleboy/ssh-action@master # ssh 접속하는 오픈소스 | |
with: | |
host: ${{ env.EC2_BASTION_HOST }} | |
debug: true | |
key: ${{ secrets.BASTION_SSH_SECRET_KEY }} | |
username: ubuntu | |
port: 22 | |
envs: EC2_BACKEND_HOST,GITHUB_SHA,ECR_REGISTRY | |
script: | | |
ssh -o StrictHostKeyChecking=no ubuntu@$EC2_BACKEND_HOST "export TAG=$GITHUB_SHA && bash /home/ubuntu/morandi-backend/deploy.sh" |