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
parent 8a8184da98
commit b4fa179180
No known key found for this signature in database
GPG Key ID: 09745AFF9D63C79B
6 changed files with 205 additions and 144 deletions

View File

@ -1,3 +1,11 @@
FROM amd64/golang:1.11-alpine AS build
RUN apk add --no-cache git build-base && \
git clone --branch v0.58.3 https://github.com/gohugoio/hugo.git && \
cd hugo/ && \
CGO_ENABLED=0 go build -ldflags "-s -w -X github.com/gohugoio/hugo/common/hugo.buildDate=$(date +%Y-%m-%dT%H:%M:%SZ) -X github.com/gohugoio/hugo/common/hugo.commitHash=$(git rev-parse --short HEAD)" -o /tmp/hugo . && \
CGO_ENABLED=1 go build -tags extended -ldflags "-s -w -X github.com/gohugoio/hugo/common/hugo.buildDate=$(date +%Y-%m-%dT%H:%M:%SZ) -X github.com/gohugoio/hugo/common/hugo.commitHash=$(git rev-parse --short HEAD)" -o /tmp/hugo-extended
FROM plugins/base:linux-amd64 FROM plugins/base:linux-amd64
LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \ LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \
@ -5,13 +13,13 @@ LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \
org.label-schema.vendor="Drone.IO Community" \ org.label-schema.vendor="Drone.IO Community" \
org.label-schema.schema-version="1.0" org.label-schema.schema-version="1.0"
ENV HUGO_VERSION=0.58.3 RUN apk --no-cache add git libc6-compat libstdc++
ENV HUGO_ARCH=64bit
ENV PLUGIN_HUGO_ARCH=$HUGO_ARCH
ENV PLUGIN_HUGO_SHIPPED_VERSION=$HUGO_VERSION
RUN apk --no-cache add git && \ ENV PLUGIN_HUGO_ARCH=64bit
wget -O- https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_Linux-${HUGO_ARCH}.tar.gz | tar xz -C /bin ENV PLUGIN_HUGO_SHIPPED_VERSION=0.58.3
COPY --from=build /tmp/hugo /bin/hugo
COPY --from=build /tmp/hugo-extended /bin/hugo-extended
ADD release/linux/amd64/drone-hugo /bin/ ADD release/linux/amd64/drone-hugo /bin/
ENTRYPOINT ["/bin/drone-hugo"] ENTRYPOINT ["/bin/drone-hugo"]

View File

@ -1,3 +1,11 @@
FROM arm32v6/golang:1.11-alpine AS build
RUN apk add --no-cache git build-base && \
git clone --branch v0.58.3 https://github.com/gohugoio/hugo.git && \
cd hugo/ && \
CGO_ENABLED=0 go build -ldflags "-s -w -X github.com/gohugoio/hugo/common/hugo.buildDate=$(date +%Y-%m-%dT%H:%M:%SZ) -X github.com/gohugoio/hugo/common/hugo.commitHash=$(git rev-parse --short HEAD)" -o /tmp/hugo . && \
CGO_ENABLED=1 go build -tags extended -ldflags "-s -w -X github.com/gohugoio/hugo/common/hugo.buildDate=$(date +%Y-%m-%dT%H:%M:%SZ) -X github.com/gohugoio/hugo/common/hugo.commitHash=$(git rev-parse --short HEAD)" -o /tmp/hugo-extended
FROM plugins/base:linux-arm FROM plugins/base:linux-arm
LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \ LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \
@ -5,13 +13,13 @@ LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \
org.label-schema.vendor="Drone.IO Community" \ org.label-schema.vendor="Drone.IO Community" \
org.label-schema.schema-version="1.0" org.label-schema.schema-version="1.0"
ENV HUGO_VERSION=0.58.3 RUN apk --no-cache add git libc6-compat libstdc++
ENV HUGO_ARCH=arm
ENV PLUGIN_HUGO_ARCH=$HUGO_ARCH
ENV PLUGIN_HUGO_SHIPPED_VERSION=$HUGO_VERSION
RUN apk --no-cache add git && \ ENV PLUGIN_HUGO_ARCH=arm
wget -O- https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_Linux-${HUGO_ARCH}.tar.gz | tar xz -C /bin ENV PLUGIN_HUGO_SHIPPED_VERSION=0.58.3
COPY --from=build /tmp/hugo /bin/hugo
COPY --from=build /tmp/hugo-extended /bin/hugo-extended
ADD release/linux/arm/drone-hugo /bin/ ADD release/linux/arm/drone-hugo /bin/
ENTRYPOINT ["/bin/drone-hugo"] ENTRYPOINT ["/bin/drone-hugo"]

