0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-08 02:52:39 -05:00

Added docker:setup yarn script (#22058)

ref
https://linear.app/ghost/issue/ENG-1959/extend-setupjs-to-modify-config-as-appropriate-for-full-docker-dev

- When switching from local development to docker, there are a few
configuration parameters that need to be updated to e.g. point to the
right database host within the docker network.
- Setting these values with environment variables doesn't work well
because the configuration passed via environment overrides the
configuration set in tests, and thus points tests to the wrong database.
- This commit adds a yarn docker:setup command to the root of the repo,
to make it easier to get started with a full docker compose based
workflow. It edits you config.local.json file to update the necessary
settings for running Ghost in docker compose.
- It also updates the clean.js script such that it will run successfully
regardless of whether it is run locally or in docker.
- Finally, this commit also adds convenience commands for developing and
running tests in docker compose
This commit is contained in:
Chris Raible 2025-01-27 21:37:40 -08:00 committed by GitHub
parent 2f63fa2302
commit 1344970128
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 108 additions and 9 deletions

View file

@ -4,5 +4,7 @@
# so we need to install dependencies again
yarn install --frozen-lockfile --prefer-offline
yarn nx run-many -t build:ts
# Execute the CMD
exec "$@"

View file

@ -1,8 +1,6 @@
// NOTE: this file can't use any NPM dependencies because it needs to run even if dependencies aren't installed yet or are corrupted
const {execSync} = require('child_process');
const isDevContainer = process.env.DEVCONTAINER === 'true';
cleanYarnCache();
resetNxCache();
deleteNodeModules();
@ -49,13 +47,7 @@ function resetNxCache() {
function cleanYarnCache() {
console.log('Cleaning yarn cache...');
try {
if (isDevContainer) {
// In devcontainer, these directories are mounted from the host so we can't delete them — `yarn cache clean` will fail
// so we delete the contents of the directories instead
execSync('rm -rf .yarncache/* .yarncachecopy/*');
} else {
execSync('yarn cache clean');
}
execSync('rm -rf .yarncache/* .yarncachecopy/*');
} catch (error) {
console.error('Failed to clean yarn cache:', error);
process.exit(1);

100
.github/scripts/setup-docker.js vendored Normal file
View file

@ -0,0 +1,100 @@
const path = require('path');
const fs = require('fs').promises;
const {spawn} = require('child_process');
/**
* Run a command and stream output to the console
*
* @param {string} command
* @param {string[]} args
* @param {object} options
*/
async function runAndStream(command, args, options) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, {
stdio: 'inherit',
...options
});
child.on('close', (code) => {
if (code === 0) {
resolve(code);
} else {
reject(new Error(`'${command} ${args.join(' ')}' exited with code ${code}`));
}
});
});
}
/**
* Removes node dependencies and cleans up local caches
*/
function clean() {
require('./clean');
}
/**
* Adjust config.local.json for Docker Compose setup
*/
async function adjustConfig() {
console.log('Adjusting configuration...');
const coreFolder = path.join(__dirname, '../../ghost/core');
const currentConfigPath = path.join(coreFolder, 'config.local.json');
let currentConfig;
try {
currentConfig = require(currentConfigPath);
} catch (err) {
currentConfig = {};
}
currentConfig.database = {
client: 'mysql',
docker: true,
connection: {
host: 'mysql',
user: 'root',
password: 'root',
database: 'ghost'
}
};
currentConfig.adapters = {
...currentConfig.adapters,
Redis: {
host: 'redis',
port: 6379
}
};
currentConfig.server = {
...currentConfig.server,
host: '0.0.0.0',
port: 2368
};
try {
await fs.writeFile(currentConfigPath, JSON.stringify(currentConfig, null, 4));
} catch (err) {
console.error('Failed to write config.local.json', err);
console.log(`Please add the following to config.local.json:\n`, JSON.stringify(currentConfig, null, 4));
process.exit(1);
}
}
async function buildContainer() {
console.log('Building container...');
await runAndStream('docker-compose', ['build'], {});
}
async function runMigrations() {
console.log('Running migrations...');
await runAndStream('docker-compose', ['run', '--rm', '-w', '/home/ghost/ghost/core', 'ghost', 'yarn', 'knex-migrator', 'init'], {cwd: path.join(__dirname, '../../')});
}
(async () => {
clean();
await adjustConfig();
await buildContainer();
await runMigrations();
})();

View file

@ -34,6 +34,11 @@
"reset:data": "cd ghost/core && node index.js generate-data --clear-database --quantities members:100000,posts:500 --seed 123",
"reset:data:empty": "cd ghost/core && node index.js generate-data --clear-database --quantities members:0,posts:0 --seed 123",
"reset:data:xxl": "cd ghost/core && node index.js generate-data --clear-database --quantities members:2000000,posts:0,emails:0,members_stripe_customers:0,members_login_events:0,members_status_events:0 --seed 123",
"docker:setup": "git submodule update --init && node .github/scripts/setup-docker.js",
"docker:dev": "COMPOSE_PROFILES=full docker compose up --attach=ghost --no-log-prefix",
"docker:test:unit": "COMPOSE_PROFILES=full docker compose run --rm --no-deps ghost yarn test:unit",
"docker:test:browser": "COMPOSE_PROFILES=full docker compose run --rm ghost yarn test:browser",
"docker:test:all": "COMPOSE_PROFILES=full docker compose run --rm ghost yarn nx run ghost:test:all",
"docker:reset": "docker compose down -v && docker compose up -d --wait",
"docker:down": "docker compose down",
"compose": "docker compose -f .devcontainer/compose.yml",