CI/CD #59
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: Java CI CD with Gradle | |
on: | |
workflow_dispatch: | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
environment: production | |
steps: | |
- name: ✅Checkout branch | |
uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
## create application.yml & application-dev.yml | |
- name: 🗂️Create application.yml | |
run: | | |
cd ./src/main/resources | |
## application.yml 파일 생성 | |
touch application.yml | |
echo "${{ secrets.APPLICATION_YML }}" >> application.yml | |
## application-prod.yml 파일 생성 | |
touch application-prod.yml | |
echo "${{ secrets.APPLICATION_PROD }}" >> application-prod.yml | |
shell: bash | |
## ⚙️jar 빌드 | |
- name: Grant execute permission for gradlew & Build with Gradle | |
run: | | |
chmod +x ./gradlew | |
./gradlew build | |
shell: bash | |
## AWS 인증 | |
- name: 🌎 Access to AWS | |
uses: aws-actions/configure-aws-credentials@v2 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }} | |
aws-region: ap-northeast-2 | |
## 🐋 도커 빌드 & ECR Push | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v2 | |
- name: Build, tag, and push docker image to Amazon ECR | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }} | |
IMAGE_TAG: ${{ github.sha }} | |
run: | | |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
mkdir ./scripts | |
touch scripts/deploy.sh | |
echo "aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin $ECR_REGISTRY" >> scripts/deploy.sh | |
echo "docker pull $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> scripts/deploy.sh | |
echo "echo '$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG' > ~/scripts/IMAGE_ID" >> scripts/deploy.sh | |
echo "sudo ~/scripts/start.sh" >> scripts/deploy.sh | |
## 스크립트 + appscpec.yml 압축 | |
- name: 📦 Zip project files | |
run: zip -r ./$GITHUB_SHA.zip ./scripts appspec.yml | |
shell: bash | |
## Zip S3 업로드 | |
- name: 🚛 Upload to S3 | |
run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://${{ secrets.S3_BUCKET_NAME }}/${{ secrets.S3_BUCKET_DIR_NAME }}/$GITHUB_SHA.zip | |
## CodeDeploy 배포 요청 | |
- name: 🚀 Deploy to EC2 with CodeDeploy | |
run: aws deploy create-deployment | |
--application-name codedeploy | |
--deployment-config-name CodeDeployDefault.AllAtOnce | |
--deployment-group-name ${{ secrets.DEPLOYMENT_GROUP_NAME }} | |
--s3-location bucket=${{ secrets.S3_BUCKET_NAME }},bundleType=zip,key=${{ secrets.S3_BUCKET_DIR_NAME }}/$GITHUB_SHA.zip | |