Remove extra jenkinsfile concerns

This commit is contained in:
Jake Runyan 2026-06-21 20:35:16 -07:00
parent 9f0a963f37
commit 851b67a0f5
3 changed files with 56 additions and 65 deletions

4
.dockerignore Normal file
View File

@ -0,0 +1,4 @@
node_modules
build
src/generated
.git

View File

@ -1,32 +1,28 @@
# Use the official Node.js image as a build stage # Shared dependency layer, reused by the ci and build targets.
FROM node:20-alpine AS build FROM node:20-alpine AS deps
# Set the working directory
WORKDIR /app WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
# Copy package.json and package-lock.json # Lint + content validation target, used by CI (docker build --target ci).
COPY package*.json ./ FROM deps AS ci
# Install dependencies
RUN npm install
# Copy the rest of the application code
COPY . . COPY . .
RUN node scripts/build-content.js && npx eslint src --ignore-pattern 'src/generated/**' --max-warnings=0
# Build the React application # Production build target: generates content then bundles the static site.
FROM deps AS build
COPY . .
RUN npm run build RUN npm run build
# Use a lightweight web server to serve the build files # Serve the static bundle with nginx.
FROM nginx:alpine FROM nginx:alpine
# Copy the build files from the previous stage
COPY --from=build /app/build /usr/share/nginx/html COPY --from=build /app/build /usr/share/nginx/html
# Custom config so client-side routes fall back to index.html.
# Use custom nginx config so client-side routes (e.g. /contact, /about) fall back to index.html
COPY nginx.conf /etc/nginx/conf.d/default.conf COPY nginx.conf /etc/nginx/conf.d/default.conf
# Expose port 80
EXPOSE 80 EXPOSE 80
# Liveness probe. Use 127.0.0.1 (not localhost): nginx listens IPv4-only and
# busybox wget would resolve localhost to ::1 and get connection refused.
HEALTHCHECK --interval=5s --timeout=3s --start-period=5s --retries=3 \
CMD wget -q -O /dev/null http://127.0.0.1:80/ || exit 1
# Start Nginx CMD ["nginx", "-g", "daemon off;"]
CMD ["nginx", "-g", "daemon off;"]

79
Jenkinsfile vendored
View File

@ -4,8 +4,8 @@ pipeline {
environment { environment {
DISCORD_WEBHOOK = credentials('discord-pws-builds-channel-webhook') DISCORD_WEBHOOK = credentials('discord-pws-builds-channel-webhook')
COMPOSE_PROJECT = 'jakesphotos' COMPOSE_SERVICE = 'portfolio'
APP_HOST = 'jakesphotos.whitney.rip' CONTAINER_NAME = 'jakesphotos'
} }
options { options {
@ -36,59 +36,51 @@ pipeline {
stage('Lint & Type-check') { stage('Lint & Type-check') {
steps { steps {
// Run checks in a clean, throwaway container so nothing leaks from the agent. // Static checks run inside the image's ci target; nothing touches the agent.
sh ''' sh 'docker build --target ci -t ${CONTAINER_NAME}-ci:${BUILD_NUMBER} .'
set -e
docker run --rm -v "$PWD":/app -w /app node:20-alpine sh -c '
set -e
npm ci
node scripts/build-content.js
npx eslint src --max-warnings=0
' || { echo "ERROR: static quality checks failed"; exit 1; }
'''
} }
} }
stage('Teardown') { stage('Teardown') {
steps { steps {
sh ''' sh '''
set -e set -eu
docker compose down --remove-orphans || { docker compose down --remove-orphans || true
echo "ERROR: failed to tear down previous deployment"; exit 1 # container_name is fixed, so a stale container can survive "down"
} # and then block "up" with a name conflict; reap it explicitly.
docker rm -f "$CONTAINER_NAME" >/dev/null 2>&1 || true
''' '''
} }
} }
stage('Build & Deploy') { stage('Build & Deploy') {
steps { steps {
// The React build consumes only static content/, so there are no build-time secrets to inject. // No build-time secrets: the React build consumes only static content/.
sh ''' sh 'docker compose up -d --build'
set -e
docker compose build || { echo "ERROR: build failed"; exit 1; }
docker compose up -d || { echo "ERROR: deploy failed"; exit 1; }
'''
} }
} }
stage('Health Check') { stage('Health Check') {
steps { steps {
sh ''' sh '''
set -e set -eu
echo "Waiting for $COMPOSE_PROJECT to report live..." cid="$(docker compose ps -q "$COMPOSE_SERVICE")"
for i in $(seq 1 30); do [ -n "$cid" ] || { echo "ERROR: $COMPOSE_SERVICE container not found" >&2; exit 1; }
running=$(docker inspect -f '{{.State.Running}}' "$COMPOSE_PROJECT" 2>/dev/null || echo false)
if [ "$running" != "true" ]; then # Wait for the image's HEALTHCHECK to report healthy, failing fast on terminal states.
echo "ERROR: container $COMPOSE_PROJECT is not running" deadline=$(( $(date +%s) + 90 ))
docker logs --tail 50 "$COMPOSE_PROJECT" || true while :; do
exit 1 status="$(docker inspect -f '{{.State.Status}}' "$cid")"
fi health="$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$cid")"
if docker exec "$COMPOSE_PROJECT" wget -q -O /dev/null http://localhost:80/; then [ "$status" = "running" ] && [ "$health" = "healthy" ] && break
echo "$COMPOSE_PROJECT is live."; exit 0 [ "$health" = "unhealthy" ] && { echo "ERROR: $COMPOSE_SERVICE reported unhealthy" >&2; exit 1; }
fi case "$status" in
exited|dead) echo "ERROR: $COMPOSE_SERVICE container $status before becoming healthy" >&2; exit 1 ;;
esac
[ "$(date +%s)" -ge "$deadline" ] && { echo "ERROR: timed out waiting for healthy (status=$status, health=$health)" >&2; exit 1; }
sleep 2 sleep 2
done done
echo "ERROR: $COMPOSE_PROJECT did not become healthy in time"; exit 1 echo "$COMPOSE_SERVICE healthy"
''' '''
} }
} }
@ -96,15 +88,14 @@ pipeline {
stage('Smoke Test') { stage('Smoke Test') {
steps { steps {
sh ''' sh '''
set -e set -eu
echo "Smoke testing https://$APP_HOST/ ..." cid="$(docker compose ps -q "$COMPOSE_SERVICE")"
body=$(curl -fsS --retry 5 --retry-delay 3 "https://$APP_HOST/") || { [ -n "$cid" ] || { echo "ERROR: $COMPOSE_SERVICE container not found" >&2; exit 1; }
echo "ERROR: request to https://$APP_HOST/ failed"; exit 1
} # Real request from inside the container: GET / must serve the SPA shell.
echo "$body" | grep -q 'id="root"' || { body="$(docker exec "$cid" wget -q -O - http://127.0.0.1:80/)" || { echo "ERROR: GET / did not return a successful response" >&2; exit 1; }
echo "ERROR: response did not contain expected app markup"; exit 1 echo "$body" | grep -q 'id="root"' || { echo "ERROR: GET / response missing expected app markup" >&2; exit 1; }
} echo "smoke test passed"
echo "Smoke test passed."
''' '''
} }
} }