Merge pull request #15 from vle-miu/master #1
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: Build and Deploy | |
on: | |
push: | |
branches: [ "master" ] | |
paths: | |
- 'src/**' | |
- '.github/workflows/ci-cd.yml' | |
pull_request: | |
branches: [ "master" ] | |
paths: | |
- 'src/**' | |
- '.github/workflows/ci-cd.yml' | |
jobs: | |
project-build: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up JDK | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
cache: maven | |
- name: Update db url | |
run: | | |
sed -i "s|<DB_URL>|${{ secrets.DB_URL }}|" src/main/resources/application.yml | |
sed -i "s|<DB_USERNAME>|${{ secrets.DB_USERNAME }}|" src/main/resources/application.yml | |
sed -i "s|<DB_PASSWORD>|${{ secrets.DB_PASSWORD }}|" src/main/resources/application.yml | |
sed -i "s|<JWT_SECRETKEY>|${{ secrets.JWT_SECRETKEY }}|" src/main/resources/application.yml | |
- name: Maven build | |
run: mvn clean install -DskipTests | |
- name: Upload build artifacts | |
uses: actions/upload-artifact@v2 | |
with: | |
name: build-artifacts | |
path: target/ | |
docker-build: | |
if: ${{ github.ref == 'refs/heads/master' }} | |
runs-on: ubuntu-latest | |
needs: | |
- project-build | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Download build artifacts | |
uses: actions/download-artifact@v2 | |
with: | |
name: build-artifacts | |
path: target/ | |
- name: Setup QEMU | |
uses: docker/setup-qemu-action@v2 | |
- name: Setup Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Login into Docker Hub | |
uses: docker/#-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Docker build and publish to docker hub | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
file: Dockerfile | |
push: true | |
tags: ${{ secrets.DOCKERHUB_TAG }}:latest | |
deploy: | |
if: ${{ github.ref == 'refs/heads/master' }} | |
needs: | |
- docker-build | |
runs-on: [ aws-ec2 ] | |
steps: | |
- name: Pull image from docker hub | |
run: sudo docker pull ${{ secrets.DOCKERHUB_TAG }}:latest | |
- name: Delete old container | |
run: | | |
sudo docker stop car-rental-api-container | |
sudo docker rm -f car-rental-api-container | |
- name: Run docker container | |
run: sudo docker run -d -p 8080:8080 --init --name car-rental-api-container ${{ secrets.DOCKERHUB_TAG }} |