Merge pull request #27 from drone-plugins/refactor-and-fix

Refactor, fixes and latest Hugo version
This commit is contained in:
Thomas Boerger 2020-05-15 07:57:30 +02:00 committed by GitHub
commit 054249e93a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 232 additions and 170 deletions

View File

@ -10,7 +10,7 @@ platform:
steps:
- name: vet
pull: always
image: golang:1.11-windowsservercore-1803
image: golang:1.14-windowsservercore-1803
commands:
- go vet ./...
environment:
@ -21,7 +21,7 @@ steps:
- name: test
pull: always
image: golang:1.11-windowsservercore-1803
image: golang:1.14-windowsservercore-1803
commands:
- go test -cover ./...
environment:
@ -52,7 +52,7 @@ platform:
steps:
- name: build-push
pull: always
image: golang:1.11-windowsservercore-1803
image: golang:1.14-windowsservercore-1803
commands:
- "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/windows/amd64/drone-hugo.exe"
environment:
@ -65,7 +65,7 @@ steps:
- name: build-tag
pull: always
image: golang:1.11-windowsservercore-1803
image: golang:1.14-windowsservercore-1803
commands:
- "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/windows/amd64/drone-hugo.exe"
environment:
@ -77,7 +77,7 @@ steps:
- name: executable
pull: always
image: golang:1.11-windowsservercore-1803
image: golang:1.14-windowsservercore-1803
commands:
- ./release/windows/amd64/drone-hugo.exe --help
@ -148,7 +148,7 @@ platform:
steps:
- name: build-push
pull: always
image: golang:1.11-windowsservercore-1809
image: golang:1.14-windowsservercore-1809
commands:
- "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/windows/amd64/drone-hugo.exe"
environment:
@ -161,7 +161,7 @@ steps:
- name: build-tag
pull: always
image: golang:1.11-windowsservercore-1809
image: golang:1.14-windowsservercore-1809
commands:
- "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/windows/amd64/drone-hugo.exe"
environment:
@ -173,7 +173,7 @@ steps:
- name: executable
pull: always
image: golang:1.11-windowsservercore-1809
image: golang:1.14-windowsservercore-1809
commands:
- ./release/windows/amd64/drone-hugo.exe --help

View File

@ -9,7 +9,7 @@ platform:
steps:
- name: vet
pull: always
image: golang:1.11
image: golang:1.14
commands:
- go vet ./...
environment:
@ -20,7 +20,7 @@ steps:
- name: test
pull: always
image: golang:1.11
image: golang:1.14
commands:
- go test -cover ./...
environment:
@ -50,7 +50,7 @@ platform:
steps:
- name: build-push
pull: always
image: golang:1.11
image: golang:1.14
commands:
- "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/amd64/drone-hugo"
environment:
@ -63,7 +63,7 @@ steps:
- name: build-tag
pull: always
image: golang:1.11
image: golang:1.14
commands:
- "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/amd64/drone-hugo"
environment:
@ -75,7 +75,7 @@ steps:
- name: executable
pull: always
image: golang:1.11
image: golang:1.14
commands:
- ./release/linux/amd64/drone-hugo --help
@ -134,7 +134,7 @@ platform:
steps:
- name: build-push
pull: always
image: golang:1.11
image: golang:1.14
commands:
- "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/arm64/drone-hugo"
environment:
@ -147,7 +147,7 @@ steps:
- name: build-tag
pull: always
image: golang:1.11
image: golang:1.14
commands:
- "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/arm64/drone-hugo"
environment:
@ -159,7 +159,7 @@ steps:
- name: executable
pull: always
image: golang:1.11
image: golang:1.14
commands:
- ./release/linux/arm64/drone-hugo --help
@ -218,7 +218,7 @@ platform:
steps:
- name: build-push
pull: always
image: golang:1.11
image: golang:1.14
commands:
- "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/arm/drone-hugo"
environment:
@ -231,7 +231,7 @@ steps:
- name: build-tag
pull: always
image: golang:1.11
image: golang:1.14
commands:
- "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/arm/drone-hugo"
environment:
@ -243,7 +243,7 @@ steps:
- name: executable
pull: always
image: golang:1.11
image: golang:1.14
commands:
- ./release/linux/arm/drone-hugo --help

View File

@ -1,3 +1,11 @@
FROM amd64/golang:1.14-alpine AS build
RUN apk add --no-cache git build-base && \
git clone --branch v0.70.0 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
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.schema-version="1.0"
ENV HUGO_VERSION=0.58.3
ENV HUGO_ARCH=64bit
ENV PLUGIN_HUGO_ARCH=$HUGO_ARCH
ENV PLUGIN_HUGO_SHIPPED_VERSION=$HUGO_VERSION
RUN apk --no-cache add git libc6-compat libstdc++
RUN apk --no-cache add git && \
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_ARCH=64bit
ENV PLUGIN_HUGO_SHIPPED_VERSION=0.70.0
COPY --from=build /tmp/hugo /bin/hugo
COPY --from=build /tmp/hugo-extended /bin/hugo-extended
ADD release/linux/amd64/drone-hugo /bin/
ENTRYPOINT ["/bin/drone-hugo"]

View File

