Replacing package example with CloudRun definition

Signed-off-by: Tihomir Jovicic <tihomir.jovicic.develop@gmail.com>
This commit is contained in:
Tihomir Jovicic 2021-07-16 10:14:53 +02:00
parent 216686c7ec
commit 3137e72057

View File

@ -35,45 +35,82 @@ That will create 2 directories: `.dagger` and `cue.mod` where our package will r
### Writing the package ### Writing the package
Now that you've initialized your workspace it's time to write a simple package. Package name usually starts with a Now that you've initialized your workspace it's time to write a simple package. Package name usually starts with a
domain name (as in Go) followed with a descriptive name. In this example we will use my Github domain name and call our domain name (as in Go) followed with a descriptive name. In this example we reuse the Cloud Run example and create a
package `echo` because it will print a single line to stdout. package from it.
```shell ```shell
mkdir -p cue.mod/pkg/github.com/tjovicic/echo mkdir -p cue.mod/pkg/github.com/tjovicic/gcpcloudrun
``` ```
Let's write the package logic. It will spin up an alpine container and print out `Hello` to stdout: Let's write the package logic. It is basically what we've seen in the 106-cloudrun example:
```shell ```shell
touch cue.mod/pkg/github.com/tjovicic/echo/source.cue touch cue.mod/pkg/github.com/tjovicic/gcpcloudrun/source.cue
``` ```
```cue title="cue.mod/pkg/github.com/tjovicic/echo/source.cue" ```cue title="cue.mod/pkg/github.com/tjovicic/gcpcloudrun/source.cue"
package echo package gcpcloudrun
import ( import (
"alpha.dagger.io/alpine" "alpha.dagger.io/dagger"
"alpha.dagger.io/os" "alpha.dagger.io/docker"
"alpha.dagger.io/gcp"
"alpha.dagger.io/gcp/cloudrun"
"alpha.dagger.io/gcp/gcr"
) )
ctr: os.#Container & { #Run: {
image: alpine.#Image & { // Source code of the sample application
package: { src: dagger.#Artifact & dagger.#Input
bash: "=~5.1"
} // GCR full image name
imageRef: string & dagger.#Input
image: docker.#Build & {
source: src
} }
command: "echo Hello" gcpConfig: gcp.#Config
creds: gcr.#Credentials & {
config: gcpConfig
}
push: docker.#Push & {
target: imageRef
source: image
auth: {
username: creds.username
secret: creds.secret
}
}
deploy: cloudrun.#Service & {
config: gcpConfig
image: push.ref
}
} }
``` ```
### Running the package ### Running the package
Now that you've successfully created a package, let's run it in a new environment. Create a new environment using the Now that you've successfully created a package, let's run it in a new environment. Create a new test package using
new `echo` package. our reusable `gcpcloudrun`:
```shell ```shell
dagger new staging -p github.com/tjovicic/echo mkdir test
cat > test/source.cue << EOF
package test
import (
"github.com/tjovicic/gcpcloudrun"
)
run: gcpcloudrun.#Run
EOF
dagger new staging -p ./test
``` ```
Run it: Run it:
@ -82,26 +119,16 @@ Run it:
dagger up -e staging dagger up -e staging
``` ```
At the end of the output you should see `Hello` printed out: You should see a familiar output:
```shell ```shell
11:09AM INF system | starting buildkit version=v0.8.3 9:32AM ERR system | required input is missing input=run.src
WARN[0003] commandConn.CloseWrite: commandconn: failed to wait: signal: terminated 9:32AM ERR system | required input is missing input=run.imageRef
WARN[0003] commandConn.CloseRead: commandconn: failed to wait: signal: terminated 9:32AM ERR system | required input is missing input=run.gcpConfig.region
WARN[0003] commandConn.CloseWrite: commandconn: failed to wait: signal: terminated 9:32AM ERR system | required input is missing input=run.gcpConfig.project
11:09AM INF ctr | computing 9:32AM ERR system | required input is missing input=run.gcpConfig.serviceKey
11:09AM INF ctr.#up[0].from | #5 0.080 fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/main/x86_64/APKINDEX.tar.gz 9:32AM ERR system | required input is missing input=run.deploy.name
11:09AM INF ctr.#up[0].from | #5 0.612 fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/community/x86_64/APKINDEX.tar.gz 9:32AM FTL system | some required inputs are not set, please re-run with `--force` if you think it's a mistake missing=0s
11:09AM INF ctr.#up[0].from | #5 1.094 (1/4) Installing ncurses-terminfo-base (6.2_p20210109-r0)
11:09AM INF ctr.#up[0].from | #5 1.149 (2/4) Installing ncurses-libs (6.2_p20210109-r0)
11:09AM INF ctr.#up[0].from | #5 1.273 (3/4) Installing readline (8.1.0-r0)
11:09AM INF ctr.#up[0].from | #5 1.361 (4/4) Installing bash (5.1.0-r0)
11:09AM INF ctr.#up[0].from | #5 1.534 Executing bash-5.1.0-r0.post-install
11:09AM INF ctr.#up[0].from | #5 1.541 Executing busybox-1.32.1-r6.trigger
11:09AM INF ctr.#up[0].from | #5 1.554 OK: 8 MiB in 18 packages
11:09AM INF ctr | #6 0.110 Hello
11:09AM INF ctr | completed duration=5.4s
Output Value Description
``` ```
## Manually distributing packages ## Manually distributing packages
@ -110,8 +137,8 @@ You've probably guessed this package isn't tied to just your workspace. You can
of different workspaces and use it as we've showed above. of different workspaces and use it as we've showed above.
```shell ```shell
mkdir -p /my-new-workspace/cue.mod/pkg/github.com/tjovicic/echo mkdir -p /my-new-workspace/cue.mod/pkg/github.com/tjovicic/gcpcloudrun
cp ./cue.mod/pkg/github.com/tjovicic/echo/source.cue /my-new-workspace/cue.mod/pkg/github.com/tjovicic/echo cp ./cue.mod/pkg/github.com/tjovicic/gcpcloudrun/source.cue /new-workspace/cue.mod/pkg/github.com/tjovicic/gcpcloudrun
``` ```
## Contributing to Dagger stdlib ## Contributing to Dagger stdlib