2021-06-15 10:46:56 +02:00
|
|
|
// Docker container operations
|
2021-04-06 23:20:21 +02:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2021-06-23 16:31:42 +02:00
|
|
|
"alpha.dagger.io/dagger"
|
2021-06-12 17:11:27 +02:00
|
|
|
"alpha.dagger.io/alpine"
|
2021-06-23 16:31:42 +02:00
|
|
|
"alpha.dagger.io/dagger/op"
|
2021-04-06 23:20:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Build a Docker image from source, using included Dockerfile
|
2021-05-01 09:14:36 +02:00
|
|
|
#Build: {
|
2021-05-26 12:23:44 +02:00
|
|
|
source: dagger.#Artifact @dagger(input)
|
2021-04-06 23:20:21 +02:00
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#DockerBuild & {
|
|
|
|
context: source
|
|
|
|
},
|
|
|
|
]
|
2021-05-01 09:14:36 +02:00
|
|
|
|
2021-04-06 23:20:21 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 08:53:04 +02:00
|
|
|
// Pull a docker container
|
|
|
|
#Pull: {
|
|
|
|
// Remote ref (example: "index.docker.io/alpine:latest")
|
2021-05-26 12:23:44 +02:00
|
|
|
from: string @dagger(input)
|
2021-04-06 23:20:21 +02:00
|
|
|
|
|
|
|
#up: [
|
2021-05-06 08:53:04 +02:00
|
|
|
op.#FetchContainer & {ref: from},
|
2021-04-06 23:20:21 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2021-06-12 17:11:41 +02:00
|
|
|
// Push a docker image to a remote registry
|
2021-05-13 18:52:22 +02:00
|
|
|
#Push: {
|
2021-06-12 17:11:27 +02:00
|
|
|
// Remote name (example: "index.docker.io/alpine:latest")
|
|
|
|
name: string @dagger(input)
|
2021-05-13 18:52:22 +02:00
|
|
|
|
2021-06-12 17:11:27 +02:00
|
|
|
// Image source
|
2021-05-26 12:23:44 +02:00
|
|
|
source: dagger.#Artifact @dagger(input)
|
2021-05-13 18:52:22 +02:00
|
|
|
|
2021-06-12 17:11:27 +02:00
|
|
|
// Image registry
|
|
|
|
registry: {
|
|
|
|
// Remote registry
|
2021-06-12 17:11:41 +02:00
|
|
|
target: string | *"https://index.docker.io/v1/" @dagger(input)
|
2021-06-12 17:11:27 +02:00
|
|
|
|
|
|
|
// Username
|
2021-06-12 17:11:41 +02:00
|
|
|
username: string @dagger(input)
|
2021-06-12 17:11:27 +02:00
|
|
|
|
|
|
|
// Password or secret
|
2021-06-12 17:11:41 +02:00
|
|
|
secret: string | bytes @dagger(input)
|
2021-06-12 17:11:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
push: #up: [
|
|
|
|
op.#Load & {from: source},
|
|
|
|
|
|
|
|
if registry != _|_ {
|
|
|
|
op.#DockerLogin & {
|
|
|
|
target: registry.target
|
|
|
|
username: registry.username
|
|
|
|
secret: registry.secret
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
op.#PushContainer & {ref: name},
|
|
|
|
op.#Subdir & {dir: "/dagger"},
|
2021-05-13 18:52:22 +02:00
|
|
|
]
|
2021-06-12 17:11:27 +02:00
|
|
|
|
2021-06-12 17:11:41 +02:00
|
|
|
// Image ref
|
|
|
|
ref: string @dagger(output)
|
|
|
|
|
|
|
|
// Image digest
|
|
|
|
digest: string @dagger(output)
|
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#Load & {from: alpine.#Image & {
|
|
|
|
package: {
|
|
|
|
bash: true
|
|
|
|
jq: true
|
|
|
|
}
|
|
|
|
}},
|
|
|
|
|
|
|
|
op.#Exec & {
|
|
|
|
always: true
|
|
|
|
args: ["/bin/bash", "-c", #"""
|
|
|
|
jq --arg key0 'ref' --arg value0 $(cat /dagger/image_ref) \
|
|
|
|
--arg key1 'digest' --arg value1 $(cat /dagger/image_digest) \
|
|
|
|
'. | .[$key0]=$value0 | .[$key1]=$value1 '<<< '{}' > /out
|
|
|
|
"""#,
|
|
|
|
]
|
|
|
|
mount: "/dagger": from: push
|
|
|
|
},
|
|
|
|
|
|
|
|
op.#Export & {
|
|
|
|
source: "/out"
|
|
|
|
format: "json"
|
|
|
|
},
|
|
|
|
]
|
2021-05-13 18:52:22 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 19:25:44 +02:00
|
|
|
#Run: {
|
2021-06-05 01:57:31 +02:00
|
|
|
// Connect to a remote SSH server
|
2021-06-07 22:29:42 +02:00
|
|
|
ssh: {
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh host
|
|
|
|
host: string @dagger(input)
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh user
|
|
|
|
user: string @dagger(input)
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh port
|
|
|
|
port: *22 | int @dagger(input)
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// private key
|
|
|
|
key: dagger.#Secret @dagger(input)
|
2021-05-21 17:18:30 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// fingerprint
|
|
|
|
fingerprint?: string @dagger(input)
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh key passphrase
|
|
|
|
keyPassphrase?: dagger.#Secret @dagger(input)
|
|
|
|
}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
|
|
|
// Image reference (e.g: nginx:alpine)
|
2021-05-21 17:18:30 +02:00
|
|
|
ref: string @dagger(input)
|
2021-05-20 19:25:44 +02:00
|
|
|
|
|
|
|
// Container name
|
2021-05-21 17:18:30 +02:00
|
|
|
name?: string @dagger(input)
|
2021-05-20 19:25:44 +02:00
|
|
|
|
|
|
|
// Image registry
|
|
|
|
registry?: {
|
2021-05-20 19:26:24 +02:00
|
|
|
target: string
|
2021-05-20 19:25:44 +02:00
|
|
|
username: string
|
|
|
|
secret: dagger.#Secret
|
2021-05-21 17:18:30 +02:00
|
|
|
} @dagger(input)
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-11 19:01:33 +02:00
|
|
|
#command: #"""
|
2021-06-05 01:57:31 +02:00
|
|
|
# Run detach container
|
|
|
|
OPTS=""
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
if [ ! -z "$CONTAINER_NAME" ]; then
|
|
|
|
OPTS="$OPTS --name $CONTAINER_NAME"
|
|
|
|
fi
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
docker container run -d $OPTS "$IMAGE_REF"
|
|
|
|
"""#
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-11 19:01:33 +02:00
|
|
|
run: #Command & {
|
|
|
|
"ssh": ssh
|
|
|
|
command: #command
|
|
|
|
env: {
|
|
|
|
IMAGE_REF: ref
|
|
|
|
if name != _|_ {
|
|
|
|
CONTAINER_NAME: name
|
2021-05-20 19:25:44 +02:00
|
|
|
}
|
2021-06-11 19:01:33 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-20 19:25:44 +02:00
|
|
|
}
|
2021-05-06 08:53:04 +02:00
|
|
|
|
2021-04-06 23:20:21 +02:00
|
|
|
// Build a Docker image from the provided Dockerfile contents
|
2021-05-06 08:53:04 +02:00
|
|
|
// FIXME: incorporate into #Build
|
2021-04-06 23:20:21 +02:00
|
|
|
#ImageFromDockerfile: {
|
2021-06-15 10:46:56 +02:00
|
|
|
// Dockerfile passed as a string
|
2021-06-15 10:47:39 +02:00
|
|
|
dockerfile: string @dagger(input)
|
2021-06-15 10:46:56 +02:00
|
|
|
|
|
|
|
// Build context
|
2021-06-15 10:47:39 +02:00
|
|
|
context: dagger.#Artifact @dagger(input)
|
2021-04-06 23:20:21 +02:00
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#DockerBuild & {
|
|
|
|
"context": context
|
|
|
|
"dockerfile": dockerfile
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|