@ -1,3 +1,11 @@
FROM arm32v6/golang:1.14-alpine AS build
RUN apk add --no-cache git build-base && \
git clone --branch v0.70.0 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
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.schema-version="1.0"
ENV HUGO_VERSION=0.58.3
ENV HUGO_ARCH=arm
ENV PLUGIN_HUGO_ARCH=$HUGO_ARCH
ENV PLUGIN_HUGO_SHIPPED_VERSION=$HUGO_VERSION
RUN apk --no-cache add git libc6-compat libstdc++
RUN apk --no-cache add git && \
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_ARCH=arm
ENV PLUGIN_HUGO_SHIPPED_VERSION=0.70.0
COPY --from=build /tmp/hugo /bin/hugo
COPY --from=build /tmp/hugo-extended /bin/hugo-extended
ADD release/linux/arm/drone-hugo /bin/
ENTRYPOINT ["/bin/drone-hugo"]

View File

@ -1,3 +1,11 @@
FROM arm64v8/golang:1.14-alpine AS build
RUN apk add --no-cache git build-base && \
git clone --branch v0.70.0 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
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.schema-version="1.0"
ENV HUGO_VERSION=0.58.3
ENV HUGO_ARCH=arm64
ENV PLUGIN_HUGO_ARCH=$HUGO_ARCH
ENV PLUGIN_HUGO_SHIPPED_VERSION=$HUGO_VERSION
RUN apk --no-cache add git libc6-compat libstdc++
RUN apk --no-cache add git && \
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_ARCH=arm64
ENV PLUGIN_HUGO_SHIPPED_VERSION=0.70.0
COPY --from=build /tmp/hugo /bin/hugo
COPY --from=build /tmp/hugo-extended /bin/hugo-extended
ADD release/linux/arm64/drone-hugo /bin/
ENTRYPOINT ["/bin/drone-hugo"]

View File

@ -6,22 +6,71 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"strings"
"github.com/pkg/errors"
)
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 {
var archType string
var binary string
// Get will download the specified hugo verion
func Get(version string, extended bool) (string, error) {
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 {
binary = "hugo_extended"
@ -29,6 +78,15 @@ func downloadURL(version string, extended bool) string {
binary = "hugo"
}
switch runtime.GOOS {
case "linux":
osName = "Linux"
case "windows":
osName = "Windows"
default:
osName = "unsupported"
}
switch runtime.GOARCH {
case "amd64":
archType = "64bit"
@ -41,55 +99,17 @@ func downloadURL(version string, extended bool) string {
default:
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("", "")
if err != nil {
return "", nil, errors.Wrap(err, "")
return "", nil, err
}
f, err := ioutil.TempFile(d, "")
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
}
}
}

View File

@ -6,20 +6,20 @@ import (
func TestDownloadURL(t *testing.T) {
want := "https://github.com/gohugoio/hugo/releases/download/v1.0/hugo_1.0_Linux-64bit.tar.gz"
if got := downloadURL("1.0", false); got != want {
if got := download("1.0", false); got != want {
t.Errorf("Download url is not correct, got: %s, want: %s", got, want)
}
}
func TestDownloadURLExtended(t *testing.T) {
want := "https://github.com/gohugoio/hugo/releases/download/v0.55.4/hugo_extended_0.55.4_Linux-64bit.tar.gz"
if got := downloadURL("0.55.4", true); got != want {
if got := download("0.55.4", true); got != want {
t.Errorf("Download url is not correct, got: %s, want: %s", got, want)
}
}
func TestGet(t *testing.T) {
if binPath , err := Get("0.42", false); err != nil {
if binPath, err := Get("0.42", false); err != nil {
t.Errorf("Failed to download archive: %s", err)
if binPath == "" {
t.Errorf("binPath was nil")

35
main.go
View File

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

View File

@ -5,7 +5,7 @@ local test_pipeline_name = 'testing';
local windows(os) = os == 'windows';
local golang_image(os, version) =
'golang:' + '1.11' + if windows(os) then '-windowsservercore-' + version else '';
'golang:' + '1.14' + if windows(os) then '-windowsservercore-' + version else '';
{
test(os='linux', arch='amd64', version='')::

106
plugin.go
View File

@ -16,43 +16,53 @@ type (
}
Config struct {
BuildDrafts bool
BuildExpired bool
BuildFuture bool
CacheDir string
URL string
Drafts bool
Expired bool
Future bool
Validate bool
Cache string
Config string
Content string
Layout string
Output string
Source string
Theme string
Url string
HugoVersion string
HugoExtended bool
Validate bool
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)
// 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 p.Config.Extended {
hugoExecutable = "hugo-extended"
}
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 {
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, " "))
}

View File

@ -1,8 +1,9 @@
package main
import (
"github.com/pkg/errors"
"testing"
"github.com/pkg/errors"
)
func TestCommandValidate(t *testing.T) {
@ -25,12 +26,12 @@ func TestCommandValidate(t *testing.T) {
func TestVersionEqual(t *testing.T) {
want := true
if got := versionsEqual("1.0", "1.0", false); want != got {
if got := versionsEqual("1.0", "1.0"); want != got {
t.Errorf("want: %t, got: %t", want, got)
}
want = false
if got := versionsEqual("1.5", "1.0", false); want != got {
if got := versionsEqual("1.5", "1.0"); want != got {
t.Errorf("want: %t, got: %t", want, got)
}
}