mirror of
https://github.com/coder/coder.git
synced 2026-10-16 14:48:56 +08:00
feat: expose the agent's workspace build ID as CODER_WORKSPACE_BUILD_ID
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Generated
+770
-757
File diff suppressed because it is too large
Load Diff
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user