Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added docker:setup command #22024

Closed
wants to merge 10 commits into from
2 changes: 2 additions & 0 deletions .docker/development.entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$@"
10 changes: 1 addition & 9 deletions .github/scripts/clean.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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);
Expand Down
100 changes: 100 additions & 0 deletions .github/scripts/setup-docker.js
Original file line number Diff line number Diff line change
@@ -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();
})();
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading