2019-10-30 03:54:47 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-11-22 09:02:06 +07:00
|
|
|
"regexp"
|
2019-11-07 07:59:24 +07:00
|
|
|
"strings"
|
2019-10-30 03:54:47 +07:00
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-10-30 03:54:47 +07:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2021-09-16 19:39:18 +07:00
|
|
|
var container string
|
2019-12-08 22:51:12 +07:00
|
|
|
|
2021-09-16 19:39:18 +07:00
|
|
|
var containers = []string{"dev", "coverage"}
|
|
|
|
var defaultContainer = "dev"
|
2020-01-17 03:57:44 +07:00
|
|
|
var ciBranch = os.Getenv("BUILDKITE_BRANCH")
|
|
|
|
var ciPullRequest = os.Getenv("BUILDKITE_PULL_REQUEST")
|
|
|
|
var ciTag = os.Getenv("BUILDKITE_TAG")
|
2019-12-10 15:21:54 +07:00
|
|
|
var dockerTags = regexp.MustCompile(`v(?P<Patch>(?P<Minor>(?P<Major>\d+)\.\d+)\.\d+.*)`)
|
2019-11-28 17:58:17 +07:00
|
|
|
var ignoredSuffixes = regexp.MustCompile("alpha|beta")
|
2021-03-30 05:17:19 +07:00
|
|
|
var publicRepo = regexp.MustCompile(`.*:.*`)
|
2020-01-17 03:57:44 +07:00
|
|
|
var tags = dockerTags.FindStringSubmatch(ciTag)
|
2019-11-07 07:59:24 +07:00
|
|
|
|
|
|
|
func init() {
|
2021-09-16 19:39:18 +07:00
|
|
|
DockerBuildCmd.PersistentFlags().StringVar(&container, "container", defaultContainer, "target container among: "+strings.Join(containers, ", "))
|
2019-11-07 07:59:24 +07:00
|
|
|
}
|
|
|
|
|
2021-09-16 19:39:18 +07:00
|
|
|
func checkContainerIsSupported(container string) {
|
|
|
|
for _, v := range containers {
|
|
|
|
if container == v {
|
2019-11-07 07:59:24 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-09-16 19:39:18 +07:00
|
|
|
log.Fatal("Container is not supported. Please select one of " + strings.Join(containers, ", ") + ".")
|
2019-11-07 07:59:24 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func dockerBuildOfficialImage(arch string) error {
|
2019-11-03 19:07:02 +07:00
|
|
|
docker := &Docker{}
|
2021-09-16 19:39:18 +07:00
|
|
|
filename := "Dockerfile"
|
|
|
|
dockerfile := fmt.Sprintf("%s.%s", filename, arch)
|
2019-11-07 07:59:24 +07:00
|
|
|
|
2021-06-18 11:35:43 +07:00
|
|
|
flags, err := getXFlags(ciBranch, os.Getenv("BUILDKITE_BUILD_NUMBER"), "")
|
2019-12-08 22:51:12 +07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-06-18 11:35:43 +07:00
|
|
|
return docker.Build(IntermediateDockerImageName, dockerfile, ".",
|
|
|
|
strings.Join(flags, " "))
|
2019-11-03 19:07:02 +07:00
|
|
|
}
|
|
|
|
|
2019-10-30 03:54:47 +07:00
|
|
|
// DockerBuildCmd Command for building docker image of Authelia.
|
|
|
|
var DockerBuildCmd = &cobra.Command{
|
|
|
|
Use: "build",
|
|
|
|
Short: "Build the docker image of Authelia",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Infof("Building Docker image %s...", DockerImageName)
|
2021-09-16 19:39:18 +07:00
|
|
|
checkContainerIsSupported(container)
|
|
|
|
err := dockerBuildOfficialImage(container)
|
2019-11-03 19:07:02 +07:00
|
|
|
|
2019-10-30 03:54:47 +07:00
|
|
|
if err != nil {
|
2019-11-03 19:07:02 +07:00
|
|
|
log.Fatal(err)
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:07:02 +07:00
|
|
|
docker := &Docker{}
|
2019-10-30 03:54:47 +07:00
|
|
|
err = docker.Tag(IntermediateDockerImageName, DockerImageName)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-05-05 14:09:31 +07:00
|
|
|
log.Fatal(err)
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// DockerManifestCmd Command for pushing Authelia docker manifest to DockerHub.
|
2019-11-07 07:59:24 +07:00
|
|
|
var DockerManifestCmd = &cobra.Command{
|
|
|
|
Use: "push-manifest",
|
2020-01-17 03:57:44 +07:00
|
|
|
Short: "Publish Authelia docker manifest to Docker Hub",
|
2019-11-07 07:59:24 +07:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
publishDockerManifest()
|
2019-10-30 03:54:47 +07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-30 05:17:19 +07:00
|
|
|
func login(docker *Docker, registry string) {
|
|
|
|
username := ""
|
|
|
|
password := ""
|
|
|
|
|
|
|
|
switch registry {
|
|
|
|
case dockerhub:
|
|
|
|
username = os.Getenv("DOCKER_USERNAME")
|
|
|
|
password = os.Getenv("DOCKER_PASSWORD")
|
|
|
|
case ghcr:
|
|
|
|
username = os.Getenv("GHCR_USERNAME")
|
|
|
|
password = os.Getenv("GHCR_PASSWORD")
|
|
|
|
}
|
2019-10-30 03:54:47 +07:00
|
|
|
|
|
|
|
if username == "" {
|
2021-03-30 05:17:19 +07:00
|
|
|
log.Fatal(errors.New("DOCKER_USERNAME/GHCR_USERNAME is empty"))
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if password == "" {
|
2021-03-30 05:17:19 +07:00
|
|
|
log.Fatal(errors.New("DOCKER_PASSWORD/GHCR_PASSWORD is empty"))
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
|
2021-03-30 05:17:19 +07:00
|
|
|
log.Infof("Login to %s as %s", registry, username)
|
|
|
|
err := docker.Login(username, password, registry)
|
2019-10-30 03:54:47 +07:00
|
|
|
|
|
|
|
if err != nil {
|
2021-03-30 05:17:19 +07:00
|
|
|
log.Fatalf("Login to %s failed: %s", registry, err)
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 19:39:18 +07:00
|
|
|
func deployManifest(docker *Docker, tag string) {
|
|
|
|
log.Infof("Docker manifest %s:%s will be deployed on %s and %s", DockerImageName, tag, dockerhub, ghcr)
|
2019-11-10 01:42:36 +07:00
|
|
|
|
2021-09-16 19:39:18 +07:00
|
|
|
dockerhub := dockerhub + "/" + DockerImageName + ":" + tag
|
|
|
|
ghcr := ghcr + "/" + DockerImageName + ":" + tag
|
2019-10-30 03:54:47 +07:00
|
|
|
|
2021-09-16 19:39:18 +07:00
|
|
|
if err := docker.Manifest(dockerhub, ghcr); err != nil {
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Fatal(err)
|
2019-11-07 07:59:24 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func publishDockerManifest() {
|
2019-10-30 03:54:47 +07:00
|
|
|
docker := &Docker{}
|
|
|
|
|
2021-09-16 19:39:18 +07:00
|
|
|
switch {
|
|
|
|
case ciTag != "":
|
|
|
|
if len(tags) == 4 {
|
|
|
|
log.Infof("Detected tags: '%s' | '%s' | '%s'", tags[1], tags[2], tags[3])
|
|
|
|
login(docker, dockerhub)
|
|
|
|
login(docker, ghcr)
|
|
|
|
deployManifest(docker, tags[1])
|
|
|
|
publishDockerReadme(docker)
|
|
|
|
|
|
|
|
if !ignoredSuffixes.MatchString(ciTag) {
|
|
|
|
deployManifest(docker, tags[2])
|
|
|
|
deployManifest(docker, tags[3])
|
|
|
|
deployManifest(docker, "latest")
|
2020-01-17 03:57:44 +07:00
|
|
|
publishDockerReadme(docker)
|
2019-12-10 15:21:54 +07:00
|
|
|
}
|
2021-09-16 19:39:18 +07:00
|
|
|
} else {
|
|
|
|
log.Fatal("Docker manifest will not be published, the specified tag does not conform to the standard")
|
2019-11-28 17:58:17 +07:00
|
|
|
}
|
2021-09-16 19:39:18 +07:00
|
|
|
case ciBranch != masterTag && !publicRepo.MatchString(ciBranch):
|
|
|
|
login(docker, dockerhub)
|
|
|
|
login(docker, ghcr)
|
|
|
|
deployManifest(docker, ciBranch)
|
|
|
|
case ciBranch != masterTag && publicRepo.MatchString(ciBranch):
|
|
|
|
login(docker, dockerhub)
|
|
|
|
login(docker, ghcr)
|
|
|
|
deployManifest(docker, "PR"+ciPullRequest)
|
|
|
|
case ciBranch == masterTag && ciPullRequest == stringFalse:
|
|
|
|
login(docker, dockerhub)
|
|
|
|
login(docker, ghcr)
|
|
|
|
deployManifest(docker, "master")
|
|
|
|
publishDockerReadme(docker)
|
|
|
|
default:
|
|
|
|
log.Info("Docker manifest will not be published")
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
}
|
2020-01-17 03:57:44 +07:00
|
|
|
|
|
|
|
func publishDockerReadme(docker *Docker) {
|
|
|
|
log.Info("Docker pushing README.md to Docker Hub")
|
|
|
|
|
|
|
|
if err := docker.PublishReadme(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|