Skip to content

Commit

Permalink
Make sure container is able to start up (#23)
Browse files Browse the repository at this point in the history
Follow-up to #22
  • Loading branch information
MadLittleMods committed Jun 15, 2022
1 parent 780600e commit bd5c142
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/containerize.yaml
Expand Up @@ -73,3 +73,24 @@ jobs:
file: 'Dockerfile'
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

# Just make sure the container can start-up and responds to the health check
test-image:
needs: [build-image]
runs-on: ubuntu-latest

services:
matrix-public-archive:
image: ${{ needs.build-image.outputs.docker_image_name }}:sha-${{ github.sha }}
credentials:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
ports:
- 3050:3050
env:
matrixServerUrl: http://FAKE_SERVER/
matrixAccessToken: FAKE_TOKEN

steps:
- name: See if the container will respond to a request
run: curl http://localhost:3050/health-check
8 changes: 7 additions & 1 deletion Dockerfile
Expand Up @@ -6,12 +6,16 @@ WORKDIR /app

RUN npm install npm@^8 --location=global

# Copy the health-check script
COPY docker-health-check.js /app/

# Copy just what we need to install the dependencies so this layer can be cached
# in the Docker build
COPY package.json package-lock.json /app/
RUN npm install

# Copy what we need for the client-side build
COPY config /app/config/
COPY public /app/public/
COPY shared /app/shared/
COPY vite.config.js /app/
Expand All @@ -21,4 +25,6 @@ RUN npm run build
# Copy the rest of the app
COPY server /app/server/

ENTRYPOINT ["npm" "start"]
HEALTHCHECK CMD node docker-health-check.js

ENTRYPOINT ["/bin/bash", "-c", "npm start"]
22 changes: 22 additions & 0 deletions docker-health-check.js
@@ -0,0 +1,22 @@
'use strict';

const assert = require('assert');

const { fetchEndpointAsJson } = require('./server/lib/fetch-endpoint');

const config = require('./server/lib/config');
const basePort = config.get('basePort');
assert(basePort);

const healthCheckUrl = `http://localhost:${basePort}/health-check`;

(async () => {
try {
await fetchEndpointAsJson(healthCheckUrl);
process.exit(0);
} catch (err) {
// eslint-disable-next-line no-console
console.log(`Health check error: ${healthCheckUrl}`, err);
process.exit(1);
}
})();
6 changes: 3 additions & 3 deletions server/lib/fetch-endpoint.js
Expand Up @@ -48,15 +48,15 @@ async function fetchEndpointAsText(endpoint, options) {

async function fetchEndpointAsJson(endpoint, options) {
const opts = {
...options,
...(options || {}),
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...(options.headers || {}),
...(options?.headers || {}),
},
};

if (options.body) {
if (options?.body) {
opts.body = JSON.stringify(options.body);
}

Expand Down
4 changes: 4 additions & 0 deletions server/routes/install-routes.js
Expand Up @@ -63,6 +63,10 @@ function parseArchiveRangeFromReq(req) {
}

function installRoutes(app) {
app.get('/health-check', async function (req, res) {
res.send('{ "ok": true }');
});

// We have to disable no-missing-require lint because it doesn't take into
// account `package.json`. `exports`, see
// https://github.com/mysticatea/eslint-plugin-node/issues/255
Expand Down

0 comments on commit bd5c142

Please sign in to comment.