Repository navigation
Expand file tree
/
Copy pathcloudbuild.yaml
More file actions
129 lines (122 loc) · 5.49 KB
/
Copy pathcloudbuild.yaml
File metadata and controls
129 lines (122 loc) · 5.49 KB
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
# Release agents: build their images and deploy them to one environment.
#
# Two triggers (webservices-infra, hackbot/tf/modules/resources/build.tf) run
# this, telling it which environment in _ENV:
# stage-agents a push to the default branch touching an agent's VERSION
# file. Releases every agent whose version isn't
# the one stage runs. Run by hand, it releases every agent
# regardless of version, or those in _AGENTS=a,b.
# dev-agents a push to dev/agents/<agent> or dev/agents/<agent>/<anything>.
# Releases that agent to dev, always; dev/agents/all releases
# every agent.
# Prod is a separate, approved promotion; nothing here reaches it.
#
# `release-plan` decides what to release and `release` deploys it, one agent
# after another: one failing doesn't stop the rest, and the build fails at the
# end, naming them.
#
# Cloud Build reads every `$NAME` as a substitution, and MUST_MATCH fails the
# build on unknown ones, so shell variables below are written `$$name`.
steps:
# Builds of one trigger deploy to one environment: take turns. The wait
# allows for an older build's own work; the timeout is that plus this one's.
- id: Wait
name: ghcr.io/astral-sh/uv:python3.14-bookworm-slim
entrypoint: bash
args:
- -c
- uv run --frozen --package hackbot-deploy python -m hackbot_deploy wait-turn "$BUILD_ID" --project "$PROJECT_ID" --location "$LOCATION" --timeout 7200
# /workspace/plan.txt: one "name version previous-commit config-hash broker"
# line per agent to release.
- id: Plan
name: ghcr.io/astral-sh/uv:python3.14-bookworm-slim
entrypoint: bash
# The tool reads these from the environment: in a command, a run by hand's
# unresolved pusher ("$(push.pusher.name)") would run as a command.
env:
- BRANCH_NAME=$BRANCH_NAME
- AGENTS=${_AGENTS}
- PUSHER=${_PUSHER}
args:
- -c
- uv run --frozen --package hackbot-deploy python -m hackbot_deploy release-plan --env "${_ENV}" --output /workspace/plan.txt
# A read token for each planned agent whose hackbot.toml [build] table names
# an internal repository, from the build's GitHub connection
# (hackbot_deploy/build_tokens.py), so no GitHub credential of ours is ever in
# the build. One file per agent in the github-tokens volume, which only this
# step and Build mount: never under /workspace, the build context every
# agent's Dockerfile can read (they bind-mount it to install dependencies).
- id: Tokens
name: ghcr.io/astral-sh/uv:python3.14-bookworm-slim
entrypoint: bash
env:
- PROJECT_ID=$PROJECT_ID
- LOCATION=$LOCATION
volumes:
- name: github-tokens
path: /github-tokens
args:
- -c
- uv run --frozen --package hackbot-deploy python -m hackbot_deploy build-tokens --plan /workspace/plan.txt --output /github-tokens
# Every image carries the shape it was built for (hackbot.config_hash) and
# its version. An agent whose images don't build gets no digest files, and
# `release` reports it. An agent with a token gets it as the `github_token`
# BuildKit secret; the tokens are removed once the images are built.
- id: Build
name: gcr.io/cloud-builders/docker
entrypoint: bash
env:
- DOCKER_BUILDKIT=1
volumes:
- name: github-tokens
path: /github-tokens
args:
- -c
- |
set -uo pipefail
trap 'rm -f /github-tokens/*' EXIT
while read -r name version _ config_hash broker; do
agent="$${name#agents/}"
targets=agent
if [ "$$broker" = broker ]; then targets="agent broker"; fi
secret=()
token="/github-tokens/$$agent"
if [ -f "$$token" ]; then secret=(--secret "id=github_token,src=$$token"); fi
for target in $$targets; do
image="${_AR_HOSTNAME}/${_AR_PROJECT_ID}/${_AR_REPOSITORY}/$$agent-$$target:$COMMIT_SHA"
if ! { docker build -f "agents/$$agent/Dockerfile" --target "$$target" "$${secret[@]}" \
--label "hackbot.config_hash=$$config_hash" \
--label "org.opencontainers.image.version=$$version" \
--label "org.opencontainers.image.revision=$COMMIT_SHA" \
--label "org.opencontainers.image.created=$$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
-t "$$image" . && docker push "$$image"; } < /dev/null; then
rm -f "/workspace/$$agent-agent.digest"
break
fi
docker inspect --format='{{index .RepoDigests 0}}' "$$image" > "/workspace/$$agent-$$target.digest"
done
done < /workspace/plan.txt
- id: Deploy
name: ghcr.io/astral-sh/uv:python3.14-bookworm-slim
entrypoint: bash
# Recorded as deployed_by: whoever pushed, or the build when the trigger
# is run by hand. Substitutions aren't in a step's environment otherwise.
env:
- GITHUB_ACTOR=${_PUSHER}
- BUILD_ID=$BUILD_ID
args:
- -c
- uv run --frozen --package hackbot-deploy python -m hackbot_deploy release --env "${_ENV}" --plan /workspace/plan.txt --digests /workspace
# _ENV is set by each trigger; _AGENTS only when run by hand; _PUSHER from the
# push event, empty when run any other way.
substitutions:
_ENV: stage
_AGENTS: ""
_PUSHER: ""
timeout: 14400s
options:
substitutionOption: MUST_MATCH
logging: CLOUD_LOGGING_ONLY
# Agent images are large, and one build can release several of them.
machineType: E2_HIGHCPU_8
diskSizeGb: 200