Introduce version command to Authelia to check the version

The version command displays the tag and the commit hash of the
built commit along with the time when the build was done.
This commit is contained in:
Clement Michaud 2019-12-08 16:51:12 +01:00 committed by Clément Michaud
parent 55460035f7
commit b4a8c4f0ec
12 changed files with 97 additions and 31 deletions

View File

@ -3,6 +3,9 @@
# =======================================
FROM golang:1.13-alpine AS builder-backend
ARG BUILD_TAG
ARG BUILD_COMMIT
# gcc and musl-dev are required for building go-sqlite3
RUN apk --no-cache add gcc musl-dev
@ -16,6 +19,13 @@ RUN go mod download
COPY cmd cmd
COPY internal internal
# Set the build version and time
RUN echo "Write tag ${BUILD_TAG} and commit ${BUILD_COMMIT} in binary." && \
BUILD_TIME=`date +"%Y-%m-%d %T"` && \
sed -i "s/__BUILD_TAG__/${BUILD_TAG}/" cmd/authelia/constants.go && \
sed -i "s/__BUILD_COMMIT__/${BUILD_COMMIT}/" cmd/authelia/constants.go && \
sed -i "s/__BUILD_TIME__/${BUILD_TIME}/" cmd/authelia/constants.go
# CGO_ENABLED=1 is mandatory for building go-sqlite3
RUN cd cmd/authelia && GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -tags netgo -ldflags '-w' -o authelia
@ -48,4 +58,4 @@ EXPOSE 9091
VOLUME /etc/authelia
VOLUME /var/lib/authelia
CMD ["./authelia", "-config", "/etc/authelia/configuration.yml"]
CMD ["./authelia", "--config", "/etc/authelia/configuration.yml"]

View File

@ -3,6 +3,9 @@
# =======================================
FROM arm32v7/golang:1.13-alpine AS builder-backend
ARG BUILD_TAG
ARG BUILD_COMMIT
# qemu binary, gcc and musl-dev are required for building go-sqlite3
COPY ./qemu-arm-static /usr/bin/qemu-arm-static
RUN apk --no-cache add gcc musl-dev
@ -17,6 +20,13 @@ RUN go mod download
COPY cmd cmd
COPY internal internal
# Set the build version and time
RUN echo "Write tag ${BUILD_TAG} and commit ${BUILD_COMMIT} in binary." && \
BUILD_TIME=`date +"%Y-%m-%d %T"` && \
sed -i "s/__BUILD_TAG__/${BUILD_TAG}/" cmd/authelia/constants.go && \
sed -i "s/__BUILD_COMMIT__/${BUILD_COMMIT}/" cmd/authelia/constants.go && \
sed -i "s/__BUILD_TIME__/${BUILD_TIME}/" cmd/authelia/constants.go
# CGO_ENABLED=1 is mandatory for building go-sqlite3
RUN cd cmd/authelia && GOOS=linux GOARCH=arm CGO_ENABLED=1 go build -tags netgo -ldflags '-w' -o authelia
@ -52,4 +62,4 @@ EXPOSE 9091
VOLUME /etc/authelia
VOLUME /var/lib/authelia
CMD ["./authelia", "-config", "/etc/authelia/configuration.yml"]
CMD ["./authelia", "--config", "/etc/authelia/configuration.yml"]

View File

@ -3,6 +3,9 @@
# =======================================
FROM arm64v8/golang:1.13-alpine AS builder-backend
ARG BUILD_TAG
ARG BUILD_COMMIT
# qemu binary, gcc and musl-dev are required for building go-sqlite3
COPY ./qemu-aarch64-static /usr/bin/qemu-aarch64-static
RUN apk --no-cache add gcc musl-dev
@ -17,6 +20,13 @@ RUN go mod download
COPY cmd cmd
COPY internal internal
# Set the build version and time
RUN echo "Write tag ${BUILD_TAG} and commit ${BUILD_COMMIT} in binary." && \
BUILD_TIME=`date +"%Y-%m-%d %T"` && \
sed -i "s/__BUILD_TAG__/${BUILD_TAG}/" cmd/authelia/constants.go && \
sed -i "s/__BUILD_COMMIT__/${BUILD_COMMIT}/" cmd/authelia/constants.go && \
sed -i "s/__BUILD_TIME__/${BUILD_TIME}/" cmd/authelia/constants.go
# CGO_ENABLED=1 is mandatory for building go-sqlite3
RUN cd cmd/authelia && GOOS=linux GOARCH=arm64 CGO_ENABLED=1 go build -tags netgo -ldflags '-w' -o authelia
@ -52,4 +62,4 @@ EXPOSE 9091
VOLUME /etc/authelia
VOLUME /var/lib/authelia
CMD ["./authelia", "-config", "/etc/authelia/configuration.yml"]
CMD ["./authelia", "--config", "/etc/authelia/configuration.yml"]

View File

@ -13,6 +13,7 @@ import (
)
var arch string
var supportedArch = []string{"amd64", "arm32v7", "arm64v8"}
var defaultArch = "amd64"
var travisBranch = os.Getenv("TRAVIS_BRANCH")
@ -25,7 +26,6 @@ var tags = dockerTags.FindStringSubmatch(travisTag)
func init() {
DockerBuildCmd.PersistentFlags().StringVar(&arch, "arch", defaultArch, "target architecture among: "+strings.Join(supportedArch, ", "))
DockerPushCmd.PersistentFlags().StringVar(&arch, "arch", defaultArch, "target architecture among: "+strings.Join(supportedArch, ", "))
}
func checkArchIsSupported(arch string) {
@ -75,7 +75,22 @@ func dockerBuildOfficialImage(arch string) error {
}
}
return docker.Build(IntermediateDockerImageName, dockerfile, ".")
gitTag := travisTag
if gitTag == "" {
// If commit is not tagged, mark the build has having unknown tag.
gitTag = "unknown"
}
cmd := utils.Shell("git rev-parse HEAD")
cmd.Stdout = nil
cmd.Stderr = nil
commitBytes, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
commitHash := strings.Trim(string(commitBytes), "\n")
return docker.Build(IntermediateDockerImageName, dockerfile, ".", gitTag, commitHash)
}
// DockerBuildCmd Command for building docker image of Authelia.

