Skip to content

Commit

Permalink
feat: support custom git config
Browse files Browse the repository at this point in the history
This allows for, among other things, mapping URLs to provide authentication for private repos.
  • Loading branch information
shaug committed Mar 14, 2024
1 parent fa9ac03 commit bf80c8c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
11 changes: 11 additions & 0 deletions lib/pip.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,17 @@ 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
Original file line number Diff line number Diff line change
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

0 comments on commit bf80c8c

Please sign in to comment.