Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple environments for Github deployments. #681

Merged
merged 1 commit into from
Jan 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/empire/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
FlagGithubApiURL = "github.api.url"

FlagGithubWebhooksSecret = "github.webhooks.secret"
FlagGithubDeploymentsEnvironment = "github.deployments.environment"
FlagGithubDeploymentsEnvironments = "github.deployments.environment"
FlagGithubDeploymentsImageTemplate = "github.deployments.template"
FlagGithubDeploymentsTugboatURL = "github.deployments.tugboat.url"

Expand Down Expand Up @@ -105,9 +105,9 @@ var Commands = []cli.Command{
EnvVar: "EMPIRE_GITHUB_WEBHOOKS_SECRET",
},
cli.StringFlag{
Name: FlagGithubDeploymentsEnvironment,
Name: FlagGithubDeploymentsEnvironments,
Value: "",
Usage: "If provided, only github deployments to the specified environment will be handled.",
Usage: "If provided, only github deployments to the specified environments will be handled.",
EnvVar: "EMPIRE_GITHUB_DEPLOYMENTS_ENVIRONMENT",
},
cli.StringFlag{
Expand Down
6 changes: 3 additions & 3 deletions cmd/empire/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"fmt"
"log"
"net/http"
"strings"
"time"

"golang.org/x/oauth2"

"github.com/codegangsta/cli"
"github.com/remind101/empire"
"github.com/remind101/empire/server"
"github.com/remind101/empire/server/auth"
"github.com/remind101/empire/server/auth/github"
"golang.org/x/oauth2"
)

func runServer(c *cli.Context) {
Expand All @@ -36,7 +36,7 @@ func newServer(c *cli.Context, e *empire.Empire) http.Handler {
var opts server.Options
opts.Authenticator = newAuthenticator(c, e)
opts.GitHub.Webhooks.Secret = c.String(FlagGithubWebhooksSecret)
opts.GitHub.Deployments.Environment = c.String(FlagGithubDeploymentsEnvironment)
opts.GitHub.Deployments.Environments = strings.Split(c.String(FlagGithubDeploymentsEnvironments), ",")
opts.GitHub.Deployments.ImageTemplate = c.String(FlagGithubDeploymentsImageTemplate)
opts.GitHub.Deployments.TugboatURL = c.String(FlagGithubDeploymentsTugboatURL)

Expand Down
2 changes: 1 addition & 1 deletion empiretest/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func NewServer(t testing.TB, e *empire.Empire) *httptest.Server {
}

server.DefaultOptions.GitHub.Webhooks.Secret = "abcd"
server.DefaultOptions.GitHub.Deployments.Environment = "test"
server.DefaultOptions.Authenticator = auth.Anyone(&empire.User{Name: "fake"})
server.DefaultOptions.GitHub.Deployments.Environments = []string{"test"}
return httptest.NewServer(server.New(e, server.DefaultOptions))
}

Expand Down
17 changes: 13 additions & 4 deletions server/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Options struct {

// If provided, specifies the environments that this Empire instance
// should handle deployments for.
Environment string
Environments []string

// ImageTemplate is used to determine the image to deploy.
ImageTemplate string
Expand All @@ -40,7 +40,7 @@ func New(e *empire.Empire, opts Options) httpx.Handler {

d := newDeployer(e, opts)
secret := opts.Secret
r.Handle("deployment", hookshot.Authorize(&DeploymentHandler{deployer: d, environment: opts.Environment}, secret))
r.Handle("deployment", hookshot.Authorize(&DeploymentHandler{deployer: d, environments: opts.Environments}, secret))
r.Handle("ping", hookshot.Authorize(http.HandlerFunc(Ping), secret))

return r
Expand All @@ -49,7 +49,7 @@ func New(e *empire.Empire, opts Options) httpx.Handler {
// Deployment is an http.Handler for handling the `deployment` event.
type DeploymentHandler struct {
deployer
environment string
environments []string
}

func (h *DeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand All @@ -64,7 +64,7 @@ func (h *DeploymentHandler) ServeHTTPContext(ctx context.Context, w http.Respons
return nil
}

if p.Deployment.Environment != h.environment {
if !currentEnvironment(p.Deployment.Environment, h.environments) {
w.WriteHeader(http.StatusNoContent)
fmt.Fprintf(w, "Ignore deployment to environment: %s", p.Deployment.Environment)
return nil
Expand All @@ -79,6 +79,15 @@ func (h *DeploymentHandler) ServeHTTPContext(ctx context.Context, w http.Respons
return nil
}

func currentEnvironment(eventEnv string, environments []string) bool {
for _, env := range environments {
if env == eventEnv {
return true
}
}
return false
}

func Ping(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Ok\n")
}
Expand Down
4 changes: 2 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Options struct {
Secret string
}
Deployments struct {
Environment string
Environments []string
ImageTemplate string
TugboatURL string
}
Expand All @@ -44,7 +44,7 @@ func New(e *empire.Empire, options Options) http.Handler {
// Mount GitHub webhooks
g := github.New(e, github.Options{
Secret: options.GitHub.Webhooks.Secret,
Environment: options.GitHub.Deployments.Environment,
Environments: options.GitHub.Deployments.Environments,
ImageTemplate: options.GitHub.Deployments.ImageTemplate,
TugboatURL: options.GitHub.Deployments.TugboatURL,
})
Expand Down