-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): add docker as a build target (#14475)
- Loading branch information
1 parent
7857ae0
commit 0e018e6
Showing
14 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "setup-docker", | ||
"factory": "./src/generators/setup-docker/setup-docker", | ||
"schema": { | ||
"$schema": "http://json-schema.org/schema", | ||
"cli": "nx", | ||
"$id": "SchematicsNxSetupDocker", | ||
"title": "Nx Node Docker Options Schema", | ||
"description": "Nx Node Docker Options Schema.", | ||
"type": "object", | ||
"properties": { | ||
"projectName": { | ||
"description": "The name of the project", | ||
"$default": { "$source": "argv", "index": 0 }, | ||
"type": "string" | ||
}, | ||
"targetName": { | ||
"description": "The name of the target to create", | ||
"type": "string", | ||
"default": "docker-build" | ||
}, | ||
"buildTargetName": { | ||
"description": "The name of the build target", | ||
"type": "string", | ||
"default": "build" | ||
} | ||
}, | ||
"presets": [] | ||
}, | ||
"description": "Set up Docker configuration for a project.", | ||
"hidden": false, | ||
"implementation": "/packages/node/src/generators/setup-docker/setup-docker.ts", | ||
"aliases": [], | ||
"path": "/packages/node/src/generators/setup-docker/schema.json", | ||
"type": "generator" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/node/src/generators/setup-docker/files/Dockerfile__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM docker.io/node:lts-alpine | ||
|
||
WORKDIR /app | ||
|
||
RUN addgroup --system <%= projectName %> && \ | ||
adduser --system -G <%= projectName %> <%= projectName %> | ||
|
||
COPY <%= buildLocation %> dist | ||
RUN chown -R <%= projectName %>:<%= projectName %> . | ||
|
||
CMD [ "node", "dist" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface SetUpDockerOptions { | ||
projectName?: string; | ||
targetName?: string; | ||
buildTarget?: string; | ||
skipFormat?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"cli": "nx", | ||
"$id": "SchematicsNxSetupDocker", | ||
"title": "Nx Node Docker Options Schema", | ||
"description": "Nx Node Docker Options Schema.", | ||
"type": "object", | ||
"properties": { | ||
"projectName": { | ||
"description": "The name of the project", | ||
"$default": { "$source": "argv", "index": 0 }, | ||
"type": "string" | ||
}, | ||
"targetName": { | ||
"description": "The name of the target to create", | ||
"type": "string", | ||
"default": "docker-build" | ||
}, | ||
"buildTargetName": { | ||
"description": "The name of the build target", | ||
"type": "string", | ||
"default": "build" | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
packages/node/src/generators/setup-docker/setup-docker.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { readProjectConfiguration, Tree } from '@nrwl/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import { applicationGenerator } from '../application/application'; | ||
describe('setupDockerGenerator', () => { | ||
let tree: Tree; | ||
beforeEach(async () => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
}); | ||
|
||
describe('integrated', () => { | ||
it('should create docker assets when --docker is passed', async () => { | ||
await applicationGenerator(tree, { | ||
name: 'api', | ||
framework: 'express', | ||
e2eTestRunner: 'none', | ||
docker: true, | ||
}); | ||
|
||
const project = readProjectConfiguration(tree, 'api'); | ||
|
||
expect(tree.exists('api/Dockerfile')).toBeTruthy(); | ||
expect(project.targets).toEqual( | ||
expect.objectContaining({ | ||
'docker-build': { | ||
dependsOn: ['build'], | ||
executor: 'nx:run-commands', | ||
options: { | ||
commands: ['docker build -f api/Dockerfile .'], | ||
}, | ||
}, | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
describe('standalone', () => { | ||
it('should create docker assets when --docker is passed', async () => { | ||
await applicationGenerator(tree, { | ||
name: 'api', | ||
framework: 'fastify', | ||
rootProject: true, | ||
docker: true, | ||
}); | ||
|
||
const project = readProjectConfiguration(tree, 'api'); | ||
expect(tree.exists('Dockerfile')).toBeTruthy(); | ||
expect(project.targets).toEqual( | ||
expect.objectContaining({ | ||
'docker-build': { | ||
dependsOn: ['build'], | ||
executor: 'nx:run-commands', | ||
options: { | ||
commands: ['docker build -f Dockerfile .'], | ||
}, | ||
}, | ||
}) | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { | ||
addDependenciesToPackageJson, | ||
convertNxGenerator, | ||
formatFiles, | ||
generateFiles, | ||
GeneratorCallback, | ||
joinPathFragments, | ||
logger, | ||
readNxJson, | ||
readProjectConfiguration, | ||
Tree, | ||
updateProjectConfiguration, | ||
} from '@nrwl/devkit'; | ||
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial'; | ||
import { SetUpDockerOptions } from './schema'; | ||
|
||
function normalizeOptions( | ||
tree: Tree, | ||
setupOptions: SetUpDockerOptions | ||
): SetUpDockerOptions { | ||
return { | ||
...setupOptions, | ||
projectName: setupOptions.projectName ?? readNxJson(tree).defaultProject, | ||
targetName: setupOptions.targetName ?? 'docker-build', | ||
buildTarget: setupOptions.buildTarget ?? 'build', | ||
}; | ||
} | ||
|
||
function addDocker(tree: Tree, options: SetUpDockerOptions) { | ||
const project = readProjectConfiguration(tree, options.projectName); | ||
if (!project || !options.targetName) { | ||
return; | ||
} | ||
|
||
if (tree.exists(joinPathFragments(project.root, 'DockerFile'))) { | ||
logger.info( | ||
`Skipping setup since a Dockerfile already exists inside ${project.root}` | ||
); | ||
} else { | ||
const outputPath = | ||
project.targets[`${options.buildTarget}`]?.options.outputPath; | ||
generateFiles(tree, joinPathFragments(__dirname, './files'), project.root, { | ||
tmpl: '', | ||
app: project.sourceRoot, | ||
buildLocation: outputPath, | ||
projectName: options.projectName, | ||
}); | ||
} | ||
} | ||
|
||
export function updateProjectConfig(tree: Tree, options: SetUpDockerOptions) { | ||
let projectConfig = readProjectConfiguration(tree, options.projectName); | ||
|
||
projectConfig.targets[`${options.targetName}`] = { | ||
dependsOn: [`${options.buildTarget}`], | ||
executor: 'nx:run-commands', | ||
options: { | ||
commands: [ | ||
`docker build -f ${joinPathFragments( | ||
projectConfig.root, | ||
'Dockerfile' | ||
)} .`, | ||
], | ||
}, | ||
}; | ||
|
||
updateProjectConfiguration(tree, options.projectName, projectConfig); | ||
} | ||
|
||
export async function setupDockerGenerator( | ||
tree: Tree, | ||
setupOptions: SetUpDockerOptions | ||
) { | ||
const tasks: GeneratorCallback[] = []; | ||
const options = normalizeOptions(tree, setupOptions); | ||
// Should check if the node project exists | ||
addDocker(tree, options); | ||
updateProjectConfig(tree, options); | ||
|
||
if (!options.skipFormat) { | ||
await formatFiles(tree); | ||
} | ||
|
||
return runTasksInSerial(...tasks); | ||
} | ||
|
||
export default setupDockerGenerator; | ||
export const setupDockerSchematic = convertNxGenerator(setupDockerGenerator); |
0e018e6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nx-dev – ./
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev
nx-five.vercel.app