Attempting to create a build script

1. The problem I’m having:

I know this is more of a bash question than caddy, I apologize and love you for still helping

I’m trying to create a bash script to build caddy with defined plugins using xcaddy but it keeps erroring no matter what I try

2. Error messages and/or full log output:

○ → build-caddy
Using plugins defined in /home/dietpi/.local/bin/build-caddy:
	github.com/caddy-dns/cloudflare
	github.com/caddy-dns/desec
	github.com/corazawaf/coraza-caddy/v2
	github.com/caddyserver/nginx-adapter
2023/12/20 00:56:02 [INFO] Temporary folder: /tmp/buildenv_2023-12-20-0056.2963610898
2023/12/20 00:56:02 [INFO] Writing main module: /tmp/buildenv_2023-12-20-0056.2963610898/main.go
package main

import (
	caddycmd "github.com/caddyserver/caddy/v2/cmd"

	// plug in Caddy modules here
	_ "github.com/caddyserver/caddy/v2/modules/standard"
)

func main() {
	caddycmd.Main()
}
2023/12/20 00:56:02 [INFO] Initializing Go module
2023/12/20 00:56:02 [INFO] exec (timeout=0s): /usr/bin/go mod init caddy
go: creating new go.mod: module caddy
go: to add module requirements and sums:
	go mod tidy
2023/12/20 00:56:02 [INFO] Pinning versions
2023/12/20 00:56:02 [INFO] exec (timeout=0s): /usr/bin/go get -d -v github.com/caddyserver/caddy/v2@ --with github.com/caddy-dns/cloudflare --with github.com/caddy-dns/desec --with github.com/corazawaf/coraza-caddy/v2 --with github.com/caddyserver/nginx-adapter
go: github.com/caddyserver/caddy/v2@ --with github.com/caddy-dns/cloudflare --with github.com/caddy-dns/desec --with github.com/corazawaf/coraza-caddy/v2 --with github.com/caddyserver/nginx-adapter : invalidversion: version " --with github.com/caddy-dns/cloudflare --with github.com/caddy-dns/desec --with github.com/corazawaf/coraza-caddy/v2 --with github.com/caddyserver/nginx-adapter " invalid: disallowed version string
2023/12/20 00:56:02 [FATAL] exit status 1

3. Caddy version:

○ → xcaddy version
v0.3.5 h1:XyC3clncb2Q3gTQC6hOJerRt3FS9+vAljW1f8jlryZA=

4. How I installed and ran Caddy:

N/A

a. System environment:

DietPi v8.2.5 (Deb bookworm) on RPi4
GNU bash, version 5.2.15(1)-release (aarch64-unknown-linux-gnu)

b. Command:

Current revision:

○ → cat ~/.local/bin/build-caddy
#!/usr/bin/env bash

_plugins=(
    "github.com/caddy-dns/cloudflare "
    "github.com/caddy-dns/desec "
    "github.com/corazawaf/coraza-caddy/v2 "
    "github.com/caddyserver/nginx-adapter "
)

echo Using plugins defined in "${BASH_SOURCE[@]}":

_cont=
_plugin=
_with=

if [[ -n "${_plugins[@]}" ]]; then
    for _plugin in "${_plugins[@]}"; do
        echo -e "\t${_plugin}"
        _with+="--with ${_plugin}"
    done
else
    read -p "No plugins defined, continue? [Y/n]" _cont
    _cont=${_cont,,}
    if [[ $_cont =~ ^(y| ) ]] || [[ -z $_cont ]]; then
        continue
    else
        exit
    fi
    unset _cont
fi

CGO_ENABLED=0 GOOS=linux \
xcaddy build \
"${_with/-/ -}"

if [[ ./caddy -nt /usr/local/bin/caddy ]]; then
    sudo mv ./caddy /usr/local/bin/caddy
fi

c. Service/unit/compose file:

N/A

d. My complete Caddy config:

N/A

5. Links to relevant resources:

N/A


TIA!

xstefen

Also strange, if I define a version IE with

CGO_ENABLED=0 GOOS=linux \
xcaddy build 'v2.7.6' \
"${_with/-/ -}" \
--output ./caddy-x

I get

2023/12/20 01:12:02 [ERROR] missing flag; caddy version already set at v2.7.6

I think this a bash scripting issue. You need to make sure the --with args are actually separate tokens. It seems like it’s getting passed as a single contiguous string.

I think you can use the spellcheck command to help with linting your script.

Thanks for the reply. shellcheck is my go-to tidy check once I have a functional script. Your mention of separate tokens did put me on the right path, however, and I have a functional script now.

For anyone who may be interested:

#!/usr/bin/env bash

# this script assumes your caddy binary resides in /usr/local/bin, as thats where mine is expected

# list of desired plugins
_plugins=(
    "github.com/caddy-dns/cloudflare"
    "github.com/caddy-dns/desec"
    "github.com/corazawaf/coraza-caddy/v2"
    "github.com/caddyserver/nginx-adapter"
)

echo Using plugins defined in "${BASH_SOURCE[*]}":

_cont=
_plugin=
_with=()

# if plugins are defined, echo them and append an array with needed build flags
if [[ -n "${_plugins[*]}" ]]; then
    for _plugin in "${_plugins[@]}"; do
        echo -e "\t$_plugin"
        _with+=(--with "$_plugin")
    done
else
    # if no plugins defined, ask to exit or continue
    read -pr "No plugins defined, continue? [Y/n]" _cont
    _cont=${_cont,,}
    [[ ! $_cont =~ ^(y| ) ]] || [[ -z $_cont ]] &&
        exit
    unset _cont
fi

# build caddy with optionally defined plugins to ./caddy-x
CGO_ENABLED=0 GOOS=linux \
xcaddy build \
"${_with[@]}" \
--output ./caddy-x

# if ./caddy-x exists and is newer than /usr/local/bin/caddy, replace it
if [[ ./caddy-x -nt /usr/local/bin/caddy ]]; then
    sudo mv ./caddy-x /usr/local/bin/caddy
fi

Thanks for your reply @francislavoie

Cheers!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.