Compile Hugo on our own, fix extended version and refactoring

Since the extended version is only available for amd64 I have started to
build Hugo on our own in a multi stage Dockerfile. Now we are installing
both binaries, regular hugo and extended hugo, bundled into the Docker
image.

Beside that the download for regular hugo and extended hugo should also
be more solid now.

After that I have also added more aliases for the environment variables.
As mentioned on some GitHub issue we are also installing more required
packages for a downloaded extended Hugo version as this relies on
libc6-compat and libstdc++.
This commit is contained in:
Thomas Boerger 2020-05-15 02:13:51 +02:00
commit b4fa179180
No known key found for this signature in database
GPG key ID: 09745AFF9D63C79B
6 changed files with 199 additions and 138 deletions

114
plugin.go
View file

@ -16,43 +16,53 @@ type (
}
Config struct {
BuildDrafts bool
BuildExpired bool
BuildFuture bool
CacheDir string
Config string
Content string
Layout string
Output string
Source string
Theme string
Url string
HugoVersion string
HugoExtended bool
Validate bool
URL string
Drafts bool
Expired bool
Future bool
Validate bool
Cache string
Config string
Content string
Layout string
Output string
Source string
Theme string
Version string
Extended bool
}
)
var hugoExecutable = "hugo"
var (
hugoExecutable = "hugo"
)
// Exec executes the plugins functionality
func (p Plugin) Exec() error {
var cmds = make([]*exec.Cmd, 0)
if p.Config.Extended {
hugoExecutable = "hugo-extended"
}
// Check if buildIn plugin version equals
// plugin version declared in drone.yml
if !versionsEqual(p.BuildInVersion, p.Config.HugoVersion, p.Config.HugoExtended) {
hugoVersion := p.Config.HugoVersion
if hugoVersion == "" {
hugoVersion = p.BuildInVersion
if !versionsEqual(p.BuildInVersion, p.Config.Version) {
version := p.Config.Version
if version == "" {
version = p.BuildInVersion
}
hugoPath, err := download.Get(hugoVersion, p.Config.HugoExtended)
executable, err := download.Get(version, p.Config.Extended)
if err != nil {
return err
}
hugoExecutable = hugoPath
hugoExecutable = executable
}
if p.Config.Validate {
cmds = append(cmds, commandValidate(p.Config))
}
@ -63,75 +73,66 @@ func (p Plugin) Exec() error {
func commandValidate(config Config) *exec.Cmd {
args := []string{"check"}
if config.Config != "" {
args = append(args, "--config", config.Config)
}
return exec.Command(hugoExecutable, args...)
}
func commandBuild(config Config) *exec.Cmd {
var args = make([]string, 0)
// add bool args
if config.BuildDrafts {
if config.Drafts {
args = append(args, "-D")
}
if config.BuildExpired {
if config.Expired {
args = append(args, "-E")
}
if config.BuildFuture {
if config.Future {
args = append(args, "-F")
}
// add string args
if config.CacheDir != "" {
args = append(args, "--cacheDir", config.CacheDir)
if config.Cache != "" {
args = append(args, "--cacheDir", config.Cache)
}
if config.Config != "" {
args = append(args, "--config", config.Config)
}
if config.Content != "" {
args = append(args, "--contentDir", config.Content)
}
if config.Layout != "" {
args = append(args, "--layoutDir", config.Layout)
}
if config.Output != "" {
args = append(args, "--destination", config.Output)
}
if config.Source != "" {
args = append(args, "--source", config.Source)
}
if config.Theme != "" {
args = append(args, "--theme", config.Theme)
}
if config.Url != "" {
args = append(args, "--baseURL", config.Url)
if config.URL != "" {
args = append(args, "--baseURL", config.URL)
}
return exec.Command(hugoExecutable, args...)
}
// trace writes each command to stdout with the command wrapped in an xml
// tag so that it can be extracted and displayed in the logs.
func trace(cmd *exec.Cmd) {
fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " "))
}
func versionsEqual(version string, toCompare string, extended bool) bool {
if extended {
return false
}
if toCompare == version || toCompare == "" {
return true
} else {
return false
}
}
// execAll executes a slice of commands as a batch job
func execAll(cmds []*exec.Cmd) error {
// Execute all commands
for _, cmd := range cmds {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@ -141,5 +142,20 @@ func execAll(cmds []*exec.Cmd) error {
return err
}
}
return nil
}
func versionsEqual(version string, toCompare string) bool {
if toCompare == version || toCompare == "" {
return true
}
return false
}
// trace writes each command to stdout with the command wrapped in an xml
// tag so that it can be extracted and displayed in the logs.
func trace(cmd *exec.Cmd) {
fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " "))
}