feat: expose the agent's workspace build ID as CODER_WORKSPACE_BUILD_ID

This commit is contained in:
Steven Masley
2026-10-15 21:25:14 +00:00
parent 5261723186
commit bf1dba164b
8 changed files with 811 additions and 759 deletions
+5
View File
@@ -1553,6 +1553,11 @@ func (a *agent) updateCommandEnv(current []string) (updated []string, err error)
envs["VSCODE_PROXY_URI"] = manifest.VSCodePortProxyURI
}
// Older servers do not send the build ID.
if manifest.WorkspaceBuildID != uuid.Nil {
envs["CODER_WORKSPACE_BUILD_ID"] = manifest.WorkspaceBuildID.String()
}
// Allow any of the current env to override what we defined above.
for _, env := range current {
parts := strings.SplitN(env, "=", 2)
+15
View File
@@ -1793,6 +1793,21 @@ func TestAgent_CoderEnvVars(t *testing.T) {
}
}
func TestAgent_CoderWorkspaceBuildIDEnvVar(t *testing.T) {
t.Parallel()
command := "sh -c 'echo $CODER_WORKSPACE_BUILD_ID'"
if runtime.GOOS == "windows" {
command = "cmd.exe /c echo %CODER_WORKSPACE_BUILD_ID%"
}
buildID := uuid.New()
session := setupSSHSession(t, agentsdk.Manifest{WorkspaceBuildID: buildID}, codersdk.ServiceBannerConfig{}, nil)
output, err := session.Output(command)
require.NoError(t, err)
require.Equal(t, buildID.String(), strings.TrimSpace(string(output)))
}
func TestAgent_SSHConnectionEnvVars(t *testing.T) {
t.Parallel()
+770 -757
View File
File diff suppressed because it is too large Load Diff
+3
View File
@@ -124,6 +124,9 @@ message Manifest {
repeated WorkspaceAgentMetadata.Description metadata = 12;
repeated WorkspaceAgentDevcontainer devcontainers = 17;
repeated WorkspaceSecret secrets = 19;
// The workspace build that created this agent. Empty when the server
// predates this field.
bytes workspace_build_id = 20;
}
// WorkspaceSecret is a secret included in the agent manifest
+1
View File
@@ -153,6 +153,7 @@ func (a *ManifestAPI) GetManifest(ctx context.Context, _ *agentproto.GetManifest
AgentName: workspaceAgent.Name,
OwnerUsername: workspace.OwnerUsername,
WorkspaceId: workspace.ID[:],
WorkspaceBuildId: a.WorkspaceBuildID[:],
WorkspaceName: workspace.Name,
GitAuthConfigs: gitAuthConfigs,
EnvironmentVariables: envs,
+3
View File
@@ -371,6 +371,7 @@ func TestGetManifest(t *testing.T) {
ParentId: nil,
OwnerUsername: owner.Username,
WorkspaceId: workspace.ID[:],
WorkspaceBuildId: agentBuild.ID[:],
WorkspaceName: workspace.Name,
GitAuthConfigs: 2, // two "enhanced" external auth configs
EnvironmentVariables: expectedEnvVars,
@@ -440,6 +441,7 @@ func TestGetManifest(t *testing.T) {
ParentId: agent.ID[:],
OwnerUsername: owner.Username,
WorkspaceId: workspace.ID[:],
WorkspaceBuildId: agentBuild.ID[:],
WorkspaceName: workspace.Name,
GitAuthConfigs: 2, // two "enhanced" external auth configs
EnvironmentVariables: nil,
@@ -748,6 +750,7 @@ func TestGetManifest(t *testing.T) {
AgentName: agent.Name,
OwnerUsername: owner.Username,
WorkspaceId: workspace.ID[:],
WorkspaceBuildId: agentBuild.ID[:],
WorkspaceName: workspace.Name,
GitAuthConfigs: 2, // two "enhanced" external auth configs
EnvironmentVariables: expectedEnvVars,
+3
View File
@@ -120,6 +120,9 @@ type Manifest struct {
OwnerName string `json:"owner_name"`
WorkspaceID uuid.UUID `json:"workspace_id"`
WorkspaceName string `json:"workspace_name"`
// WorkspaceBuildID is the build that created this agent. It is
// uuid.Nil when the server predates the field.
WorkspaceBuildID uuid.UUID `json:"workspace_build_id"`
// GitAuthConfigs stores the number of Git configurations
// the Coder deployment has. If this number is >0, we
// set up special configuration in the workspace.
+11 -2
View File
@@ -44,6 +44,13 @@ func ManifestFromProto(manifest *proto.Manifest) (Manifest, error) {
if err != nil {
return Manifest{}, xerrors.Errorf("error converting workspace ID: %w", err)
}
workspaceBuildID := uuid.Nil
if bid := manifest.GetWorkspaceBuildId(); len(bid) > 0 {
workspaceBuildID, err = uuid.FromBytes(bid)
if err != nil {
return Manifest{}, xerrors.Errorf("error converting workspace build ID: %w", err)
}
}
devcontainers, err := DevcontainersFromProto(manifest.Devcontainers)
if err != nil {
return Manifest{}, xerrors.Errorf("error converting workspace agent devcontainers: %w", err)
@@ -55,6 +62,7 @@ func ManifestFromProto(manifest *proto.Manifest) (Manifest, error) {
OwnerName: manifest.OwnerUsername,
WorkspaceID: workspaceID,
WorkspaceName: manifest.WorkspaceName,
WorkspaceBuildID: workspaceBuildID,
Apps: apps,
Scripts: scripts,
DERPMap: tailnet.DERPMapFromProto(manifest.DerpMap),
@@ -87,8 +95,9 @@ func ProtoFromManifest(manifest Manifest) (*proto.Manifest, error) {
AgentId: manifest.AgentID[:],
AgentName: manifest.AgentName,
OwnerUsername: manifest.OwnerName,
WorkspaceId: manifest.WorkspaceID[:],
WorkspaceName: manifest.WorkspaceName,
WorkspaceId: manifest.WorkspaceID[:],
WorkspaceName: manifest.WorkspaceName,
WorkspaceBuildId: manifest.WorkspaceBuildID[:],
// #nosec G115 - Safe conversion for GitAuthConfigs which is expected to be small and positive
GitAuthConfigs: uint32(manifest.GitAuthConfigs),
EnvironmentVariables: manifest.EnvironmentVariables,