Skip to content

Commit 8e497f6

Browse files
fix: reduce copied files in templates (#263)
* fix: reduce copied files in templates * fix: prevent custom dockerignore issue
1 parent 300e81e commit 8e497f6

File tree

6 files changed

+107
-11
lines changed

6 files changed

+107
-11
lines changed

cli/src/execDocker/docker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Docker from 'dockerode';
22
import os from 'os';
33
import { readdir } from 'fs/promises';
44
import { createSigintAbortSignal } from '../utils/abortController.js';
5+
import { CONFIG_FILE } from '../config/config.js';
56

67
type ProgressEvent = { stream?: string };
78
type FinishOutputRow = { error?: string };
@@ -31,9 +32,10 @@ export async function dockerBuild({
3132
const osType = os.type();
3233

3334
const contextPath = process.cwd(); // Use current working directory
35+
const contextFiles = await readdir(contextPath);
3436
const buildArgs = {
3537
context: contextPath,
36-
src: await readdir(contextPath), // Include all files of the context
38+
src: contextFiles.filter((fileName) => fileName !== CONFIG_FILE), // exclude config file from build context even if not in dockerignore
3739
};
3840

3941
// by default force to build amd64 image which is architecture used in iExec workers

cli/templates/JavaScript/.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
################ IMPORTANT NOTICE ###################
2+
# Do not modify this file #
3+
# Any modifications might not be taken into account #
4+
#####################################################
5+
16
# iapp config file may contain secrets
27
iapp.config.json
38

cli/templates/JavaScript/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ FROM node:22-alpine3.21
22
WORKDIR /app
33
COPY package*.json ./
44
RUN npm ci
5-
COPY . .
5+
COPY src/ ./src/
66
ENTRYPOINT ["node", "--disable-wasm-trap-handler", "/app/src/app.js"]

cli/templates/Python3.13/.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
################ IMPORTANT NOTICE ###################
2+
# Do not modify this file #
3+
# Any modifications might not be taken into account #
4+
#####################################################
5+
16
# iapp config file may contain secrets
27
iapp.config.json
38

cli/templates/Python3.13/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ FROM python:3.13.3-alpine3.21
22
WORKDIR /app
33
COPY requirements.txt ./
44
RUN pip install -r requirements.txt
5-
COPY . .
5+
COPY src/ ./src/
66
ENTRYPOINT ["python3", "/app/src/app.py"]

cli/test/iapp.test.ts

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, beforeEach, after, afterEach, describe } from 'node:test';
2-
import { join } from 'node:path';
2+
import { dirname, join } from 'node:path';
33
import assert from 'node:assert/strict';
44
import { render, cleanup } from 'cli-testing-library';
55
import {
@@ -10,6 +10,8 @@ import {
1010
removeTestDir,
1111
retry,
1212
} from './test-utils.ts';
13+
import { fileURLToPath } from 'node:url';
14+
import { readFile, rm, writeFile } from 'node:fs/promises';
1315

1416
// Final cleanup code after all tests
1517
after(async () => {
@@ -42,10 +44,16 @@ test('iapp help command works', async () => {
4244
});
4345

4446
test('iapp -v command works', async () => {
47+
const __dirname = dirname(fileURLToPath(import.meta.url));
48+
const packageJson = JSON.parse(
49+
await readFile(join(__dirname, '../package.json'), 'utf-8')
50+
);
51+
const { version } = packageJson;
52+
4553
const { findByText, debug, clear } = await render(IAPP_COMMAND, ['-v'], {
4654
cwd: testDir,
4755
});
48-
await findByText('1.3.3');
56+
await findByText(version);
4957
// debug();
5058
clear();
5159
});
@@ -123,8 +131,6 @@ describe('JavaScript iApp', () => {
123131
await checkDockerImageContent({
124132
dockerImageId,
125133
expectedFiles: [
126-
'Dockerfile',
127-
'README.md',
128134
'node_modules',
129135
'package-lock.json',
130136
'package.json',
@@ -194,8 +200,6 @@ describe('JavaScript iApp', () => {
194200
await checkDockerImageContent({
195201
dockerImageId,
196202
expectedFiles: [
197-
'Dockerfile',
198-
'README.md',
199203
'node_modules',
200204
'package-lock.json',
201205
'package.json',
@@ -254,7 +258,7 @@ describe('Python iApp', () => {
254258
// check built docker image content
255259
await checkDockerImageContent({
256260
dockerImageId,
257-
expectedFiles: ['Dockerfile', 'README.md', 'requirements.txt', 'src'],
261+
expectedFiles: ['requirements.txt', 'src'],
258262
});
259263
});
260264
});
@@ -318,8 +322,88 @@ describe('Python iApp', () => {
318322
// check built docker image content
319323
await checkDockerImageContent({
320324
dockerImageId,
321-
expectedFiles: ['Dockerfile', 'README.md', 'requirements.txt', 'src'],
325+
expectedFiles: ['requirements.txt', 'src'],
326+
});
327+
});
328+
});
329+
});
330+
});
331+
332+
describe('Custom app', () => {
333+
describe('iapp test', () => {
334+
const projectName = 'test-iapp';
335+
336+
// Initialize a test iApp project before each test
337+
beforeEach(async () => {
338+
await initIappProject({
339+
testDir,
340+
projectName,
341+
template: 'JavaScript',
342+
projectType: 'Hello World',
343+
});
344+
});
345+
346+
test('iapp test command works', async () => {
347+
// removed .dockerignore
348+
await rm(join(testDir, projectName, '.dockerignore'));
349+
// changed Dockerfile to a custom one
350+
const dockerfileContent = await readFile(
351+
join(testDir, projectName, 'Dockerfile'),
352+
'utf-8'
353+
);
354+
const customDockerfileContent = dockerfileContent.replace(
355+
'COPY src/ ./src/',
356+
'COPY . ./'
357+
);
358+
await writeFile(
359+
join(testDir, projectName, 'Dockerfile'),
360+
customDockerfileContent,
361+
'utf-8'
362+
);
363+
364+
const { findByText, debug, clear, userEvent, getStdallStr } =
365+
await render(IAPP_COMMAND, ['test'], {
366+
cwd: join(testDir, projectName),
322367
});
368+
// wait for docker build and test run
369+
await retry(() => findByText('Would you like to see the app logs?'), {
370+
retries: 8,
371+
delay: 3000,
372+
});
373+
// extract docker image id from stdout
374+
const std = getStdallStr();
375+
const dockerImageIdMatch = std.match(
376+
/App docker image built \(sha256:[a-f0-9]{64}\)/
377+
);
378+
assert.ok(dockerImageIdMatch, 'Docker image ID not found in output');
379+
const dockerImageId = dockerImageIdMatch![0].split('(')[1].slice(0, -1);
380+
381+
// debug();
382+
clear();
383+
userEvent.keyboard('n');
384+
await findByText('Would you like to see the result?');
385+
// debug();
386+
clear();
387+
userEvent.keyboard('n');
388+
await findByText('When ready run iapp deploy');
389+
// debug();
390+
clear();
391+
392+
// check built docker image content
393+
await checkDockerImageContent({
394+
dockerImageId,
395+
expectedFiles: [
396+
'Dockerfile',
397+
'README.md',
398+
'cache',
399+
'input',
400+
'mock',
401+
'node_modules',
402+
'output',
403+
'package-lock.json',
404+
'package.json',
405+
'src',
406+
],
323407
});
324408
});
325409
});

0 commit comments

Comments
 (0)