diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index bdaa0b5e..ebd2b538 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -1 +1,72 @@ # ci cd +name: ci-cd + +on: + pull_request: + branches: [BE, dev] + +env: + working-directory: ./be + APPLICATION_PROPERTIES_FILE_PATH: ./src/main/resources/application.properties + AWS_REGION: ap-northeast-2 + S3_BUCKET_NAME: team2-kiosk + CODE_DEPLOY_APPLICATION_NAME: team2-kiosk + CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: team2-kiosk + +permissions: + contents: read + +jobs: + deploy: + runs-on: ubuntu-latest + defaults: + run: + shell: bash + working-directory: ./be + + steps: + + - name: Checkout Source Code + uses: actions/checkout@v3 + + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'temurin' + + - name: Make application.properties file + run: touch '${{ env.APPLICATION_PROPERTIES_FILE_PATH }}' + + - name: Output application information + run: echo '${{ secrets.APPLICATION }}' > '${{ env.APPLICATION_PROPERTIES_FILE_PATH }}' + + - name: Grant execute permission for gradlew + run: chmod +x ./gradlew + + - name: Build with Gradle + run: ./gradlew clean build -x test + + + - name: Make zip file + run: zip -r ./$GITHUB_SHA.zip . + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v2 + 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: Upload to S3 + run: aws s3 cp + --region '${{ env.AWS_REGION }}' ./$GITHUB_SHA.zip + s3://$S3_BUCKET_NAME/Build/$GITHUB_SHA.zip + + - name: Code Deploy + run: aws deploy create-deployment + --application-name $CODE_DEPLOY_APPLICATION_NAME + --deployment-config-name CodeDeployDefault.AllAtOnce + --deployment-group-name $CODE_DEPLOY_DEPLOYMENT_GROUP_NAME + --s3-location bucket=$S3_BUCKET_NAME,bundleType=zip,key=Build/$GITHUB_SHA.zip + diff --git a/be/appspec.yml b/be/appspec.yml new file mode 100644 index 00000000..0b3695f7 --- /dev/null +++ b/be/appspec.yml @@ -0,0 +1,19 @@ +version: 0.0 # CodeDeploy 버전 +os: linux +files: + - source: / # CodeDeploy에 있는 파일 위치 + destination: /home/ubuntu/app/ # EC2로 전달할 위치 + overwrite: yes + +permissions: + - object: / + pattern: "**" + owner: ubuntu + group: ubuntu + mode: 755 + +hooks: + ApplicationStart: + - location: deploy.sh + timeout: 60 + runas: ubuntu \ No newline at end of file diff --git a/be/deploy.sh b/be/deploy.sh new file mode 100644 index 00000000..d3534d3f --- /dev/null +++ b/be/deploy.sh @@ -0,0 +1,12 @@ +KIOSK_ID=$(jps | grep be | awk '{ print $1 }') + +if [ -z $KIOSK_ID ]; then + echo "동작중인 서버가 없습니다." +else + echo "$KIOSK_ID 프로세스를 삭제합니다." + kill -9 $KIOSK_ID +fi + +echo "서버 시작" +nohup java -jar ~/app/build/libs/be-0.0.1-SNAPSHOT.jar >/home/ubuntu/app/log.txt 2>&1 & +echo "배포 성공" \ No newline at end of file