-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from mrc-ide/mrc-4261
mrc-4261 dockerise web app
- Loading branch information
Showing
16 changed files
with
240 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
FROM node:18 | ||
|
||
COPY package.json /app/package.json | ||
COPY package-lock.json /app/package-lock.json | ||
WORKDIR /app | ||
RUN npm ci | ||
|
||
COPY . /app | ||
|
||
RUN npm run build | ||
|
||
FROM nginx:stable | ||
|
||
COPY nginx.conf /etc/nginx/nginx.conf | ||
|
||
COPY --from=0 /app/build /usr/share/nginx/html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
user nginx; | ||
worker_processes 1; | ||
|
||
error_log /var/log/nginx/error.log warn; | ||
pid /var/run/nginx.pid; | ||
|
||
|
||
events { | ||
worker_connections 1024; | ||
} | ||
|
||
|
||
http { | ||
include /etc/nginx/mime.types; | ||
default_type application/octet-stream; | ||
|
||
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' | ||
'$status $body_bytes_sent "$http_referer" ' | ||
'"$http_user_agent" "$http_x_forwarded_for"'; | ||
|
||
access_log /var/log/nginx/access.log main; | ||
|
||
sendfile on; | ||
|
||
keepalive_timeout 65; | ||
|
||
server { | ||
listen 80; | ||
server_name localhost; | ||
root /usr/share/nginx/html; | ||
|
||
# Don't cache these files | ||
location ~* \.(?:manifest|appcache|html?|xml|json)$ { | ||
expires -1; | ||
} | ||
|
||
# CSS, JS, and source map files (do cache these) | ||
location ~* \.(?:css|js|map)$ { | ||
try_files $uri =404; | ||
expires -1; | ||
access_log off; | ||
add_header Cache-Control "public"; | ||
} | ||
|
||
# Any route that doesn't have a file extension, and which doesn't exist on the | ||
# sever, we assume is a route within the React app, and just map it to index.html | ||
location / { | ||
try_files $uri $uri/ /index.html; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
HERE=$(dirname $0) | ||
. $HERE/common | ||
|
||
PACKAGE_ROOT=$(realpath $HERE/..) | ||
|
||
docker build \ | ||
-t "$TAG_SHA" \ | ||
-t "$TAG_BRANCH" \ | ||
$PACKAGE_ROOT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
|
||
HERE=$(dirname $0) | ||
. "$HERE"/common | ||
|
||
# build docker image | ||
. "$HERE"/build | ||
|
||
docker push $TAG_SHA | ||
docker push $TAG_BRANCH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env bash | ||
set -ex | ||
|
||
if [[ -v "GITHUB_SHA" ]]; then | ||
GIT_ID=${GITHUB_SHA:0:7} | ||
else | ||
GIT_ID=$(git rev-parse --short=7 HEAD) | ||
fi | ||
|
||
if [[ -v "BRANCH_NAME" ]]; then | ||
GIT_BRANCH=${BRANCH_NAME} | ||
else | ||
GIT_BRANCH=$(git symbolic-ref --short HEAD) | ||
fi | ||
|
||
ORG=mrcide | ||
IMAGE_NAME=packit | ||
TAG_SHA="${ORG}/${IMAGE_NAME}:${GIT_ID}" | ||
TAG_BRANCH="${ORG}/${IMAGE_NAME}:${GIT_BRANCH}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
|
||
HERE=$(dirname $0) | ||
. $HERE/common | ||
|
||
docker run -d \ | ||
--network=host \ | ||
--name packit \ | ||
"$TAG_BRANCH" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env bash | ||
wait_for() | ||
{ | ||
echo "waiting up to $TIMEOUT seconds for web app" | ||
start_ts=$(date +%s) | ||
for i in $(seq $TIMEOUT); do | ||
|
||
result=$(curl --write-out %{http_code} --silent --output /dev/null http://localhost) | ||
if [[ $result -eq 200 ]]; then | ||
end_ts=$(date +%s) | ||
echo "Web app available after $((end_ts - start_ts)) seconds" | ||
break | ||
fi | ||
sleep 1 | ||
echo "...still waiting" | ||
done | ||
return $result | ||
} | ||
|
||
if [[ -v "BRANCH_NAME" ]]; then | ||
TAG=$BRANCH_NAME | ||
else | ||
TAG=$(git symbolic-ref --short HEAD) | ||
fi | ||
|
||
# The variable expansion below is 60s by default, or the argument provided | ||
# to this script | ||
TIMEOUT="${1:-60}" | ||
wait_for | ||
RESULT=$? | ||
if [[ $RESULT -ne 200 ]]; then | ||
echo "App did not become available in time" | ||
exit 1 | ||
fi | ||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
interface AppConfig { | ||
apiUrl: () => string | ||
} | ||
|
||
const devConfig: AppConfig = { | ||
apiUrl: () => "http://localhost:8080" | ||
}; | ||
|
||
const prodConfig: AppConfig = { | ||
apiUrl: () => `https://${window.location.host}/packit/api`, | ||
}; | ||
|
||
let appConfig = devConfig; | ||
|
||
if (process.env.NODE_ENV == "production") { | ||
appConfig = prodConfig; | ||
} | ||
|
||
export default appConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import appConfig from "../config/appConfig"; | ||
|
||
describe("api service", () => { | ||
|
||
const OLD_ENV = process.env; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); // Important - it clears the cache | ||
process.env = { ...OLD_ENV }; // Make a copy | ||
}); | ||
|
||
afterAll(() => { | ||
process.env = OLD_ENV; // Restore old environment | ||
}); | ||
|
||
test("uses default config by default", () => { | ||
expect(appConfig.apiUrl()).toBe("http://localhost:8080"); | ||
}); | ||
|
||
test("uses production config if node_env is production", () => { | ||
/* eslint-disable */ | ||
// @ts-ignore | ||
process.env.NODE_ENV = "production"; | ||
const appConfig = require("../config/appConfig").default; | ||
/* eslint-enable */ | ||
expect(appConfig.apiUrl()).toBe("https://localhost/packit/api"); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env bash | ||
set -euxo pipefail | ||
|
||
docker rm --force $(docker ps --all --quiet) || true | ||
docker network prune --force | ||
docker volume prune --force |