Deploy to Development and Production #117
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: Deploy to Development and Production | |
| on: | |
| workflow_dispatch: | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v3 | |
| - name: Set up JDK 21 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '21' | |
| distribution: 'zulu' | |
| - name: Make application.yml | |
| run: | | |
| mkdir -p ./src/main/resources | |
| cd ./src/main/resources | |
| touch ./application.yml | |
| if [ "${{ github.ref }}" == "refs/heads/main" ]; then | |
| echo "${{ secrets.APPLICATION_PROD }}" >> ./application.yml | |
| else | |
| echo "${{ secrets.APPLICATION_DEV }}" >> ./application.yml | |
| fi | |
| shell: bash | |
| - name: Build with Gradle | |
| run: ./gradlew bootJar | |
| - name: Docker Build & Push | |
| run: | | |
| docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | |
| if [ "${{ github.ref }}" == "refs/heads/main" ]; then | |
| IMAGE_TAG="${{ secrets.DOCKER_IMAGE_TAG_PROD }}" | |
| else | |
| IMAGE_TAG="${{ secrets.DOCKER_IMAGE_TAG_DEV }}" | |
| fi | |
| docker build -t ${{ secrets.DOCKER_REPO }}/$IMAGE_TAG . | |
| docker push ${{ secrets.DOCKER_REPO }}/$IMAGE_TAG | |
| deploy: | |
| needs: build | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| environment: [dev, prod] | |
| steps: | |
| - name: Set up Prometheus targets | |
| if: ${{ matrix.environment == 'dev' && github.ref != 'refs/heads/main' }} | |
| run: | | |
| echo "global:" > prometheus.yml | |
| echo " scrape_interval: 15s" >> prometheus.yml | |
| echo "scrape_configs:" >> prometheus.yml | |
| echo " - job_name: 'springboot-app'" >> prometheus.yml | |
| echo " metrics_path: '/actuator/prometheus'" >> prometheus.yml | |
| echo " scrape_interval: 5s" >> prometheus.yml # scrape_interval 설정 | |
| echo " static_configs:" >> prometheus.yml | |
| echo " - targets: ['${{ secrets.PROMETHEUS_TARGET_IP }}:8080']" >> prometheus.yml | |
| - name: Deploy | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ matrix.environment == 'prod' && secrets.HOST_PROD || secrets.HOST_DEV }} | |
| username: ubuntu | |
| key: ${{ matrix.environment == 'prod' && secrets.KEY_PROD || secrets.KEY_DEV }} | |
| port: 22 | |
| script: | | |
| if [ "${{ matrix.environment }}" == "prod" ]; then | |
| IMAGE_TAG="${{ secrets.DOCKER_IMAGE_TAG_PROD }}" | |
| else | |
| IMAGE_TAG="${{ secrets.DOCKER_IMAGE_TAG_DEV }}" | |
| fi | |
| sudo docker pull ${{ secrets.DOCKER_REPO }}/$IMAGE_TAG | |
| sudo docker compose down | |
| sudo docker compose up -d | |
| if: | | |
| (matrix.environment == 'prod' && github.ref == 'refs/heads/main') || | |
| (matrix.environment == 'dev' && github.ref != 'refs/heads/main') |