-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cue
197 lines (186 loc) · 6.49 KB
/
main.cue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"dagger.io/dagger"
"universe.dagger.io/alpine"
"universe.dagger.io/docker"
"universe.dagger.io/docker/cli"
"universe.dagger.io/go"
)
dagger.#Plan & {
// Declare client for multiple usecases
client: {
filesystem: "./": read: {
contents: dagger.#FS
}
network: "unix:///var/run/docker.sock": connect: {
dagger.#Socket
}
env: {
REPOSITORY: string
GITHUB_REF: string
GITHUB_ACTOR: string
DOCKER_REPO: string
DOCKER_USER: string
DOCKER_SECRET: dagger.#Secret
COSIGN_PASSWORD: dagger.#Secret
COSIGN_PRIVATE_KEY: dagger.#Secret
}
commands: {
version: {
name: "bash"
args: ["-c", #"""
if [[ \#(client.env.GITHUB_REF) =~ "refs/tags/" ]]; then echo \#(client.env.GITHUB_REF) | sed -e 's/^refs\/tags\/v//' | tr -d "[:space:]"
elif [[ \#(client.env.GITHUB_REF) =~ "refs/heads/" ]]; then echo \#(client.env.GITHUB_REF) | sed -e 's/^refs\/heads\///' | tr -d "[:space:]"
fi
"""#]
stdout: string
}
}
}
actions: {
// Build app in a Golang container
_build: go.#Build & {
source: client.filesystem."./".read.contents
}
// Ugly way to set the password env
_cosign: alpine.#Build & {
packages: {
"bash": _
}
}
_secret: docker.#Run & {
input: _cosign.output
mounts: secret: {
dest: "/run/cosign.key"
contents: client.env.COSIGN_PRIVATE_KEY
}
command: {
name: "cp"
args: ["/run/cosign.key", "/cosign.key"]
}
export: directories: {
"/cosign.key": _
}
}
_password: docker.#Run & {
input: _cosign.output
mounts: secret: {
dest: "/run/cosign.passwd"
contents: client.env.COSIGN_PASSWORD
}
command: {
name: "cp"
args: ["/run/cosign.passwd", "/cosign.passwd"]
}
export: directories: {
"/cosign.passwd": _
}
}
// Build lighter image
run: docker.#Build & {
steps: [
alpine.#Build & {
packages: {
"gcc": _
"libc-dev": _
"ca-certificates": _
}
},
// Update certificates
docker.#Run & {
command: {
name: "update-ca-certificates"
}
},
// Copy from build to run
docker.#Copy & {
contents: _build.output
dest: "/app"
},
// Copy config to container
docker.#Copy & {
contents: client.filesystem."./".read.contents
include: ["config.json"]
dest: "/"
},
// Run binary at the end
docker.#Set & {
config: cmd: ["./app/\(client.env.REPOSITORY)"]
}
]
}
// Push image to remote registry
push: {
docker.#Push & {
"image": run.output
dest: "\(client.env.DOCKER_USER)/\(client.env.REPOSITORY):\(client.commands.version.stdout)"
auth: {
username: client.env.DOCKER_USER
secret: client.env.DOCKER_SECRET
}
}
}
// Sign the image with cosign
sign: docker.#Build & {
steps: [
alpine.#Build & {
packages: {
"gcc": _
"libc-dev": _
"ca-certificates": _
}
},
// Update certificates
docker.#Run & {
command: {
name: "update-ca-certificates"
}
},
// Copy cosign password
docker.#Copy & {
contents: _password.export.directories."/cosign.passwd"
dest: "/"
},
// Copy cosign key
docker.#Copy & {
contents: _secret.export.directories."/cosign.key"
dest: "/"
},
// Copy config to container
docker.#Copy & {
contents: client.filesystem."./".read.contents
include: [ "dockerconfig.json"]
dest: "/"
},
// Sign with cosign
docker.#Run & {
env: IMAGE_ID: "\(client.env.DOCKER_REPO)/\(client.env.DOCKER_USER)/\(client.env.REPOSITORY)"
env: VERSION: client.commands.version.stdout
env: REPOSITORY: client.env.REPOSITORY
env: DEVELOPER: client.env.GITHUB_ACTOR
env: DOCKER_USER: client.env.DOCKER_USER
env: DOCKER_SECRET: client.env.DOCKER_SECRET
env: COSIGN_PASSWORD: client.env.COSIGN_PASSWORD
command: {
name: "sh"
args: ["-c", #"""
echo Setting up Cosign
export COSIGN_PASSWORD=$(cat /cosign.passwd)
wget -q https://github.com/sigstore/cosign/releases/download/v1.6.0/cosign-linux-amd64
mv cosign-linux-amd64 /usr/local/bin/cosign && chmod +x /usr/local/bin/cosign
mkdir /root/.docker && mv dockerconfig.json /root/.docker/config.json
sed -i 's/\"_auth_\"/'\"$(echo -n $DOCKER_USER:$DOCKER_SECRET | base64)\"'/g' /root/.docker/config.json
cosign sign --key cosign.key -a REPO=$REPOSITORY -a TAG=$VERSION -a SIGNER=GitHub -a DEVELOPER=$DEVELOPER -a TIMESTAMP=$(date +'%Y-%m-%dT%H:%M:%S:%z') $IMAGE_ID:$VERSION
"""#]
}
}
]
}
// Create a docker image localy
load: cli.#Load & {
image: run.output
tag: client.env.REPOSITORY
host: client.network."unix:///var/run/docker.sock".connect
}
}
}