dnephin / dobi

A build automation tool for Docker applications
https://dnephin.github.io/dobi/
Apache License 2.0
309 stars 36 forks source link

{git.branch} segfaults for detached HEAD #137

Closed Enteee closed 6 years ago

Enteee commented 6 years ago

dobi version 0.11 (build: 0027fb7, date: Thu Apr 19 03:22:59 UTC 2018)

Using the following dobi.yaml:

env=env/base:
  variables: [
    "TAG={git.branch}",
  ]

image=image/noop:
  depends: [
    "env/base",
  ]
  image: "noop"
  context: "base/noop"
  tags: [
    "{env.TAG}"
  ]

and base/noop/Dockerfile

FROM alpine
CMD "exit"

with:

$ git branch
* (HEAD detached at 592f8fe4)

running dobi image/noop, I get the following segfault:

/dobi/execenv.valueFromGit(0x9784c0, 0xc4203c8c40, 0xc4201d07a9, 0x6, 0x0, 0x0, 0x919a00, 0x1, 0x1)
    /go/src/github.com/dnephin/dobi/execenv/environment.go:155 +0x37c
github.com/dnephin/dobi/execenv.(*ExecEnv).templateContext(0xc42008bf40, 0x9784c0, 0xc4203c8c40, 0xc4201d07a5, 0xa, 0xc4201d07a0, 0x4, 0x4)
    /go/src/github.com/dnephin/dobi/execenv/environment.go:92 +0x705
github.com/dnephin/dobi/execenv.(*ExecEnv).(github.com/dnephin/dobi/execenv.templateContext)-fm(0x9784c0, 0xc4203c8c40, 0xc4201d07a5, 0xa, 0x4, 0x0, 0x0)
    /go/src/github.com/dnephin/dobi/execenv/environment.go:50 +0x52
github.com/dnephin/dobi/vendor/github.com/valyala/fasttemplate.(*Template).ExecuteFunc(0xc4203a1cc0, 0x9784c0, 0xc4203c8c40, 0xc42015d5e8, 0x919a3c, 0x1, 0xc4203a1cc0)
    /go/src/github.com/dnephin/dobi/vendor/github.com/valyala/fasttemplate/template.go:246 +0x156
github.com/dnephin/dobi/execenv.(*ExecEnv).Resolve(0xc42008bf40, 0xc4201d07a0, 0x10, 0x0, 0x1, 0xc42039d840, 0x0)
    /go/src/github.com/dnephin/dobi/execenv/environment.go:50 +0x147
github.com/dnephin/dobi/execenv.(*ExecEnv).ResolveSlice(0xc42008bf40, 0xc42011f700, 0x8, 0x8, 0xbf3750, 0x0, 0x0, 0x0, 0x0)
    /go/src/github.com/dnephin/dobi/execenv/environment.go:61 +0xb5
github.com/dnephin/dobi/config.(*EnvConfig).Resolve(0xc420232700, 0x97a780, 0xc42008bf40, 0x1, 0x0, 0x43199c, 0x0)
    /go/src/github.com/dnephin/dobi/config/env.go:53 +0x113
github.com/dnephin/dobi/tasks.executeTasks(0xc4203b0de0, 0xc4203c4380, 0x0, 0x0)
    /go/src/github.com/dnephin/dobi/tasks/tasks.go:139 +0x20c
github.com/dnephin/dobi/tasks.Run(0x981840, 0xc42016e7e0, 0xc420198750, 0xc420079970, 0x1, 0x1, 0x100, 0x4288d4, 0x93efc8)
    /go/src/github.com/dnephin/dobi/tasks/tasks.go:223 +0x1cd
github.com/dnephin/dobi/cmd.runDobi(0x91ed8e, 0x9, 0x0, 0xc420079970, 0x1, 0x1, 0x0, 0x7fd785, 0x0)
    /go/src/github.com/dnephin/dobi/cmd/dobi.go:92 +0x1c6
github.com/dnephin/dobi/cmd.NewRootCommand.func1(0xc42009cd80, 0xc420079970, 0x1, 0x1, 0x0, 0x0)
    /go/src/github.com/dnephin/dobi/cmd/dobi.go:49 +0x76
github.com/dnephin/dobi/vendor/github.com/spf13/cobra.(*Command).execute(0xc42009cd80, 0xc420072030, 0x1, 0x1, 0xc42009cd80, 0xc420072030)
    /go/src/github.com/dnephin/dobi/vendor/github.com/spf13/cobra/command.go:695 +0x449
github.com/dnephin/dobi/vendor/github.com/spf13/cobra.(*Command).ExecuteC(0xc42009cd80, 0xc4200a0b80, 0xc42009cfc0, 0xc42009d200)
    /go/src/github.com/dnephin/dobi/vendor/github.com/spf13/cobra/command.go:792 +0x2e4
github.com/dnephin/dobi/vendor/github.com/spf13/cobra.(*Command).Execute(0xc42009cd80, 0x0, 0x0)
    /go/src/github.com/dnephin/dobi/vendor/github.com/spf13/cobra/command.go:744 +0x2b
main.main()
    /go/src/github.com/dnephin/dobi/main.go:9 +0x27
dnephin commented 6 years ago

Thanks for the bug report! I have a fix in #139. It will still be an error to use git.branch on a detached head, but the error message will be clear. You can prevent the error by setting a default value.

Enteee commented 6 years ago

thank you! :+1:

Note that there is a caveat when integrating dobi in a gitlab cicd pipeline (and possibly others). When building a branch, Gitlab will always checkout the branch by it's hash. This means the runner always has a detached head. I understand that there is nothing dobi can do to get from a detached head state back to the branch (possibly ambiguity since there could be two branches referencing the same commit).