2019-10-29 20:54:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2021-06-18 04:35:43 +00:00
|
|
|
"strings"
|
2021-09-26 02:08:47 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-10-29 20:54:47 +00:00
|
|
|
"github.com/spf13/cobra"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-10-29 20:54:47 +00:00
|
|
|
)
|
|
|
|
|
2021-09-16 12:39:18 +00:00
|
|
|
func buildAutheliaBinary(xflags []string, buildkite bool) {
|
2021-09-26 02:08:47 +00:00
|
|
|
if buildkite {
|
|
|
|
var wg sync.WaitGroup
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-09-26 02:08:47 +00:00
|
|
|
s := time.Now()
|
2021-09-16 12:39:18 +00:00
|
|
|
|
2021-09-26 02:08:47 +00:00
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
cmd := utils.CommandWithStdout("gox", "-output={{.Dir}}-{{.OS}}-{{.Arch}}-musl", "-buildmode=pie", "-trimpath", "-cgo", "-ldflags=-linkmode=external -s -w "+strings.Join(xflags, " "), "-osarch=linux/amd64 linux/arm linux/arm64", "./cmd/authelia/")
|
|
|
|
|
|
|
|
cmd.Env = append(os.Environ(),
|
|
|
|
"CGO_CPPFLAGS=-D_FORTIFY_SOURCE=2 -fstack-protector-strong", "CGO_LDFLAGS=-Wl,-z,relro,-z,now",
|
|
|
|
"GOX_LINUX_ARM_CC=arm-linux-musleabihf-gcc", "GOX_LINUX_ARM64_CC=aarch64-linux-musl-gcc")
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
cmd := utils.CommandWithStdout("bash", "-c", "docker run --rm -e GOX_LINUX_ARM_CC=arm-linux-gnueabihf-gcc -e GOX_LINUX_ARM64_CC=aarch64-linux-gnu-gcc -e GOX_FREEBSD_AMD64_CC=x86_64-pc-freebsd13-gcc -v ${PWD}:/workdir -v /buildkite/.go:/root/go authelia/crossbuild "+
|
|
|
|
"gox -output={{.Dir}}-{{.OS}}-{{.Arch}} -buildmode=pie -trimpath -cgo -ldflags=\"-linkmode=external -s -w "+strings.Join(xflags, " ")+"\" -osarch=\"linux/amd64 linux/arm linux/arm64 freebsd/amd64\" ./cmd/authelia/")
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
e := time.Since(s)
|
|
|
|
|
|
|
|
log.Debugf("Binary compilation completed in %s.", e)
|
|
|
|
} else {
|
|
|
|
cmd := utils.CommandWithStdout("go", "build", "-buildmode=pie", "-trimpath", "-o", OutputDir+"/authelia", "-ldflags", "-linkmode=external -s -w "+strings.Join(xflags, " "), "./cmd/authelia/")
|
2021-09-16 12:39:18 +00:00
|
|
|
|
|
|
|
cmd.Env = append(os.Environ(),
|
2021-09-26 02:08:47 +00:00
|
|
|
"CGO_CPPFLAGS=-D_FORTIFY_SOURCE=2 -fstack-protector-strong", "CGO_LDFLAGS=-Wl,-z,relro,-z,now")
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2021-09-26 02:08:47 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 23:14:52 +00:00
|
|
|
func buildFrontend(branch string) {
|
2021-10-08 04:00:06 +00:00
|
|
|
cmd := utils.CommandWithStdout("pnpm", "install")
|
2020-05-02 16:20:40 +00:00
|
|
|
cmd.Dir = webDirectory
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2021-01-03 04:28:46 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Fatal(err)
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 23:14:52 +00:00
|
|
|
if !strings.HasPrefix(branch, "renovate/") {
|
|
|
|
cmd = utils.CommandWithStdout("pnpm", "build")
|
|
|
|
cmd.Dir = webDirectory
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-10-08 04:00:06 +00:00
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = utils.CommandWithStdout("bash", "-c", "sed -i -e 's/{{.[a-zA-Z]*}}/\"&\"/g' internal/server/public_html/index.html")
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2021-10-06 23:14:52 +00:00
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
2021-01-03 04:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildSwagger() {
|
2021-09-26 02:25:14 +00:00
|
|
|
swaggerVer := "3.52.3"
|
2021-01-03 04:28:46 +00:00
|
|
|
cmd := utils.CommandWithStdout("bash", "-c", "wget -q https://github.com/swagger-api/swagger-ui/archive/v"+swaggerVer+".tar.gz -O ./v"+swaggerVer+".tar.gz")
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-02-21 23:07:06 +00:00
|
|
|
cmd = utils.CommandWithStdout("cp", "-r", "api", "internal/server/public_html")
|
2021-01-03 04:28:46 +00:00
|
|
|
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-02-21 23:07:06 +00:00
|
|
|
cmd = utils.CommandWithStdout("tar", "-C", "internal/server/public_html/api", "--exclude=index.html", "--strip-components=2", "-xf", "v"+swaggerVer+".tar.gz", "swagger-ui-"+swaggerVer+"/dist")
|
2021-01-03 04:28:46 +00:00
|
|
|
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = utils.CommandWithStdout("rm", "./v"+swaggerVer+".tar.gz")
|
|
|
|
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
2020-04-28 14:07:20 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-21 23:07:06 +00:00
|
|
|
func cleanAssets() {
|
|
|
|
if err := os.Rename("internal/server/public_html", OutputDir+"/public_html"); err != nil {
|
2021-01-03 04:28:46 +00:00
|
|
|
log.Fatal(err)
|
2020-04-28 14:07:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 23:07:06 +00:00
|
|
|
cmd := utils.CommandWithStdout("mkdir", "-p", "internal/server/public_html/api")
|
2020-06-17 06:25:35 +00:00
|
|
|
|
2021-02-21 23:07:06 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
2021-01-03 04:28:46 +00:00
|
|
|
log.Fatal(err)
|
2020-06-17 06:25:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 23:07:06 +00:00
|
|
|
cmd = utils.CommandWithStdout("bash", "-c", "touch internal/server/public_html/{index.html,api/index.html,api/openapi.yml}")
|
2020-04-28 14:07:20 +00:00
|
|
|
|
2021-02-21 23:07:06 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Fatal(err)
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Build build Authelia.
|
2019-10-29 20:54:47 +00:00
|
|
|
func Build(cobraCmd *cobra.Command, args []string) {
|
2021-09-26 02:08:47 +00:00
|
|
|
buildkite, _ := cobraCmd.Flags().GetBool("buildkite")
|
2021-10-06 22:45:37 +00:00
|
|
|
branch := os.Getenv("BUILDKITE_BRANCH")
|
|
|
|
|
|
|
|
if strings.HasPrefix(branch, "renovate/") {
|
2021-10-06 23:14:52 +00:00
|
|
|
buildFrontend(branch)
|
2021-10-06 22:45:37 +00:00
|
|
|
log.Info("Skip building Authelia for deps...")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Building Authelia...")
|
2021-09-26 02:08:47 +00:00
|
|
|
|
2019-10-29 20:54:47 +00:00
|
|
|
Clean(cobraCmd, args)
|
|
|
|
|
2021-10-06 22:45:37 +00:00
|
|
|
xflags, err := getXFlags(branch, os.Getenv("BUILDKITE_BUILD_NUMBER"), "")
|
2021-06-18 04:35:43 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Debug("Creating `" + OutputDir + "` directory")
|
2021-06-18 04:35:43 +00:00
|
|
|
err = os.MkdirAll(OutputDir, os.ModePerm)
|
2019-10-29 20:54:47 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-01-03 04:28:46 +00:00
|
|
|
log.Fatal(err)
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Debug("Building Authelia frontend...")
|
2021-10-06 23:14:52 +00:00
|
|
|
buildFrontend(branch)
|
2020-04-28 14:07:20 +00:00
|
|
|
|
2021-01-03 04:28:46 +00:00
|
|
|
log.Debug("Building swagger-ui frontend...")
|
|
|
|
buildSwagger()
|
|
|
|
|
2021-06-25 11:53:20 +00:00
|
|
|
if buildkite {
|
2021-09-16 12:39:18 +00:00
|
|
|
log.Debug("Building Authelia Go binaries with gox...")
|
2021-06-25 11:53:20 +00:00
|
|
|
} else {
|
|
|
|
log.Debug("Building Authelia Go binary...")
|
|
|
|
}
|
|
|
|
|
2021-09-16 12:39:18 +00:00
|
|
|
buildAutheliaBinary(xflags, buildkite)
|
|
|
|
|
2021-02-21 23:07:06 +00:00
|
|
|
cleanAssets()
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|