View File

@ -1,3 +1,11 @@
FROM arm64v8/golang:1.11-alpine AS build
RUN apk add --no-cache git build-base && \
git clone --branch v0.58.3 https://github.com/gohugoio/hugo.git && \
cd hugo/ && \
CGO_ENABLED=0 go build -ldflags "-s -w -X github.com/gohugoio/hugo/common/hugo.buildDate=$(date +%Y-%m-%dT%H:%M:%SZ) -X github.com/gohugoio/hugo/common/hugo.commitHash=$(git rev-parse --short HEAD)" -o /tmp/hugo . && \
CGO_ENABLED=1 go build -tags extended -ldflags "-s -w -X github.com/gohugoio/hugo/common/hugo.buildDate=$(date +%Y-%m-%dT%H:%M:%SZ) -X github.com/gohugoio/hugo/common/hugo.commitHash=$(git rev-parse --short HEAD)" -o /tmp/hugo-extended
FROM plugins/base:linux-arm64 FROM plugins/base:linux-arm64
LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \ LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \
@ -5,13 +13,13 @@ LABEL maintainer="Drone.IO Community <drone-dev@googlegroups.com>" \
org.label-schema.vendor="Drone.IO Community" \ org.label-schema.vendor="Drone.IO Community" \
org.label-schema.schema-version="1.0" org.label-schema.schema-version="1.0"
ENV HUGO_VERSION=0.58.3 RUN apk --no-cache add git libc6-compat libstdc++
ENV HUGO_ARCH=arm64
ENV PLUGIN_HUGO_ARCH=$HUGO_ARCH
ENV PLUGIN_HUGO_SHIPPED_VERSION=$HUGO_VERSION
RUN apk --no-cache add git && \ ENV PLUGIN_HUGO_ARCH=arm64
wget -O- https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_Linux-${HUGO_ARCH}.tar.gz | tar xz -C /bin ENV PLUGIN_HUGO_SHIPPED_VERSION=0.58.3
COPY --from=build /tmp/hugo /bin/hugo
COPY --from=build /tmp/hugo-extended /bin/hugo-extended
ADD release/linux/arm64/drone-hugo /bin/ ADD release/linux/arm64/drone-hugo /bin/
ENTRYPOINT ["/bin/drone-hugo"] ENTRYPOINT ["/bin/drone-hugo"]

View File

