Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .dagger/modules/e2e/main.dang
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,37 @@ type E2e {
null
}

"""
A git token reaches git's credential prompt for its own host only, and never
lands in the container's plain environment.
"""
gitTokenCheck(ws: Workspace!): Void @check {
let token = setSecret(name: "e2e-git-token", plaintext: "e2e-not-a-real-token")
let base = pyproject(markers: ["pyproject.toml"], gitToken: token).base
let ask = `| GIT_TERMINAL_PROMPT=0 git credential fill`
# Compared in the shell: stdout would come back with the secret scrubbed.
base
.withExec([
"sh",
"-c",
`test "$(printf 'protocol=https\nhost=github.com\n\n' ` + ask + ` | grep '^password=')" = "password=$PYPROJECT_GIT_TOKEN"`,
])
.sync
let elsewhere = { base.withExec(["sh", "-c", `printf 'protocol=https\nhost=gitlab.com\n\n' ` + ask]).stdout } rescue {
err: Error => "refused"
}
assert(elsewhere == "refused", "the git token was offered to another host")
assert(
base.envVariable("PYPROJECT_GIT_TOKEN") == null,
"the git token is a plain environment variable",
)
assert(
pyproject(markers: ["pyproject.toml"]).base.envVariable("GIT_CONFIG_COUNT") == null,
"a base with no git token carries a credential helper",
)
null
}

