From a805b348852b1f9d876157c18bdaeb2258578db8 Mon Sep 17 00:00:00 2001 From: Allan Date: Wed, 14 Jul 2021 20:55:52 +0200 Subject: [PATCH] Configure docker and ci --- .github/workflows/ci-dev.yml | 65 +++++++++++++++++++++++++++++ .github/workflows/ci-master.yml | 74 +++++++++++++++++++++++++++++++++ RUNME.md | 23 ++++++++++ docker/Dockerfile | 14 +++++++ 4 files changed, 176 insertions(+) create mode 100644 .github/workflows/ci-dev.yml create mode 100644 .github/workflows/ci-master.yml create mode 100644 RUNME.md create mode 100644 docker/Dockerfile diff --git a/.github/workflows/ci-dev.yml b/.github/workflows/ci-dev.yml new file mode 100644 index 000000000..d09720630 --- /dev/null +++ b/.github/workflows/ci-dev.yml @@ -0,0 +1,65 @@ +name: Build and Publish Docker Image + +on: + pull_request: + branches: [ master ] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 1.11 + uses: actions/setup-java@v1 + with: + java-version: 1.11 + - name: Build with Maven + run: | + mvn -B clean package --file pom.xml + echo "::set-output name=APP_VERSION::$(mvn --file pom.xml -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)" + id: build-version + - name: Upload Artifact + uses: actions/upload-artifact@v2 + with: + name: spring-petclinic-jar + path: target/*.jar + - name: Save version + uses: noobly314/share-data@v1 + with: + share-id: app_version + mode: set + key: version + value: ${{ steps.build-version.outputs.APP_VERSION }} + + docker: + name: Docker Build and Push + runs-on: ubuntu-latest + needs: build + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 1.11 + uses: actions/setup-java@v1 + with: + java-version: 1.11 + - name: Get Version + id: get-version + uses: noobly314/share-data@v1 + with: + share-id: app_version + mode: get + key: version + - name: Download Artifact + uses: actions/download-artifact@v2 + with: + name: spring-petclinic-jar + path: target + - name: Docker Login + run: docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASS }} + - name: Docker Build Version DEV + run: docker build -t allanweber/spring-petclinic:${{ steps.get-version.outputs.data }} -f docker/Dockerfile . + - name: Build and Push Version + run: docker push allanweber/spring-petclinic:${{ steps.get-version.outputs.data }} + - name: Delete Artifact + uses: geekyeggo/delete-artifact@v1 + with: + name: spring-petclinic-jar diff --git a/.github/workflows/ci-master.yml b/.github/workflows/ci-master.yml new file mode 100644 index 000000000..98a21c510 --- /dev/null +++ b/.github/workflows/ci-master.yml @@ -0,0 +1,74 @@ +name: Build and Publish Docker Image + +on: + push: + branches: [ master ] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 1.11 + uses: actions/setup-java@v1 + with: + java-version: 1.11 + - name: Build with Maven + run: | + mvn -B clean package --file pom.xml + echo "::set-output name=APP_VERSION::$(mvn --file pom.xml -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)" + id: build-version + - name: Upload Artifact + uses: actions/upload-artifact@v2 + with: + name: spring-petclinic-jar + path: target/*.jar + - name: Create Tag + uses: negz/create-tag@v1 + with: + version: ${{ steps.build-version.outputs.APP_VERSION }} + token: ${{ secrets.GITHUB_TOKEN }} + - name: Save version + uses: noobly314/share-data@v1 + with: + share-id: app_version + mode: set + key: version + value: ${{ steps.build-version.outputs.APP_VERSION }} + + docker: + name: Docker Build and Push + runs-on: ubuntu-latest + needs: build + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 1.11 + uses: actions/setup-java@v1 + with: + java-version: 1.11 + - name: Get Version + id: get-version + uses: noobly314/share-data@v1 + with: + share-id: app_version + mode: get + key: version + - name: Download Artifact + uses: actions/download-artifact@v2 + with: + name: spring-petclinic-jar + path: target + - name: Docker Login + run: docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASS }} + - name: Docker Build Version PRD + run: docker build -t allanweber/spring-petclinic:${{ steps.get-version.outputs.data }} -f docker/Dockerfile . + - name: Build and Push Version + run: docker push allanweber/spring-petclinic:${{ steps.get-version.outputs.data }} + - name: Docker Build Latest + run: docker build -t allanweber/spring-petclinic:latest -f docker/Dockerfile . + - name: Docker Push Latest + run: docker push allanweber/spring-petclinic:latest + - name: Delete Artifact + uses: geekyeggo/delete-artifact@v1 + with: + name: spring-petclinic-jar diff --git a/RUNME.md b/RUNME.md new file mode 100644 index 000000000..b63cdc99b --- /dev/null +++ b/RUNME.md @@ -0,0 +1,23 @@ +# How to run with docker + +## To Run it locally + +1. Create a docker network: `docker network create my-network` +2. Start a mysql container in the specified network: `docker run -p 3306:3306 --name petclinicdb --net=my-network -e MYSQL_USER=petclinic -e MYSQL_PASSWORD=petclinic -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=petclinic mysql:5.7.8` +3. Run the app in the specified network: `docker run -p 8080:8080 --name spring-petclinic --net=my-network -e SPRING_PROFILES_ACTIVE=mysql -e MYSQL_URL='jdbc:mysql://petclinicdb:3306/petclinic' allanweber/spring-petclinic:2.4.5` + +## Build image and deploy it + +1. Build version: `docker build -t allanweber/spring-petclinic: -f docker/Dockerfile .` +2. Build latest: `docker build -t allanweber/spring-petclinic:latest -f docker/Dockerfile .` +3. Deploy version to registry: `docker push allanweber/spring-petclinic:` +4. Deploy latest to registry: `docker push allanweber/spring-petclinic:latest` + +* **Run prd**: docker run -p 8080:8080 --name spring-petclinic -d -e SPRING_PROFILES_ACTIVE=mysql -e MYSQL_URL=0.0.0.0:3306 -e MYSQL_USER=petclinic -e MYSQL_PASS=petclinic allanweber/spring-petclinic:2.4.5 + + +* **Remove local container**: `docker rm -f spring-petclinic` + + + +docker run -p 8080:8080 --name spring-petclinic --net=my-network -e SPRING_PROFILES_ACTIVE=mysql -e MYSQL_URL='jdbc:mysql://petclinicdb:3306/petclinic' -e MYSQL_USER=petclinic -e MYSQL_PASS=petclinic allanweber/spring-petclinic:2.4.5 diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 000000000..ae5d304e8 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,14 @@ +FROM adoptopenjdk/openjdk11:alpine-jre + +EXPOSE 8080 + +ARG Xmx="-Xmx500m" +ARG Xms="-Xmx500m" + +ENV JAVA_OPTS "$Xmx \ + $Xms \ + -XX:+UseParallelGC" + +ADD target/spring-petclinic*.jar spring-petclinic.jar + +CMD [ "sh", "-c", "java $JAVA_OPTS -jar spring-petclinic.jar" ]