[chore] github ci/cd 워크플로 설정 #3
Workflow file for this run
This file contains hidden or 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: CD with Gradle and Docker | |
| on: | |
| push: | |
| branches: | |
| - 'develop' | |
| pull_request: | |
| branches: | |
| - 'develop' | |
| permissions: | |
| contents: read | |
| env: | |
| RESOURCE_PATH: ./src/main/resources | |
| COMPOSE_PATH: /home/ec2-user/compose | |
| APP_PORT : 8000 | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: ☕️ set up JDK 17 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| server-id: github | |
| settings-path: ${{ github.workspace }} | |
| - name: 🧾 Create application.yml from secret | |
| run: | | |
| cd ${{env.RESOURCE_PATH}} | |
| echo "${{ secrets.APPLICATION_YML_DEV }}" > application.yml | |
| echo "===== application.yml content start =====" | |
| cat application.yml | |
| echo "===== application.yml content end =====" | |
| - name: 🐘 Cache Gradle dependencies | |
| uses: actions/cache@v3 | |
| with: | |
| path: | | |
| ~/.gradle/caches | |
| ~/.gradle/wrapper | |
| key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
| restore-keys: | | |
| gradle-${{ runner.os }}- | |
| - name: 👏🏻 grant execute permission for gradlew | |
| run: chmod +x gradlew | |
| - name: 🐘 build with Gradle (without test) | |
| run: ./gradlew clean build -x test --stacktrace | |
| - name: 🐳 Docker build & push | |
| run: | | |
| docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | |
| docker build --build-arg PORT=${{env.APP_PORT}} -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }} . | |
| docker push ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }} | |
| - name: 🫴🏻 Get Public IP | |
| id: ip | |
| uses: haythem/[email protected] | |
| - name: 🪪 Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| 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: 🚀 deploy to server | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USERNAME }} | |
| key: ${{ secrets.EC2_KEY }} | |
| port: ${{ secrets.EC2_PORT }} | |
| envs: GITHUB_SHA | |
| script: | | |
| echo "🗂️ Change Directory to EC2 Root" | |
| cd ${{env.COMPOSE_PATH}} | |
| echo "✋🏻Stopping existing container and Cleaning up old images" | |
| sudo docker-compose down --rmi all | |
| sudo docker ps -a | |
| echo "🥳 Pulling new image" | |
| sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }} | |
| echo "🌱 Starting new container" | |
| sudo docker-compose up -d | |
| sudo docker image prune -f |