From 24e3a831f395bd43bdd10f29adc440cb52c684e7 Mon Sep 17 00:00:00 2001 From: Lavender Shannon Date: Tue, 21 Mar 2023 11:35:26 -0500 Subject: [PATCH] Added docker-build.yml which should build solarthing-server only --- .github/workflows/codeql-analysis.yml | 1 + .github/workflows/docker-build.yml | 64 +++++++++++++++++++++++++++ .gitignore | 1 + .idea/dictionaries/lavender.xml | 3 +- docker/README.md | 12 +++++ docker/solarthing-server/Dockerfile | 14 ++++++ docker/solarthing/Dockerfile | 0 graphql/README.md | 11 +++++ graphql/build.gradle | 20 ++++++++- other/docs/todo.md | 10 +++-- 10 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/docker-build.yml create mode 100644 docker/README.md create mode 100644 docker/solarthing-server/Dockerfile create mode 100644 docker/solarthing/Dockerfile diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 5fcc8869..8a0090eb 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -50,6 +50,7 @@ jobs: # Prefix the list here with "+" to use these queries and those in the config file. # queries: ./path/to/local/query, your-org/your-repo/queries@main + # https://docs.github.com/en/actions/learn-github-actions/expressions - if: ${{ matrix.language == 'java' }} uses: actions/setup-node@v3 with: diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml new file mode 100644 index 00000000..aa5515f8 --- /dev/null +++ b/.github/workflows/docker-build.yml @@ -0,0 +1,64 @@ +name: Create and publish a Docker image + +on: + push: + branches: + - master + +env: + REGISTRY: ghcr.io + +jobs: + build-push: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + # https://github.com/marketplace/actions/checkout + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up JDK 11 + uses: actions/setup-java@v1 + with: + java-version: 11 + - uses: actions/setup-node@v3 + with: + node-version: 16 + - name: Install dependencies + run: (cd web && npm install) + - name: Build Boot Jar + run: ./gradlew graphql:bootJar + + # https://github.com/marketplace/actions/docker-login + - name: Log in to the Container registry + uses: docker/login-action@v2.1.0 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + # https://github.com/marketplace/actions/docker-metadata-action + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v4.3.0 + with: + # images: ${{ env.REGISTRY }}/${{ github.actor }}/solarthing-server + images: ${{ env.REGISTRY }}/wildmountainfarms/solarthing-server + tags: | + type=raw,value=edge,enable=${{ github.ref == 'refs/heads/master' }} + # https://github.com/marketplace/actions/docker-setup-buildx + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + # https://github.com/marketplace/actions/build-and-push-docker-images + - name: Build and push Docker image + uses: docker/build-push-action@v4.0.0 + with: + context: . + file: docker/solarthing-server/Dockerfile + build-args: + - 'BOOT_JAR=graphql/build/libs/graphql-0.0.1-SNAPSHOT.jar' + push: true + platforms: linux/amd64,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/.gitignore b/.gitignore index 7967844c..b82c7f4f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Custom stuff: +/test/ *.attach_pid* .~lock* *.log.gz diff --git a/.idea/dictionaries/lavender.xml b/.idea/dictionaries/lavender.xml index 4a640c9b..c8131438 100644 --- a/.idea/dictionaries/lavender.xml +++ b/.idea/dictionaries/lavender.xml @@ -4,6 +4,7 @@ actuals arduino aren + buildx chatbot chksum couchdb @@ -37,4 +38,4 @@ wildmountainfarms - + \ No newline at end of file diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 00000000..79a354ef --- /dev/null +++ b/docker/README.md @@ -0,0 +1,12 @@ +# docker +This directory is used to contain `Dockerfile`s to build `solarthing` and `solarthing-server` docker images. +Note that this directory is designed to be separate from the state of a codebase at any given commit. +Even while building docker images for older SolarThing versions, +the latest files in this folder should be used to build those images. + +So, the only Dockerfiles that should be used to build solarthing should be on the master branch of this repository. + +```shell +# run in solarthing root +docker build -f docker/solarthing-server/Dockerfile --build-arg BOOT_JAR=graphql/build/libs/graphql-0.0.1-SNAPSHOT.jar --tag wildmountainfarms/solarthing-server:latest . +``` diff --git a/docker/solarthing-server/Dockerfile b/docker/solarthing-server/Dockerfile new file mode 100644 index 00000000..ae275de0 --- /dev/null +++ b/docker/solarthing-server/Dockerfile @@ -0,0 +1,14 @@ +FROM eclipse-temurin:19-jre-jammy as builder +ARG BOOT_JAR +COPY ${BOOT_JAR} application.jar +RUN java -Djarmode=layertools -jar application.jar extract + + +FROM eclipse-temurin:19-jre-jammy +RUN ["mkdir", "/app"] +WORKDIR "/app" +COPY --from=builder dependencies/ ./ +COPY --from=builder snapshot-dependencies/ ./ +COPY --from=builder spring-boot-loader/ ./ +COPY --from=builder application/ ./ +ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"] diff --git a/docker/solarthing/Dockerfile b/docker/solarthing/Dockerfile new file mode 100644 index 00000000..e69de29b diff --git a/graphql/README.md b/graphql/README.md index c003fcc9..690d059e 100644 --- a/graphql/README.md +++ b/graphql/README.md @@ -23,3 +23,14 @@ For testing your skills/syntax: https://graphql.org/swapi-graphql or https://nhz Use `./gradlew graphql:bootJar` or [./graphql_compile_and_move.sh](../graphql_compile_and_move.sh) in the root directory. Or `./gradlew graphql:bootRun` to run this + +### Future `Dockerfile` + +https://reflectoring.io/spring-boot-docker/ + +When we create a `Dockerfile` from the boot jar, we will want to extract its layers. + +```shell +java -Djarmode=layertools -jar ../graphql/build/libs/graphql-0.0.1-SNAPSHOT.jar extract +# then copy the 4 folders to /app or something +``` diff --git a/graphql/build.gradle b/graphql/build.gradle index b4f676a4..1ceec1bd 100644 --- a/graphql/build.gradle +++ b/graphql/build.gradle @@ -19,7 +19,6 @@ sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 version = '0.0.1-SNAPSHOT' -//project.ext.mainClass = "me.retrodaredevil.solarthing.graphql.SolarThingApplication" // This isn't doing anything? compileJava { // TODO We don't need the -parameters compiler argument (I don't think), plus now that Java 11 is required for GraphQL, what does it do/is it needed if we did need it? @@ -27,7 +26,6 @@ compileJava { } dependencies { -// implementation "com.fasterxml.jackson.module:jackson-module-jsonSchema:$jacksonVersion" // I don't remember why we needed this implementation('org.springframework.boot:spring-boot-starter-web') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' } @@ -52,6 +50,24 @@ jar { bootRun { workingDir = new File("../program/graphql") } +/* +If we want to in the future, we can apply the 'war' plugin and configure the war task to be enabled here. +We could then use bootWar to generate a war file to be used inside a tomcat server. + */ + +/* +If we want to in the future, we can configure this. +However, configuring a buildpack (https://buildpacks.io/docs/operator-guide/create-a-builder/) is a learning curve. +The default buildpack setup actually works perfectly, but compiling to multiple architectures (platforms) + is actually a pain in the ass to find documentation on. +bootBuildImage { + // https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#build-image + // docker buildx create --use --platform linux/386,linux/amd64,linux/arm/v7,linux/arm64/v7,linux/ppc64le,linux/s390x + // https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#build-image.examples.custom-image-builder + def containerRegistry = "ghcr.io" + imageName = "$containerRegistry/wildmountainfarms/solarthing-server:latest" +} +*/ processResources { dependsOn(":web:build") diff --git a/other/docs/todo.md b/other/docs/todo.md index 847ea57b..0774b270 100644 --- a/other/docs/todo.md +++ b/other/docs/todo.md @@ -5,7 +5,8 @@ * Packet for disk usage * Send packet when mate serial port hasn't output data for 30 seconds * Create Dockerfile and example docker-compose file - * We should use https://hub.docker.com/r/azul/zulu-openjdk + * We should use https://hub.docker.com/_/eclipse-temurin/tags?page=1&name=19-jre-jammy + * And for SolarThing server: https://hub.docker.com/_/tomcat/tags?page=1&name=jre17-temurin * Short term record packets for high/low battery voltage, FX inverter current, pv wattage, charging current, etc * This would be very useful so that if packets are replaced there is still information on how high or low the battery voltage got or how high the load was @@ -27,9 +28,6 @@ don't look like they're constantly disconnecting and reconnecting * Possibly refactor PacketListReceiver * Have a way to tell if a packet included in the packets list is important enough to warrant storing in the database. * Right now we do a bunch of packets.isEmpty() checks to see if we should continue adding packets, but there's probably a better way -* Better way to update SNAPSHOT jar so that running SolarThing instances don't get mad - * We have a great setup for versioned SolarThing jar files, because running instances will still use - whatever jar solarthing.jar pointed them to originally, but this isn't the case when we actually change the jar it is pointing to * Backend Grafana plugin to allow commands to be sent * Might not actually need a backend plugin if we can just use the proxy instead * https://github.com/grafana/grafana/issues/12556 @@ -203,6 +201,10 @@ easy displaying in Grafana * Use https://github.com/tbroyer/gradle-errorprone-plugin * Add info about the state of CouchDB on the home page of SolarThing web * Add this to web: https://github.com/IvanGoncharov/graphql-voyager +* Better way to update SNAPSHOT jar so that running SolarThing instances don't get mad + * We have a great setup for versioned SolarThing jar files, because running instances will still use + whatever jar solarthing.jar pointed them to originally, but this isn't the case when we actually change the jar it is pointing to + * Completed a while ago with SNAPSHOT-1 and SNAPSHOT-2 alternating ### Look into * Look into supporting Elasticsearch, MongoDB, Graphite