Make sure container is able to start up (#23)

Follow-up to https://github.com/matrix-org/matrix-public-archive/pull/22
This commit is contained in:
Eric Eastwood 2022-06-15 17:12:44 -05:00 committed by GitHub
parent 780600e3dd
commit bd5c14242e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 4 deletions

View File

@ -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

View File

@ -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/
@ -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
docker-health-check.js Normal file
View File

@ -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);
}
})();

View File

@ -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);
}

View File

@ -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