diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 3bf09e8..f8cdc3e 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -10,32 +10,36 @@ jobs: runs-on: ubuntu-latest steps: - - name: Checkout the code - uses: actions/checkout@v2 + - name: Checkout the code + uses: actions/checkout@v2 - - name: Set up SSH - uses: webfactory/ssh-agent@v0.5.3 - with: - ssh-private-key: ${{ secrets.SSH_KEY }} + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 - - name: Copy files to server - run: | - scp -o StrictHostKeyChecking=no docker-compose.yml Dockerfile ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:/home/${{ secrets.SSH_USER }}/munera/ - scp -o StrictHostKeyChecking=no target/your-application.jar ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:/home/${{ secrets.SSH_USER }}/munera/ + - name: Build Docker image + run: | + docker build -t vaadin-app . - - name: Deploy on server - run: | - ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'EOF' - cd /home/${{ secrets.SSH_USER }}/munera - echo "DB_ADDRESS=${DB_ADDRESS}" > .env - echo "DB_USER=${DB_USER}" >> .env - echo "DB_PASSWORD=${DB_PASSWORD}" >> .env - echo "DB_NAME=${DB_NAME}" >> .env - docker-compose down - docker-compose up -d --build - EOF - env: - DB_ADDRESS: ${{ secrets.DB_ADDRESS }} - DB_USER: ${{ secrets.DB_USER }} - DB_PASSWORD: ${{ secrets.DB_PASSWORD }} - DB_NAME: ${{ secrets.DB_NAME }} + - name: Set up SSH + uses: webfactory/ssh-agent@v0.5.3 + with: + ssh-private-key: ${{ secrets.SSH_KEY }} + + - name: Deploy Docker image + run: | + ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'EOF' + docker pull vaadin-app || true + docker stop vaadin-app || true + docker rm vaadin-app || true + docker run -d --name vaadin-app -p 8080:8080 \ + -e DB_ADDRESS=${DB_ADDRESS} \ + -e DB_USER=${DB_USER} \ + -e DB_PASSWORD=${DB_PASSWORD} \ + -e DB_NAME=${DB_NAME} \ + vaadin-app + EOF + env: + DB_ADDRESS: ${{ secrets.DB_ADDRESS }} + DB_USER: ${{ secrets.DB_USER }} + DB_PASSWORD: ${{ secrets.DB_PASSWORD }} + DB_NAME: ${{ secrets.DB_NAME }} \ No newline at end of file diff --git a/.github/workflows/deploy.yml.bak b/.github/workflows/deploy.yml.bak new file mode 100644 index 0000000..7cbb2be --- /dev/null +++ b/.github/workflows/deploy.yml.bak @@ -0,0 +1,41 @@ +name: Deploy to Debian Server + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout the code + uses: actions/checkout@v2 + + - name: Set up SSH + uses: webfactory/ssh-agent@v0.5.3 + with: + ssh-private-key: ${{ secrets.SSH_KEY }} + + - name: Copy files to server + run: | + scp -o StrictHostKeyChecking=no docker-compose.yml Dockerfile ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:/home/${{ secrets.SSH_USER }}/munera/ + scp -o StrictHostKeyChecking=no target/munera-1.0-SNAPSHOT.jar ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:/home/${{ secrets.SSH_USER }}/munera/ + + - name: Deploy on server + run: | + ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'EOF' + cd /home/${{ secrets.SSH_USER }}/munera + echo "DB_ADDRESS=${DB_ADDRESS}" > .env + echo "DB_USER=${DB_USER}" >> .env + echo "DB_PASSWORD=${DB_PASSWORD}" >> .env + echo "DB_NAME=${DB_NAME}" >> .env + docker-compose down + docker-compose up -d --build + EOF + env: + DB_ADDRESS: ${{ secrets.DB_ADDRESS }} + DB_USER: ${{ secrets.DB_USER }} + DB_PASSWORD: ${{ secrets.DB_PASSWORD }} + DB_NAME: ${{ secrets.DB_NAME }} diff --git a/Dockerfile b/Dockerfile index 0928f9e..0f6094d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,27 @@ +# Use a Maven or Gradle image to build the JAR +FROM maven:3.8.5-openjdk-21-slim AS build + +# Set the working directory +WORKDIR /app + +# Copy the source code into the container +COPY . . + +# Build the JAR file +RUN mvn clean package -DskipTests + +# Use a lightweight image to run the JAR FROM openjdk:21-jdk-slim # Set the working directory WORKDIR /app -# Copy the JAR file into the container -COPY target/munera-1.0-SNAPSHOT.jar /app/munera-1.0-SNAPSHOT.jar +# Copy the JAR file from the build stage +COPY --from=build /app/target/munera-1.0-SNAPSHOT.jar /app/munera-1.0-SNAPSHOT.jar # Expose the port the app runs on EXPOSE 8080 # Run the JAR file CMD ["java", "-jar", "/app/munera-1.0-SNAPSHOT.jar"] +