View File

@ -11,7 +11,7 @@ import (
// ServeCmd serve authelia with the provided configuration
func ServeCmd(cobraCmd *cobra.Command, args []string) {
log.Infof("Running Authelia with config %s...", args[0])
cmd := utils.CommandWithStdout(OutputDir+"/authelia", "-config", args[0])
cmd := utils.CommandWithStdout(OutputDir+"/authelia", "--config", args[0])
cmd.Env = append(os.Environ(), "PUBLIC_DIR=dist/public_html")
utils.RunCommandUntilCtrlC(cmd)
}

View File

@ -8,8 +8,10 @@ import (
type Docker struct{}
// Build build a docker image
func (d *Docker) Build(tag, dockerfile, target string) error {
return utils.CommandWithStdout("docker", "build", "-t", tag, "-f", dockerfile, target).Run()
func (d *Docker) Build(tag, dockerfile, target, gitTag, gitCommit string) error {
return utils.CommandWithStdout(
"docker", "build", "-t", tag, "-f", dockerfile, "--build-arg",
"BUILD_TAG="+gitTag, "--build-arg", "BUILD_COMMIT="+gitCommit, target).Run()
}
// Tag tag a docker image.

View File

@ -0,0 +1,5 @@
package main
var BuildTag = "__BUILD_TAG__"
var BuildCommit = "__BUILD_COMMIT__"
var BuildTime = "__BUILD_TIME__"

View File

@ -2,7 +2,7 @@ package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
@ -18,33 +18,24 @@ import (
"github.com/clems4ever/authelia/internal/storage"
"github.com/clems4ever/authelia/internal/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func tryExtractConfigPath() (string, error) {
configPtr := flag.String("config", "", "The path to a configuration file.")
flag.Parse()
var configPathFlag string
if *configPtr == "" {
return "", errors.New("No config file path provided")
func startServer() {
if configPathFlag == "" {
log.Fatal(errors.New("No config file path provided"))
}
return *configPtr, nil
}
func main() {
if os.Getenv("ENVIRONMENT") == "dev" {
logging.Logger().Info("===> Authelia is running in development mode. <===")
}
configPath, err := tryExtractConfigPath()
if err != nil {
logging.Logger().Error(err)
}
config, errs := configuration.Read(configPath)
config, errs := configuration.Read(configPathFlag)
if len(errs) > 0 {
for _, err = range errs {
for _, err := range errs {
logging.Logger().Error(err)
}
panic(errors.New("Some errors have been reported"))
@ -109,3 +100,26 @@ func main() {
}
server.StartServer(*config, providers)
}
func main() {
rootCmd := &cobra.Command{
Use: "authelia",
Run: func(cmd *cobra.Command, args []string) {
startServer()
},
}
rootCmd.Flags().StringVar(&configPathFlag, "config", "", "Configuration file")
versionCmd := &cobra.Command{
Use: "version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("build git tag: %s\n", BuildTag)
fmt.Printf("build git commit: %s\n", BuildCommit)
fmt.Printf("build time: %s\n", BuildTime)
},
}
rootCmd.AddCommand(versionCmd)
rootCmd.Execute()
}

View File

@ -8,5 +8,5 @@ repository. All the details are documented there.
When running **Authelia**, you can specify your configuration file by passing
the file path as the first argument of **Authelia**.
$ authelia -config config.custom.yml
$ authelia --config config.custom.yml

View File

@ -27,7 +27,7 @@ either by pulling the Docker image or building distributable version.
## Build and deploy the distributable version
$ authelia-scripts build
$ PUBLIC_DIR=./dist/public_html ./dist/authelia -config /path/to/your/config.yml
$ PUBLIC_DIR=./dist/public_html ./dist/authelia --config /path/to/your/config.yml
## Deploy with Docker

View File

@ -32,11 +32,11 @@ the root of the repo.
# Build it if not done already
$ authelia-scripts build
$ PUBLIC_DIR=./dist/public_html authelia -config /path/to/your/config.yml
$ PUBLIC_DIR=./dist/public_html authelia --config /path/to/your/config.yml
### Deploy With Docker
$ docker run -v /path/to/your/config.yml:/etc/authelia/config.yml -e TZ=Europe/Paris clems4ever/$ $ authelia -config /etc/authelia/config.yml
$ docker run -v /path/to/your/config.yml:/etc/authelia/config.yml -e TZ=Europe/Paris clems4ever/$ $ authelia --config /etc/authelia/config.yml
## On top of Kubernetes

View File

@ -3,10 +3,10 @@
set -e
# Build the binary
go build -o /tmp/authelia/authelia-tmp cmd/authelia/main.go
go build -o /tmp/authelia/authelia-tmp cmd/authelia/*.go
while true;
do
/tmp/authelia/authelia-tmp -config /etc/authelia/configuration.yml
/tmp/authelia/authelia-tmp --config /etc/authelia/configuration.yml
sleep 10
done