Skip to content

Commit dea64c1

Browse files
authored
fix: fail to invoke pg command in CI environment (#14)
1 parent deea907 commit dea64c1

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

.github/workflows/get-data.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ jobs:
3636
- name: Install dependencies
3737
run: yarn install
3838

39+
- name: Install PostgreSQL client tools
40+
run: |
41+
sudo apt-get update
42+
sudo apt-get install -y postgresql-client
43+
3944
- name: Wait for PostgreSQL to be ready
4045
run: |
4146
echo "Waiting for PostgreSQL to be ready..."

packages/stats-db/src/dump.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { exec } from "child_process";
22
import { createGzip } from "zlib";
33
import { createReadStream, createWriteStream, statSync } from "fs";
44
import { promisify } from "util";
5-
import { unlink } from "fs/promises";
5+
import { unlink, mkdir } from "fs/promises";
66
import * as path from "path";
77
import { S3Client } from "@aws-sdk/client-s3";
88
import { Upload } from "@aws-sdk/lib-storage";
@@ -99,8 +99,19 @@ async function dumpAndGzip(
9999
const gzipFile = path.join(exportsDir, `backup-${timestamp}.sql.gz`);
100100

101101
try {
102-
// Create dump
103-
const dumpCommand = `PGPASSWORD=password docker exec -i postgres pg_dump -U postgres example_db > ${dumpFile}`;
102+
// Ensure exports directory exists
103+
await mkdir(exportsDir, { recursive: true });
104+
105+
// Create dump - detect environment and use appropriate command
106+
const isCI =
107+
process.env.CI === "true" || process.env.GITHUB_ACTIONS === "true";
108+
const dumpCommand = isCI
109+
? `PGPASSWORD=password pg_dump -h localhost -p 5432 -U postgres example_db > ${dumpFile}`
110+
: `PGPASSWORD=password docker exec -i postgres pg_dump -U postgres example_db > ${dumpFile}`;
111+
112+
console.log(
113+
`Running dump command (CI: ${isCI}): ${dumpCommand.replace("PGPASSWORD=password", "PGPASSWORD=***")}`
114+
);
104115
await execAsync(dumpCommand);
105116

106117
// Get original file size
@@ -149,7 +160,7 @@ async function dumpAndGzip(
149160
console.log("Continuing without S3 upload.");
150161
}
151162
} else if (skipUpload) {
152-
console.log("S3 upload skipped as requested.");
163+
console.log("S3 upload skipped.");
153164
} else if (!bucketName) {
154165
console.log("No S3 bucket name provided. Skipping upload.");
155166
}

0 commit comments

Comments
 (0)