Skip to content

Feature/auth

Feature/auth #23

name: Build and Push image
on:
pull_request:
types:
- closed
branches:
- develop
env:
ECR_REGISTRY: 856195448505.dkr.ecr.ap-northeast-2.amazonaws.com
ECR_REPOSITORY_DEVELOP: hanghae-develop
AWS_REGION: ap-northeast-2
jobs:
publish:
if: github.event.pull_request.merged == true
name: Publish image
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Cache Gradle packages
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 with Gradle
run: ./gradlew build
- name: If build fails, notify as Slack
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
author_name: Build Failure Notification
fields: repo, message, commit, author, action, eventName, ref, workflow, job, took
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
if: ${{ failure() }}
- 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
- name: Push docker image to ECR
id: build-image
env:
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY_DEVELOP:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY_DEVELOP:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY_DEVELOP:$IMAGE_TAG" >> $GITHUB_OUTPUT
- name: Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.DEVELOP_SERVER_HOST }}
username: ${{ secrets.DEVELOP_SERVER_SSH_USERNAME }}
password: ${{ secrets.DEVELOP_SERVER_SSH_PASSWORD }}
port: ${{ secrets.DEVELOP_SERVER_SSH_PORT }}
script: |
echo ${{ steps.build-image.outputs.image }}
aws ecr get-login-password --region ${{ env.AWS_REGION }} | docker login --username AWS --password-stdin $ECR_REGISTRY/$ECR_REPOSITORY_DEVELOP
docker stop hanghae-app
docker rm hanghae-app
docker run -d --name hanghae-app -p 9001:9001 -e ACTIVE_PROFILE=develop ${{ steps.build-image.outputs.image }}
docker ps -a | grep hanghae-app