Skip to content

Commit

Permalink
fix go deployments when missing go.sum
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegn committed Nov 1, 2024
1 parent 6de1bfb commit 64878e3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 13 deletions.
30 changes: 17 additions & 13 deletions scanner/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@ func configureGo(sourceDir string, config *ScannerConfig) (*SourceInfo, error) {
return nil, nil
}

s := &SourceInfo{
Files: templates("templates/go"),
Family: "Go",
Port: 8080,
Env: map[string]string{
"PORT": "8080",
},
Runtime: plan.RuntimeStruct{Language: "go"},
}
vars := make(map[string]interface{})

var skipDeploy bool

if !absFileExists("go.sum") {
s.SkipDeploy = true
terminal.Warn("no go.sum file found, please adjust your Dockerfile to remove references to go.sum")
vars["skipGoSum"] = true
skipDeploy = true
}

gomod, parseErr := parseModfile()
Expand All @@ -38,8 +32,18 @@ func configureGo(sourceDir string, config *ScannerConfig) (*SourceInfo, error) {
version = gomod.Go.Version
}

s.BuildArgs = map[string]string{
"GO_VERSION": version,
s := &SourceInfo{
Files: templatesExecute("templates/go", vars),
Family: "Go",
Port: 8080,
Env: map[string]string{
"PORT": "8080",
},
Runtime: plan.RuntimeStruct{Language: "go", Version: version},
BuildArgs: map[string]string{
"GO_VERSION": version,
},
SkipDeploy: skipDeploy,
}

return s, nil
Expand Down
4 changes: 4 additions & 0 deletions scanner/templates/go/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ ARG GO_VERSION=1
FROM golang:${GO_VERSION}-bookworm as builder

WORKDIR /usr/src/app
{{ if .skipGoSum -}}
COPY go.mod ./
{{ else -}}
COPY go.mod go.sum ./
{{ end -}}
RUN go mod download && go mod verify
COPY . .
RUN go build -v -o /run-app .
Expand Down
25 changes: 25 additions & 0 deletions test/deployer/deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,31 @@ func TestLaunchDjangoBasic(t *testing.T) {
require.Contains(t, string(body), "Hello, world. You're at the polls index.")
}

func TestLaunchGoNoGoSum(t *testing.T) {
deploy := testDeployer(t,
withFixtureApp("go-no-go-sum"),
createRandomApp,
testlib.WithoutCustomize,
testlib.WithouExtensions,
testlib.DeployNow,
withWorkDirAppSource,
testlib.CleanupBeforeExit,
)

manifest, err := deploy.Output().ArtifactManifest()
require.NoError(t, err)
require.NotNil(t, manifest)

require.Equal(t, "go", manifest.Plan.Runtime.Language)
require.Equal(t, "1.22.6", manifest.Plan.Runtime.Version)

appName := deploy.Extra["appName"].(string)

body, err := testlib.RunHealthCheck(fmt.Sprintf("https://%s.fly.dev/", appName))
require.NoError(t, err)
require.Contains(t, string(body), "Hello from Go!")
}

func createRandomApp(d *testlib.DeployTestRun) {
appName := d.CreateRandomAppName()
require.NotEmpty(d, appName)
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/go-no-go-sum/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module example.com/m

go 1.22.6
21 changes: 21 additions & 0 deletions test/fixtures/go-no-go-sum/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"log"
"net/http"
"os"
)

func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"

}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello from Go!"))
})

log.Println("listening on", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}

0 comments on commit 64878e3

Please sign in to comment.