|
1 |
| -name: GitHub Actions Demo |
2 |
| -run-name: ${{ github.actor }} is testing out GitHub Actions 🚀 |
3 |
| -on: [push] |
| 1 | +name: Build and Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] # main 브랜치에 push 시 동작 |
| 6 | + |
4 | 7 | jobs:
|
5 |
| - Explore-GitHub-Actions: |
6 |
| - runs-on: ubuntu-latest |
| 8 | + build: |
| 9 | + runs-on: ubuntu-24.04 |
7 | 10 | steps:
|
8 |
| - - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." |
9 |
| - - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!" |
10 |
| - - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." |
11 |
| - - name: Check out repository code |
12 |
| - uses: actions/checkout@v4 |
13 |
| - - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner." |
14 |
| - - run: echo "🖥️ The workflow is now ready to test your code on the runner." |
15 |
| - - name: List files in the repository |
| 11 | + - name: Checkout repository |
| 12 | + uses: actions/checkout@v2 |
| 13 | + |
| 14 | + - name: Login to Docker Registry |
| 15 | + # 사용 중인 레지스트리에 맞게 수정: github secrets에 docker_username과 docker_password가 미리 등록되어 있어야 함 |
| 16 | + run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login --username ${{ secrets.DOCKER_USERNAME }} --password-stdin |
| 17 | + |
| 18 | + - name: Build Docker Image |
16 | 19 | run: |
|
17 |
| - ls ${{ github.workspace }} |
18 |
| - - run: echo "🍏 This job's status is ${{ job.status }}." |
| 20 | + docker build -t my-docker-image:${{ github.sha }} . |
| 21 | + |
| 22 | + - name: Push Docker Image |
| 23 | + run: | |
| 24 | + docker push my-docker-image:${{ github.sha }} |
| 25 | +
|
| 26 | + deploy: |
| 27 | + needs: build |
| 28 | + runs-on: ubuntu-24.04 |
| 29 | + steps: |
| 30 | + - name: Deploy to EC2 via SSH |
| 31 | + |
| 32 | + with: |
| 33 | + host: ${{ secrets.EC2_HOST }} # 예: 3.34.178.2 |
| 34 | + username: ${{ secrets.EC2_USER }} # 예: ubuntu |
| 35 | + key: ${{ secrets.EC2_SSH_KEY }} # PEM 파일 내용 |
| 36 | + script: | |
| 37 | + docker pull my-docker-image:${{ github.sha }} |
| 38 | + docker stop my-container || true |
| 39 | + docker rm my-container || true |
| 40 | + docker run -d --name my-container -p 80:80 my-docker-image:${{ github.sha }} |
| 41 | +
|
0 commit comments