"""
mypy checks what the project's config names with `files`, rather than every
file below the project root, and both checkers take extra arguments.
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ pytest a base that has one:
$ dagger settings pytest base ghcr.io/astral-sh/uv:python3.14-bookworm
```

A git dependency from a private repository needs a token. `gitToken` takes a
secret, which git presents to `gitHost` (`github.com` by default) and to no
other host. It reaches git through a credential helper declared in `GIT_CONFIG_*`
variables, so it is written to no file, URL or `uv.lock`. In CI, a GitHub App
installation token with read access to the repository is a good fit:

```toml
[modules.pytest.settings]
gitToken = "env://GIT_TOKEN"
```

A failure names the step: the environment, or the tool, with its command, exit
code and the end of its output.

Expand Down Expand Up @@ -332,6 +343,8 @@ mount.
| `nesting` | `true` | Let tests reach a nested Dagger engine. |
| `version` | `3.14` | Python version of the default base image. |
| `base` | none | Base image with Python and uv, instead of the default one. |
| `gitToken` | none | Secret git presents to `gitHost`, for private git dependencies. |
| `gitHost` | `github.com` | Host `gitToken` is presented to. |

On a project: `tests`, `test`, `has-tests`, `version`, `skip`. `tests` is a
collection of the project's test files, keyed by project-relative path, and its
Expand Down Expand Up @@ -387,6 +400,8 @@ nesting = false
| `auditArgs` | `[]` | Extra arguments for `uv audit`, e.g. `["--no-dev"]`. |
| `version` | `3.14` | Python version of the default base image. |
| `base` | none | Base image with Python and uv, instead of the default one. |
| `gitToken` | none | Secret git presents to `gitHost`, for private git dependencies. |
| `gitHost` | `github.com` | Host `gitToken` is presented to. |

On a project: `audit` (a `@check`, `uv audit`), `lock` (a `@generate`,
`uv lock`), `build` (`uv build`, returns `dist` with the wheel and sdist),
Expand Down Expand Up @@ -421,6 +436,8 @@ failing it would be wrong.
| `args` | `[]` | Extra arguments for every run, e.g. `["--strict"]`. |
| `version` | `3.14` | Python version of the default base image. |
| `base` | none | Base image with Python and uv. |
| `gitToken` | none | Secret git presents to `gitHost`, for private git dependencies. |
| `gitHost` | `github.com` | Host `gitToken` is presented to. |

On a project: `type-check` (a `@check`), `has-sources`, `version`, `skip`, and
for mypy `configures-files`. Both modules name the check `type-check`, so
Expand Down
15 changes: 14 additions & 1 deletion mypy/main.dang
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type Mypy {

let pythonVersion: String
let base: Container
let gitToken: Secret
let gitHost: String!

new(
"""
Expand All @@ -59,6 +61,15 @@ type Mypy {
"""
base: Container = null,
"""
HTTPS token git presents to gitHost, for a dependency sourced from a
private git repository, such as a GitHub App installation token.
"""
gitToken: Secret = null,
"""
Host gitToken is presented to, and to no other.
"""
gitHost: String! = "github.com",
"""
Project roots to check. A bare pattern selects, a "!"-prefixed pattern
excludes, and an exclude always wins whatever the order, so ["**", "!docs"]
checks everything but docs.
Expand All @@ -75,14 +86,16 @@ type Mypy {
) {
self.pythonVersion = version
self.base = base
self.gitToken = gitToken
self.gitHost = gitHost
self.scope = scope
self.defaultVersion = defaultVersion
self.args = args
self
}

let lib: Pyproject! {
pyproject(markers: markers, version: pythonVersion, base: base)
pyproject(markers: markers, version: pythonVersion, base: base, gitToken: gitToken, gitHost: gitHost)
}

"""
Expand Down
32 changes: 31 additions & 1 deletion pyproject/main.dang
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ type Pyproject {
Python interpreter and uv on PATH. Mutually exclusive with version.
"""
base: Container = null,
"""
HTTPS token git presents to gitHost, for a dependency sourced from a
private git repository, such as a GitHub App installation token.
"""
gitToken: Secret = null,
"""
Host gitToken is presented to, and to no other.
"""
gitHost: String! = "github.com",
) {
if (markers.isEmpty) {
raise "markers must not be empty: the caller decides what a project is"
Expand All @@ -97,7 +106,7 @@ type Pyproject {
self.exclude = exclude
self.includeExtraFiles = includeExtraFiles
self.version = if (base == null) { version ?? defaultVersion } else { null }
self.base = base ?? defaultBase(version ?? defaultVersion)
self.base = withGitToken(base ?? defaultBase(version ?? defaultVersion), gitToken, gitHost)
self
}

Expand Down Expand Up @@ -139,6 +148,27 @@ type Pyproject {
.withDirectory("/usr/local/bin", uvBinary, include: ["uv", "uvx"])
}

"""
`ctr`, with git answering `host`'s credential prompts with `token`.

uv fetches a git dependency with the git CLI and passes its environment on,
so a credential helper declared through GIT_CONFIG_* (git 2.31+) reaches it
with no config file written. The helper reads the token from a secret
variable at fetch time: it never appears in a layer, a URL, or uv.lock.
"""
let withGitToken(ctr: Container!, token: Secret, host: String!): Container! {
if (token == null) { ctr } else {
ctr
.withSecretVariable(name: "PYPROJECT_GIT_TOKEN", secret: token)
.withEnvVariable("GIT_CONFIG_COUNT", "1")
.withEnvVariable("GIT_CONFIG_KEY_0", "credential.https://" + host + ".helper")
.withEnvVariable(
"GIT_CONFIG_VALUE_0",
`!f() { test "$1" = get && echo username=x-access-token && echo "password=$PYPROJECT_GIT_TOKEN"; }; f`,
)
}
}

"""
Projects discovered at or below the workspace cwd, plus the nearest
enclosing project when the cwd is inside one but not a root itself. Paths
Expand Down
15 changes: 14 additions & 1 deletion pytest/main.dang
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ type Pytest {

let pythonVersion: String
let base: Container
let gitToken: Secret
let gitHost: String!

new(
"""
Expand All @@ -91,6 +93,15 @@ type Pytest {
"""
base: Container = null,
"""
HTTPS token git presents to gitHost, for a dependency sourced from a
private git repository, such as a GitHub App installation token.
"""
gitToken: Secret = null,
"""
Host gitToken is presented to, and to no other.
"""
gitHost: String! = "github.com",
"""
Project roots to test. A bare pattern selects, a "!"-prefixed pattern
excludes, and an exclude always wins whatever the order, so ["**", "!docs"]
tests everything but docs.
Expand All @@ -116,6 +127,8 @@ type Pytest {
) {
self.pythonVersion = version
self.base = base
self.gitToken = gitToken
self.gitHost = gitHost
self.scope = scope
self.args = args
self.defaultVersion = defaultVersion
Expand All @@ -125,7 +138,7 @@ type Pytest {
}

let lib: Pyproject! {
pyproject(markers: markers, version: pythonVersion, base: base)
pyproject(markers: markers, version: pythonVersion, base: base, gitToken: gitToken, gitHost: gitHost)
}

"""
Expand Down
15 changes: 14 additions & 1 deletion ty/main.dang
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type Ty {

let pythonVersion: String
let base: Container
let gitToken: Secret
let gitHost: String!

new(
"""
Expand All @@ -61,6 +63,15 @@ type Ty {
"""
base: Container = null,
"""
HTTPS token git presents to gitHost, for a dependency sourced from a
private git repository, such as a GitHub App installation token.
"""
gitToken: Secret = null,
"""
Host gitToken is presented to, and to no other.
"""
gitHost: String! = "github.com",
"""
Project roots to check. A bare pattern selects, a "!"-prefixed pattern
excludes, and an exclude always wins whatever the order, so ["**", "!docs"]
checks everything but docs.
Expand All @@ -77,14 +88,16 @@ type Ty {
) {
self.pythonVersion = version
self.base = base
self.gitToken = gitToken
self.gitHost = gitHost
self.scope = scope
self.defaultVersion = defaultVersion
self.args = args
self
}

let lib: Pyproject! {
pyproject(markers: markers, version: pythonVersion, base: base)
pyproject(markers: markers, version: pythonVersion, base: base, gitToken: gitToken, gitHost: gitHost)
}

"""
Expand Down
15 changes: 14 additions & 1 deletion uv/main.dang
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type Uv {

let pythonVersion: String
let base: Container
let gitToken: Secret
let gitHost: String!

new(
"""
Expand All @@ -60,6 +62,15 @@ type Uv {
"""
base: Container = null,
"""
HTTPS token git presents to gitHost, for a dependency sourced from a
private git repository, such as a GitHub App installation token.
"""
gitToken: Secret = null,
"""
Host gitToken is presented to, and to no other.
"""
gitHost: String! = "github.com",
"""
Project roots to lock. A bare pattern selects, a "!"-prefixed pattern
excludes, and an exclude always wins whatever the order, so ["**", "!docs"]
locks everything but docs.
Expand All @@ -77,14 +88,16 @@ type Uv {
) {
self.pythonVersion = version
self.base = base
self.gitToken = gitToken
self.gitHost = gitHost
self.lock = lock
self.audit = audit
self.auditArgs = auditArgs
self
}

let lib: Pyproject! {
pyproject(markers: markers, version: pythonVersion, base: base)
pyproject(markers: markers, version: pythonVersion, base: base, gitToken: gitToken, gitHost: gitHost)
}

"""
Expand Down