-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeploy_github.go
More file actions
203 lines (174 loc) · 5.8 KB
/
deploy_github.go
File metadata and controls
203 lines (174 loc) · 5.8 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
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
198
199
200
201
202
203
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/codesphere-cloud/cs-go/pkg/deploy"
"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/spf13/cobra"
)
type DeployGitHubCmd struct {
cmd *cobra.Command
Opts DeployGitHubOpts
}
type DeployGitHubOpts struct {
GlobalOptions
PlanId *int
Env *[]string
VpnConfig *string
Branch *string
Stages *string
Timeout *time.Duration
Profile *string
}
func (c *DeployGitHubCmd) RunE(_ *cobra.Command, args []string) error {
client, err := NewClient(c.Opts.GlobalOptions)
if err != nil {
return fmt.Errorf("failed to create Codesphere client: %w", err)
}
teamId, err := c.Opts.GetTeamId()
if err != nil {
return fmt.Errorf("failed to get team ID: %w", err)
}
// Load GitHub context
eventName := os.Getenv("GITHUB_EVENT_NAME")
prAction, prNumber := loadGitHubEvent()
repository := os.Getenv("GITHUB_REPOSITORY")
serverUrl := os.Getenv("GITHUB_SERVER_URL")
// Determine workspace name: <repo>-#<pr>
parts := strings.Split(repository, "/")
repo := parts[len(parts)-1]
wsName := fmt.Sprintf("%s-#%s", repo, prNumber)
// Resolve branch
branch := c.resolveBranch()
// Resolve repo URL
repoUrl := fmt.Sprintf("%s/%s.git", serverUrl, repository)
// Parse stages
var stages []string
for _, s := range strings.Fields(*c.Opts.Stages) {
if s != "" {
stages = append(stages, s)
}
}
// Parse env vars
envVars := make(map[string]string)
for _, e := range *c.Opts.Env {
if idx := strings.Index(e, "="); idx > 0 {
envVars[e[:idx]] = e[idx+1:]
}
}
cfg := deploy.Config{
TeamId: teamId,
PlanId: *c.Opts.PlanId,
Name: wsName,
EnvVars: envVars,
VpnConfig: *c.Opts.VpnConfig,
Branch: branch,
Stages: stages,
RepoUrl: repoUrl,
Timeout: *c.Opts.Timeout,
Profile: *c.Opts.Profile,
ApiUrl: c.Opts.GetApiUrl(),
}
// Determine if this is a delete operation
isDelete := eventName == "pull_request" && prAction == "closed"
deployer := deploy.NewDeployer(client)
result, err := deployer.Deploy(cfg, isDelete)
if err != nil {
return err
}
// Write GitHub-specific outputs
if result != nil {
setGitHubOutputs(result.WorkspaceId, result.WorkspaceURL)
}
return nil
}
// resolveBranch determines the branch to deploy with priority:
// flag > GITHUB_HEAD_REF > GITHUB_REF_NAME > "main"
func (c *DeployGitHubCmd) resolveBranch() string {
if c.Opts.Branch != nil && *c.Opts.Branch != "" {
return *c.Opts.Branch
}
if headRef := os.Getenv("GITHUB_HEAD_REF"); headRef != "" {
return headRef
}
if refName := os.Getenv("GITHUB_REF_NAME"); refName != "" {
return refName
}
return "main"
}
// loadGitHubEvent reads the PR action and number from GITHUB_EVENT_PATH.
func loadGitHubEvent() (action string, number string) {
path := os.Getenv("GITHUB_EVENT_PATH")
if path == "" {
return "", ""
}
data, err := os.ReadFile(path)
if err != nil {
return "", ""
}
var event struct {
Action string `json:"action"`
Number int `json:"number"`
}
if json.Unmarshal(data, &event) == nil {
return event.Action, strconv.Itoa(event.Number)
}
return "", ""
}
// setGitHubOutputs writes deployment results to GitHub Actions output files.
func setGitHubOutputs(wsId int, url string) {
if f := os.Getenv("GITHUB_OUTPUT"); f != "" {
appendToFile(f, fmt.Sprintf("deployment-url=%s\nworkspace-id=%d\n", url, wsId))
}
if f := os.Getenv("GITHUB_STEP_SUMMARY"); f != "" {
appendToFile(f, fmt.Sprintf(
"### 🚀 Codesphere Deployment\n\n| Property | Value |\n|----------|-------|\n| **URL** | [%s](%s) |\n| **Workspace** | `%d` |\n",
url, url, wsId,
))
}
}
func appendToFile(path, content string) {
f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return
}
defer f.Close() //nolint:errcheck // best-effort append
_, _ = f.WriteString(content)
}
func AddDeployGitHubCmd(deployCmd *cobra.Command, opts GlobalOptions) {
github := DeployGitHubCmd{
cmd: &cobra.Command{
Use: "github",
Short: "Deploy from GitHub Actions",
Long: io.Long(`Deploy workspaces from GitHub Actions.
Automatically detects the PR context from GitHub Actions environment
variables (GITHUB_EVENT_NAME, GITHUB_HEAD_REF, GITHUB_REPOSITORY, etc.)
and creates, updates, or deletes workspaces accordingly.
On PR open/synchronize: creates or updates a workspace.
On PR close: deletes the workspace.
Designed to be used from GitHub Actions workflows.`),
Example: io.FormatExampleCommands("deploy github", []io.Example{
{Cmd: "", Desc: "Deploy using GitHub Actions environment variables"},
{Cmd: "--plan-id 20", Desc: "Deploy with a specific plan"},
{Cmd: "--stages 'prepare test run'", Desc: "Deploy and run specific pipeline stages"},
{Cmd: "--branch feature-x", Desc: "Override the branch to deploy"},
}),
},
Opts: DeployGitHubOpts{GlobalOptions: opts},
}
github.Opts.PlanId = github.cmd.Flags().Int("plan-id", 8, "Plan ID for the workspace")
github.Opts.Env = github.cmd.Flags().StringArray("env", []string{}, "Environment variables in KEY=VALUE format")
github.Opts.VpnConfig = github.cmd.Flags().String("vpn-config", "", "VPN config name to connect the workspace to")
github.Opts.Branch = github.cmd.Flags().StringP("branch", "b", "", "Git branch to deploy (auto-detected from GitHub context if not set)")
github.Opts.Stages = github.cmd.Flags().String("stages", "prepare run", "Pipeline stages to run (space-separated: prepare test run)")
github.Opts.Timeout = github.cmd.Flags().Duration("timeout", 5*time.Minute, "Timeout for workspace creation/readiness")
github.Opts.Profile = github.cmd.Flags().StringP("profile", "p", "", "CI profile to use (e.g. 'prod' for ci.prod.yml), defaults to ci.yml")
deployCmd.AddCommand(github.cmd)
github.cmd.RunE = github.RunE
}