From b845b74c74aba091aa83257812a8dad8830db67e Mon Sep 17 00:00:00 2001 From: dar-jeeling Date: Mon, 29 Jul 2024 23:27:19 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20cd=20action=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd.yml | 77 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 .github/workflows/cd.yml diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 00000000..86d8a798 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,77 @@ +name: Java CI/CD Pipeline with Gradle and Docker + +on: + push: + branches: [ "main" ] + pull_request: # TODO: 테스트용, merge시 삭제하기 + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Get Github Actions IP + id: ip + uses: haythem/public-ip@v1.2 + + - 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: ap-northeast-2 + + - name: Add Github Actions IP to Security group + run: | + aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 + + - name: checkout + uses: actions/checkout@v3 + + - name: Set up JDK 21 + uses: actions/setup-java@v3 + with: + java-version: '21' + distribution: 'temurin' + + ## create application-database.yaml -> TODO: DB 연동할 때 활성화, submodule 사용 + # - name: make application-database.yaml + # run: | + # ## create application-database.yaml + # cd ./src/main/resources + # + # # application-database.yaml 파일 생성 + # touch ./application-database.yaml + # + # # GitHub-Actions 에서 설정한 값을 application-database.yaml 파일에 쓰기 + # echo "${{ secrets.DATABASE }}" >> ./application-database.yaml + # shell: bash + + - name: Build with Gradle + run: ./gradlew bootJar + + - name: web docker build and push + run: | + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + docker build -t ${{ secrets.DOCKER_REPO }}/fiesta-web . + docker push ${{ secrets.DOCKER_REPO }}/fiesta-web + + ## docker compose up + - name: executing remote ssh commands using password + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ubuntu + key: ${{ secrets.KEY }} + script: | + sudo docker rm -f $(docker ps -qa) + sudo docker pull ${{ secrets.DOCKER_REPO }}/fiesta-web + docker-compose up -d + docker image prune -f + + - name: Remove Github Actions IP From Security Group + run: | + aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32