From 4844cf441e89277f3fe2ecb9e8d40dc300adfc52 Mon Sep 17 00:00:00 2001 From: "Calvin A. Allen" Date: Mon, 14 Oct 2024 13:46:46 -0400 Subject: [PATCH] Add build-time args for fulfilling the browser "agent" requirements --- .dockerignore | 2 ++ .github/workflows/publish_image.yml | 6 ++++ Dockerfile | 26 ++++++++++++++++- build.gradle | 28 ++++++++++++------- .../petclinic/PetClinicApplication.java | 20 ------------- 5 files changed, 51 insertions(+), 31 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e0a57b2 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +**/node_modules +**/dist diff --git a/.github/workflows/publish_image.yml b/.github/workflows/publish_image.yml index 8844d42..654d42b 100644 --- a/.github/workflows/publish_image.yml +++ b/.github/workflows/publish_image.yml @@ -41,6 +41,12 @@ jobs: tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} github-token: ${{ secrets.GITHUB_TOKEN }} + build-args: | + BROWSER_LICENSE_KEY=${{ secrets.BROWSER_LICENSE_KEY }}, + BROWSER_ACCOUNT_ID=${{ secrets.BROWSER_ACCOUNT_ID }}, + BROWSER_TRUST_KEY=${{ secrets.BROWSER_TRUST_KEY }}, + BROWSER_AGENT_ID=${{ secrets.BROWSER_AGENT_ID }}, + BROWSER_APPLICATION_ID=${{ secrets.BROWSER_APPLICATION_ID }} - name: Generate artifact attestation uses: actions/attest-build-provenance@v1 diff --git a/Dockerfile b/Dockerfile index 74261a4..0e41fa3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,10 +7,34 @@ EXPOSE 8081 FROM gradle:jdk17 AS build WORKDIR /src + +ENV NODE_VERSION=20.15.1 +RUN apt-get update && apt-get install -y curl +RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/refs/tags/v0.40.1/install.sh | bash +ENV NVM_DIR=/root/.nvm +RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION} +RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION} +RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION} +ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}" + COPY src ./src +COPY client ./client COPY gradle ./gradle -COPY build.gradle settings.gradle ./ +COPY build.gradle settings.gradle browserMonitoringTemplate.js ./ COPY --chmod=0755 gradlew ./ + +ARG BROWSER_LICENSE_KEY +ARG BROWSER_ACCOUNT_ID +ARG BROWSER_TRUST_KEY +ARG BROWSER_AGENT_ID +ARG BROWSER_APPLICATION_ID + +ENV BROWSER_LICENSE_KEY=$BROWSER_LICENSE_KEY +ENV BROWSER_ACCOUNT_ID=$BROWSER_ACCOUNT_ID +ENV BROWSER_TRUST_KEY=$BROWSER_TRUST_KEY +ENV BROWSER_AGENT_ID=$BROWSER_AGENT_ID +ENV BROWSER_APPLICATION_ID=$BROWSER_APPLICATION_ID + RUN --mount=type=cache,target=/root/.gradle ./gradlew build --console=plain --info --no-daemon --no-watch-fs FROM base AS final diff --git a/build.gradle b/build.gradle index 5375ee6..4bf99c9 100644 --- a/build.gradle +++ b/build.gradle @@ -55,6 +55,11 @@ dependencies { developmentOnly 'org.springframework.boot:spring-boot-devtools' } +build { + dependsOn 'buildFrontEnd' + mustRunAfter 'buildFrontEnd' +} + bootRun { dependsOn 'downloadNewRelicAgent', 'buildFrontEnd', 'build' mustRunAfter 'downloadNewRelicAgent', 'buildFrontEnd', 'build' @@ -66,12 +71,12 @@ tasks.register('buildFrontEnd', DefaultTask) { group 'New Relic' doFirst { exec { - String cmd = 'npm'; + String cmd = 'npm' if (Os.isFamily(Os.FAMILY_WINDOWS)) { - cmd = "npm.cmd"; + cmd = "npm.cmd" } - workingDir 'client/' + workingDir "$rootDir/client" commandLine cmd, 'run', 'build' } @@ -86,11 +91,11 @@ tasks.register('buildFrontEnd', DefaultTask) { } doLast { - def newRelicLicenseKey = System.getenv('NEW_RELIC_LICENSE_KEY') - def newRelicAccountId = System.getenv('NEW_RELIC_ACCOUNT_ID') - def newRelicTrustKey = System.getenv('NEW_RELIC_TRUST_KEY') - def newRelicAgentId = System.getenv('NEW_RELIC_AGENT_ID') - def newRelicApplicationId = System.getenv('NEW_RELIC_APPLICATION_ID') + def newRelicLicenseKey = System.getenv('BROWSER_LICENSE_KEY') + def newRelicAccountId = System.getenv('BROWSER_ACCOUNT_ID') + def newRelicTrustKey = System.getenv('BROWSER_TRUST_KEY') + def newRelicAgentId = System.getenv('BROWSER_AGENT_ID') + def newRelicApplicationId = System.getenv('BROWSER_APPLICATION_ID') if (newRelicTrustKey == null && newRelicAccountId != null) { newRelicTrustKey = newRelicAccountId @@ -101,10 +106,10 @@ tasks.register('buildFrontEnd', DefaultTask) { } if (newRelicLicenseKey == null || newRelicAccountId == null || newRelicTrustKey == null || newRelicAgentId == null || newRelicApplicationId == null) { - throw new GradleException('NEW_RELIC_* ENVIRONMENT VARIABLES NOT SET') + throw new GradleException('BROWSER_* ENVIRONMENT VARIABLES NOT SET') } - def templateJavaScript = file("$rootDir/browserMonitoringTemplate.js"); + def templateJavaScript = file("$rootDir/browserMonitoringTemplate.js") def indexFile = file("$rootDir/src/main/resources/static/index.html") def indexFileContents = indexFile.getText() @@ -121,6 +126,9 @@ tasks.register('buildFrontEnd', DefaultTask) { indexFile.write(newIndexFileContent) } + + outputs.file('src/main/resources/static/index.html') + outputs.dir('src/main/resources/static/assets') } tasks.register('downloadNewRelicAgent', DefaultTask) { diff --git a/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java b/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java index c2d3721..d19fa9a 100644 --- a/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java +++ b/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java @@ -19,10 +19,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ImportRuntimeHints; -import org.springframework.web.servlet.config.annotation.CorsRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * PetClinic Spring Boot Application. @@ -34,24 +31,7 @@ @ImportRuntimeHints(PetClinicRuntimeHints.class) @EnableScheduling public class PetClinicApplication { - public static void main(String[] args) { SpringApplication.run(PetClinicApplication.class, args); } - - @Bean - public WebMvcConfigurer corsConfigurer() { - return new WebMvcConfigurer() { - @Override - public void addCorsMappings(CorsRegistry registry) { - registry.addMapping("/**") - .allowedOrigins("http://localhost:5173") - .allowedHeaders("*") - .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD", "PATCH") - .maxAge(-1) // add maxAge - .allowCredentials(false); - } - }; - } - }