@ -6,22 +6,71 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"os" "os"
"runtime" "runtime"
"strings" "strings"
"github.com/pkg/errors"
) )
var ( var (
_downloadURL = "https://github.com/gohugoio/hugo/releases/download/v%s/%s_%s_Linux-%s.tar.gz" url = "https://github.com/gohugoio/hugo/releases/download/v%s/%s_%s_%s-%s.tar.gz"
) )
func downloadURL(version string, extended bool) string { // Get will download the specified hugo verion
var archType string func Get(version string, extended bool) (string, error) {
var binary string resp, err := http.Get(download(version, extended))
if err != nil {
return "", err
}
defer resp.Body.Close()
gz, err := gzip.NewReader(resp.Body)
if err != nil {
return "", err
}
defer gz.Close()
targz := tar.NewReader(gz)
hugoPath, hugoBin, err := tempfile()
if err != nil {
return "", err
}
defer hugoBin.Close()
for {
h, err := targz.Next()
if err == io.EOF {
return "", fmt.Errorf("no hugo binary found")
}
if err != nil {
return "", err
}
if strings.HasSuffix(h.Name, "hugo") {
io.Copy(hugoBin, targz)
if err := os.Chmod(hugoPath, 0755); err != nil {
return "", err
}
return hugoPath, nil
}
}
}
func download(version string, extended bool) string {
var (
binary string
osName string
archType string
)
if extended { if extended {
binary = "hugo_extended" binary = "hugo_extended"
@ -29,6 +78,15 @@ func downloadURL(version string, extended bool) string {
binary = "hugo" binary = "hugo"
} }
switch runtime.GOOS {
case "linux":
osName = "Linux"
case "windows":
osName = "Windows"
default:
osName = "unsupported"
}
switch runtime.GOARCH { switch runtime.GOARCH {
case "amd64": case "amd64":
archType = "64bit" archType = "64bit"
@ -41,55 +99,17 @@ func downloadURL(version string, extended bool) string {
default: default:
archType = "unsupported" archType = "unsupported"
} }
return fmt.Sprintf(_downloadURL, version, binary, version, archType)
return fmt.Sprintf(url, version, binary, version, osName, archType)
} }
func getTempFile() (string, io.WriteCloser, error) { func tempfile() (string, io.WriteCloser, error) {
d, err := ioutil.TempDir("", "") d, err := ioutil.TempDir("", "")
if err != nil { if err != nil {
return "", nil, errors.Wrap(err, "") return "", nil, err
} }
f, err := ioutil.TempFile(d, "") f, err := ioutil.TempFile(d, "")
return f.Name(), f, err return f.Name(), f, err
} }
// Get will download the specified hugo verion
func Get(version string, extended bool) (string, error) {
resp, err := http.Get(downloadURL(version, extended))
if err != nil {
return "", errors.Wrap(err, "")
}
defer resp.Body.Close()
gz, err := gzip.NewReader(resp.Body)
if err != nil {
return "", errors.Wrap(err, "")
}
defer gz.Close()
targz := tar.NewReader(gz)
hugoPath, hugoBin, err := getTempFile()
if err != nil {
log.Printf("ERROR: %s", err)
return "", errors.Wrap(err, "")
}
defer hugoBin.Close()
for {
h, err := targz.Next()
if err == io.EOF {
return "", errors.New("no hugo binary found")
}
if err != nil {
return "", errors.Wrap(err, "")
}
if strings.HasSuffix(h.Name, "hugo") {
io.Copy(hugoBin, targz)
if err := os.Chmod(hugoPath, 0755); err != nil {
log.Fatal(err)
}
return hugoPath, nil
}
}
}

35
main.go
View File

@ -19,24 +19,24 @@ func main() {
app.Version = version app.Version = version
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
cli.BoolFlag{ cli.BoolFlag{
Name: "buildDrafts", Name: "drafts",
Usage: " include content marked as draft", Usage: " include content marked as draft",
EnvVar: "PLUGIN_BUILDDRAFTS", EnvVar: "PLUGIN_BUILDDRAFTS,PLUGIN_DRAFTS",
}, },
cli.BoolFlag{ cli.BoolFlag{
Name: "buildExpired", Name: "expired",
Usage: "include expired content", Usage: "include expired content",
EnvVar: "PLUGIN_BUILDEXPIRED", EnvVar: "PLUGIN_BUILDEXPIRED,PLUGIN_EXPIRED",
}, },
cli.BoolFlag{ cli.BoolFlag{
Name: "buildFuture", Name: "future",
Usage: "include content with publishdate in the future", Usage: "include content with publishdate in the future",
EnvVar: "PLUGIN_BUILDFUTURE", EnvVar: "PLUGIN_BUILDFUTURE,PLUGIN_FUTURE",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "cacheDir", Name: "cache",
Usage: "change cache directory (useful when using caching plugins)", Usage: "change cache directory (useful when using caching plugins)",
EnvVar: "PLUGIN_CACHEDIR", EnvVar: "PLUGIN_CACHEDIR,PLUGIN_CACHE",
Value: "", Value: "",
}, },
cli.StringFlag{ cli.StringFlag{
@ -87,13 +87,13 @@ func main() {
EnvVar: "PLUGIN_VALIDATE", EnvVar: "PLUGIN_VALIDATE",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "hugoVersion", Name: "hugoversion",
Usage: "the hugo version to be used", Usage: "the hugo version to be used",
EnvVar: "PLUGIN_HUGO_VERSION", EnvVar: "PLUGIN_HUGO_VERSION,PLUGIN_VERSION",
Value: "", Value: "",
}, },
cli.BoolFlag{ cli.BoolFlag{
Name: "hugoExtended", Name: "extended",
Usage: "If the hugo extended package should be used", Usage: "If the hugo extended package should be used",
EnvVar: "PLUGIN_EXTENDED", EnvVar: "PLUGIN_EXTENDED",
}, },
@ -106,11 +106,10 @@ func main() {
func run(c *cli.Context) error { func run(c *cli.Context) error {
plugin := Plugin{ plugin := Plugin{
Config: Config{ Config: Config{
HugoVersion: c.String("hugoVersion"), URL: c.String("url"),
HugoExtended: c.Bool("hugoExtended"), Drafts: c.Bool("drafts"),
BuildDrafts: c.Bool("buildDrafts"), Expired: c.Bool("expired"),
BuildExpired: c.Bool("buildExpired"), Future: c.Bool("future"),
BuildFuture: c.Bool("buildFuture"),
Validate: c.Bool("validate"), Validate: c.Bool("validate"),
Config: c.String("config"), Config: c.String("config"),
Content: c.String("content"), Content: c.String("content"),
@ -118,9 +117,11 @@ func run(c *cli.Context) error {
Output: c.String("output"), Output: c.String("output"),
Source: c.String("source"), Source: c.String("source"),
Theme: c.String("theme"), Theme: c.String("theme"),
Url: c.String("url"), Version: c.String("hugoversion"),
Extended: c.Bool("extended"),
}, },
BuildInVersion: os.Getenv("PLUGIN_HUGO_SHIPPED_VERSION"), BuildInVersion: os.Getenv("PLUGIN_HUGO_SHIPPED_VERSION"),
} }
return plugin.Exec() return plugin.Exec()
} }

106
plugin.go
View File

@ -16,43 +16,53 @@ type (
} }
Config struct { Config struct {
BuildDrafts bool URL string
BuildExpired bool Drafts bool
BuildFuture bool Expired bool
CacheDir string Future bool
Validate bool
Cache string
Config string Config string
Content string Content string
Layout string Layout string
Output string Output string
Source string Source string
Theme string Theme string
Url string Version string
HugoVersion string Extended bool
HugoExtended bool
Validate bool
} }
) )
var hugoExecutable = "hugo" var (
hugoExecutable = "hugo"
)
// Exec executes the plugins functionality // Exec executes the plugins functionality
func (p Plugin) Exec() error { func (p Plugin) Exec() error {
var cmds = make([]*exec.Cmd, 0) var cmds = make([]*exec.Cmd, 0)
// Check if buildIn plugin version equals if p.Config.Extended {
// plugin version declared in drone.yml hugoExecutable = "hugo-extended"
if !versionsEqual(p.BuildInVersion, p.Config.HugoVersion, p.Config.HugoExtended) {
hugoVersion := p.Config.HugoVersion
if hugoVersion == "" {
hugoVersion = p.BuildInVersion
} }
hugoPath, err := download.Get(hugoVersion, p.Config.HugoExtended) // Check if buildIn plugin version equals
// plugin version declared in drone.yml
if !versionsEqual(p.BuildInVersion, p.Config.Version) {
version := p.Config.Version
if version == "" {
version = p.BuildInVersion
}
executable, err := download.Get(version, p.Config.Extended)
if err != nil { if err != nil {
return err return err
} }
hugoExecutable = hugoPath
hugoExecutable = executable
} }
if p.Config.Validate { if p.Config.Validate {
cmds = append(cmds, commandValidate(p.Config)) cmds = append(cmds, commandValidate(p.Config))
} }
@ -63,75 +73,66 @@ func (p Plugin) Exec() error {
func commandValidate(config Config) *exec.Cmd { func commandValidate(config Config) *exec.Cmd {
args := []string{"check"} args := []string{"check"}
if config.Config != "" { if config.Config != "" {
args = append(args, "--config", config.Config) args = append(args, "--config", config.Config)
} }
return exec.Command(hugoExecutable, args...) return exec.Command(hugoExecutable, args...)
} }
func commandBuild(config Config) *exec.Cmd { func commandBuild(config Config) *exec.Cmd {
var args = make([]string, 0) var args = make([]string, 0)
// add bool args if config.Drafts {
if config.BuildDrafts {
args = append(args, "-D") args = append(args, "-D")
} }
if config.BuildExpired {
if config.Expired {
args = append(args, "-E") args = append(args, "-E")
} }
if config.BuildFuture {
if config.Future {
args = append(args, "-F") args = append(args, "-F")
} }
// add string args
if config.CacheDir != "" { if config.Cache != "" {
args = append(args, "--cacheDir", config.CacheDir) args = append(args, "--cacheDir", config.Cache)
} }
if config.Config != "" { if config.Config != "" {
args = append(args, "--config", config.Config) args = append(args, "--config", config.Config)
} }
if config.Content != "" { if config.Content != "" {
args = append(args, "--contentDir", config.Content) args = append(args, "--contentDir", config.Content)
} }
if config.Layout != "" { if config.Layout != "" {
args = append(args, "--layoutDir", config.Layout) args = append(args, "--layoutDir", config.Layout)
} }
if config.Output != "" { if config.Output != "" {
args = append(args, "--destination", config.Output) args = append(args, "--destination", config.Output)
} }
if config.Source != "" { if config.Source != "" {
args = append(args, "--source", config.Source) args = append(args, "--source", config.Source)
} }
if config.Theme != "" { if config.Theme != "" {
args = append(args, "--theme", 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...) 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 // execAll executes a slice of commands as a batch job
func execAll(cmds []*exec.Cmd) error { func execAll(cmds []*exec.Cmd) error {
// Execute all commands
for _, cmd := range cmds { for _, cmd := range cmds {
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -141,5 +142,20 @@ func execAll(cmds []*exec.Cmd) error {
return err return err
} }
} }
return nil 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, " "))
}