diff --git a/stdlib/gcp/gcr/gcr.cue b/stdlib/gcp/gcr/gcr.cue new file mode 100644 index 00000000..b855a96e --- /dev/null +++ b/stdlib/gcp/gcr/gcr.cue @@ -0,0 +1,44 @@ +package gcr + +import ( + "dagger.io/dagger/op" + "dagger.io/gcp" +) + +// Credentials retriever for GCR +#Credentials: { + // GCP Config + config: gcp.#Config + + // GCR credentials + username: "oauth2accesstoken" + secret: { + string + + #up: [ + op.#Load & { + from: gcp.#GCloud & { + "config": config + } + }, + op.#Exec & { + always: true + args: [ + "/bin/bash", + "--noprofile", + "--norc", + "-eo", + "pipefail", + "-c", + #""" + printf $(gcloud auth print-access-token) > /token.txt + """#, + ] + }, + + op.#Export & { + source: "/token.txt" + }, + ] + } +} diff --git a/tests/stdlib.bats b/tests/stdlib.bats index 564135b2..baf6a169 100644 --- a/tests/stdlib.bats +++ b/tests/stdlib.bats @@ -66,6 +66,12 @@ setup() { "$DAGGER" compute "$TESTDIR"/stdlib/gcp/gke --input-yaml "$TESTDIR"/stdlib/gcp/inputs.yaml } +@test "stdlib: gcp: gcr" { + skip_unless_secrets_available "$TESTDIR"/stdlib/gcp/inputs.yaml + + "$DAGGER" compute "$TESTDIR"/stdlib/gcp/gcr --input-yaml "$TESTDIR"/stdlib/gcp/inputs.yaml +} + @test "stdlib: docker-build" { "$DAGGER" compute "$TESTDIR"/stdlib/docker/build/ --input-dir source="$TESTDIR"/stdlib/docker/build } diff --git a/tests/stdlib/gcp/gcr/gcr.cue b/tests/stdlib/gcp/gcr/gcr.cue new file mode 100644 index 00000000..e33f6547 --- /dev/null +++ b/tests/stdlib/gcp/gcr/gcr.cue @@ -0,0 +1,96 @@ +package gcr + +import ( + "dagger.io/gcp" + "dagger.io/gcp/gcr" + "dagger.io/alpine" + "dagger.io/dagger/op" +) + +TestConfig: gcpConfig: gcp.#Config + +// Generate a random number +random: { + string + #up: [ + op.#Load & {from: alpine.#Image}, + op.#Exec & { + args: ["sh", "-c", "cat /dev/urandom | tr -dc 'a-z' | fold -w 10 | head -n 1 | tr -d '\n' > /rand"] + }, + op.#Export & { + source: "/rand" + }, + ] +} + +TestGCR: { + repository: "gcr.io/dagger-ci/test" + tag: "test-gcr-\(random)" + + creds: gcr.#Credentials & { + config: TestConfig.gcpConfig + } + + push: { + ref: "\(repository):\(tag)" + + #up: [ + op.#DockerBuild & { + dockerfile: """ + FROM alpine + RUN echo \(random) > /test + """ + }, + + op.#DockerLogin & { + target: repository + username: creds.username + secret: creds.secret + }, + + op.#PushContainer & { + "ref": ref + }, + ] + } + + pull: #up: [ + op.#DockerLogin & { + target: push.ref + username: creds.username + secret: creds.secret + }, + + op.#FetchContainer & { + ref: push.ref + }, + ] + + verify: #up: [ + op.#Load & { + from: pull + }, + + op.#Exec & { + always: true + args: [ + "sh", "-c", "test $(cat test) = \(random)", + ] + }, + ] + + verifyBuild: #up: [ + op.#DockerLogin & { + target: push.ref + username: creds.username + secret: creds.secret + }, + + op.#DockerBuild & { + dockerfile: #""" + FROM \#(push.ref) + RUN test $(cat test) = \#(random) + """# + }, + ] +}