Add deploy to aws ec2 instance step #7
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: Pipeline Backend | |
on: | |
push: | |
paths: | |
- book-sweeper/** | |
- docker/backend/Dockerfile | |
- 'docker-compose.yml' | |
- .github/workflows/*-backend.yml | |
jobs: | |
compile: | |
runs-on: ubuntu-latest | |
name: Compile project | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Setup JDK | |
uses: actions/setup-java@v4 | |
with: | |
java-version: 17 | |
distribution: 'corretto' | |
- name: Compile | |
run: | | |
cd book-sweeper | |
./mvnw clean compile | |
unit-tests: | |
runs-on: ubuntu-latest | |
name: Unit tests | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Setup JDK | |
uses: actions/setup-java@v4 | |
with: | |
java-version: 17 | |
distribution: 'corretto' | |
- name: Unit Tests | |
run: | | |
cd book-sweeper | |
./mvnw clean test | |
build: | |
runs-on: ubuntu-latest | |
needs: [compile, unit-tests] | |
name: Build project | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Setup JDK | |
uses: actions/setup-java@v4 | |
with: | |
java-version: 17 | |
distribution: 'corretto' | |
- name: Build project | |
run: | | |
cd book-sweeper | |
./mvnw clean package | |
build-image: | |
runs-on: ubuntu-latest | |
needs: [build] | |
name: Build Docker image | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Setup JDK | |
uses: actions/setup-java@v4 | |
with: | |
java-version: 17 | |
distribution: 'corretto' | |
- name: Extract version | |
id: extract_version | |
run: | | |
cd book-sweeper | |
echo "VERSION=$(./mvnw -q -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec)" >> $GITHUB_OUTPUT | |
- name: Login to Dockerhub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: book-sweeper | |
file: docker/backend/Dockerfile | |
push: true | |
platforms: linux/amd64 | |
tags: ${{ secrets.DOCKER_USERNAME }}/book-sweeper-backend:${{ steps.extract_version.outputs.VERSION }} | |
build-args: | | |
PROFILE=dev | |
APP_VERSION=${{ steps.extract_version.outputs.VERSION }} | |
deploy: | |
needs: [build, build-image] | |
runs-on: [aws-ec2] | |
name: Deploy to AWS | |
steps: | |
- name: Pull the image from dockerhub | |
run: | | |
docker pull ${{ secrets.DOCKER_USERNAME }}/book-sweeper-backend:${{ steps.extract_version.outputs.VERSION }} | |
- name: Upload docker-compose.yml to artifacts | |
uses: actions/upload-artifact@v3 | |
with: | |
name: docker-compose.yml | |
path: docker-compose.yml | |
- name: Download docker-compose from artifacts | |
uses: actions/download-artifact@v3 | |
with: | |
name: docker-compose.yml | |
- name: Deploy containers with docker-compose | |
run: | | |
docker-compose up -d --build |