Skip to content

Commit

Permalink
Add build-time args for fulfilling the browser "agent" requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvinAllen committed Oct 14, 2024
1 parent 93d2386 commit 4844cf4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/node_modules
**/dist
6 changes: 6 additions & 0 deletions .github/workflows/publish_image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 18 additions & 10 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
}

Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
};
}

}

0 comments on commit 4844cf4

Please sign in to comment.