Fix: refining the steps using docker #16
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: CI-CD | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
services: | |
postgres: | |
image: postgres:13 | |
env: | |
POSTGRES_USER: ${{ secrets.DB_USER }} | |
POSTGRES_PASSWORD: ${{ secrets.DB_PASSWORD }} | |
POSTGRES_DB: ${{ secrets.DB_NAME }} | |
ports: | |
- 5432:5432 | |
options: >- | |
--health-cmd="pg_isready -U postgres -d testdb" | |
--health-interval=10s | |
--health-timeout=5s | |
--health-retries=5 | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
distribution: 'corretto' | |
java-version: '17' | |
- name: Cache Gradle packages | |
uses: actions/cache@v3 | |
with: | |
path: ~/.gradle/caches | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
- name: Build with Gradle | |
run: ./gradlew build --scan | |
- name: Build Docker image | |
run: docker build -t ${{ secrets.DOCKER_USERNAME }}/blogue:latest . | |
- name: Log in to Docker Hub | |
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin | |
- name: Push Docker image to Docker Hub | |
run: docker push ${{ secrets.DOCKER_USERNAME }}/blogue:latest | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: SSH to EC2 and pull Docker image | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ubuntu | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
docker login -u "${{ secrets.DOCKER_USERNAME }}" -p "${{ secrets.DOCKER_PASSWORD }}" | |
docker pull ${{ secrets.DOCKER_USERNAME }}/blogue:latest | |
docker stop blogue || true | |
docker rm blogue || true | |
docker run -d --name blogue -p 80:8080 ${{ secrets.DOCKER_USERNAME }}/blogue:latest |