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

feat: support custom git config #833

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions index.js
Expand Up @@ -44,6 +44,8 @@ class ServerlessPythonRequirements {
dockerizePip: false,
dockerSsh: false,
dockerPrivateKey: null,
dockerGit: false,
dockerGitOptions: null,
dockerImage: null,
dockerFile: null,
dockerEnv: false,
Expand Down Expand Up @@ -86,6 +88,7 @@ class ServerlessPythonRequirements {
if (
!options.dockerizePip &&
(options.dockerSsh ||
options.dockerGit ||
options.dockerImage ||
options.dockerFile ||
options.dockerPrivateKey)
Expand Down
9 changes: 9 additions & 0 deletions lib/pip.js
Expand Up @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions test.js
Expand Up @@ -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', '../..']);
Expand Down
4 changes: 4 additions & 0 deletions tests/base/serverless.yml
Expand Up @@ -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}
Expand All @@ -29,6 +31,8 @@ custom:
dockerizePip: false
dockerSsh: false
dockerPrivateKey: ''
dockerGit: false
dockerGitOptions: ''
dockerImage: ''
individually: false
useStaticCache: true
Expand Down