diff --git a/README.md b/README.md index d9127adb..64f2387f 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,28 @@ custom: With `Dockerfile` the path to the Dockerfile that must be in the current folder (or a subfolder). Please note the `dockerImage` and the `dockerFile` are mutually exclusive. +To install requirements with custom git settings (e.g., to map private repositories with +authentication parameters), add the following to your `serverless.yml`: + +```yaml +custom: + pythonRequirements: + dockerizePip: true + dockerGit: true +``` + +The `dockerGit` option will mount your `$HOME/.gitconfig` as a volume in the docker container. + +In case you want to use a different git configuration, you can specify the path (absolute) to it through `dockerGitConfig` option: + +```yaml +custom: + pythonRequirements: + dockerizePip: true + dockerGit: true + dockerGitConfig: /home/.git/config +``` + To install requirements from private git repositories, add the following to your `serverless.yml`: ```yaml diff --git a/index.js b/index.js index ca8b191f..8fb0fea3 100644 --- a/index.js +++ b/index.js @@ -44,6 +44,8 @@ class ServerlessPythonRequirements { dockerizePip: false, dockerSsh: false, dockerPrivateKey: null, + dockerGit: false, + dockerGitOptions: null, dockerImage: null, dockerFile: null, dockerEnv: false, @@ -86,6 +88,7 @@ class ServerlessPythonRequirements { if ( !options.dockerizePip && (options.dockerSsh || + options.dockerGit || options.dockerImage || options.dockerFile || options.dockerPrivateKey) diff --git a/lib/pip.js b/lib/pip.js index 40140d36..b1a238cd 100644 --- a/lib/pip.js +++ b/lib/pip.js @@ -286,6 +286,15 @@ async function installRequirements(targetFolder, pluginInstance, funcOptions) { ); } + if (options.dockerGit) { + const homePath = require('os').homedir(); + const gitConfigPath = + options.dockerGitConfig || `${homePath}/.gitconfig`; + + // Mount necessary git files to work with private repos + dockerCmd.push('-v', `${gitConfigPath}:/root/.gitconfig:z`); + } + // If we want a download cache... const dockerDownloadCacheDir = '/var/useDownloadCache'; if (options.useDownloadCache) { diff --git a/test.js b/test.js index b97f3fdc..e2efb935 100644 --- a/test.js +++ b/test.js @@ -223,6 +223,32 @@ test( { skip: !canUseDocker() || brokenOn('win32') } ); +test( + 'dockerGitConfig option correctly resolves docker command', + async (t) => { + process.chdir('tests/base'); + const path = npm(['pack', '../..']); + npm(['i', path]); + const stdout = sls(['package'], { + noThrow: true, + env: { + dockerizePip: true, + dockerGit: true, + dockerGitConfig: `${__dirname}${sep}tests${sep}base${sep}custom_gitconfig`, + dockerImage: 'break the build to log the command', + }, + }); + t.true( + stdout.includes( + `-v ${__dirname}${sep}tests${sep}base${sep}custom_gitconfig:/root/.gitconfig:z` + ), + 'docker command properly resolved' + ); + t.end(); + }, + { skip: !canUseDocker() || brokenOn('win32') } +); + test('default pythonBin can package flask with default options', async (t) => { process.chdir('tests/base'); const path = npm(['pack', '../..']); diff --git a/tests/base/serverless.yml b/tests/base/serverless.yml index 87423210..bcee3fcf 100644 --- a/tests/base/serverless.yml +++ b/tests/base/serverless.yml @@ -12,6 +12,8 @@ custom: dockerizePip: ${env:dockerizePip, self:custom.defaults.dockerizePip} dockerSsh: ${env:dockerSsh, self:custom.defaults.dockerSsh} dockerPrivateKey: ${env:dockerPrivateKey, self:custom.defaults.dockerPrivateKey} + dockerGit: ${env:dockerGit, self:custom.defaults.dockerGit} + dockerGitOptions: ${env:dockerGitOptions, self:custom.defaults.dockerGitOptions} dockerImage: ${env:dockerImage, self:custom.defaults.dockerImage} slim: ${env:slim, self:custom.defaults.slim} slimPatterns: ${file(./slimPatterns.yml):slimPatterns, self:custom.defaults.slimPatterns} @@ -29,6 +31,8 @@ custom: dockerizePip: false dockerSsh: false dockerPrivateKey: '' + dockerGit: false + dockerGitOptions: '' dockerImage: '' individually: false useStaticCache: true