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"
|
2021-05-05 14:09:31 +07:00
|
|
|
"time"
|
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"
|
2020-04-05 19:37:21 +07:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/utils"
|
2019-10-30 03:54:47 +07:00
|
|
|
)
|
|
|
|
|
2019-11-07 07:59:24 +07:00
|
|
|
var arch string
|
2019-12-08 22:51:12 +07:00
|
|
|
|
2020-06-05 07:43:19 +07:00
|
|
|
var supportedArch = []string{"amd64", "arm32v7", "arm64v8", "coverage"}
|
2019-11-07 07:59:24 +07:00
|
|
|
var defaultArch = "amd64"
|
2019-12-27 18:07:53 +07:00
|
|
|
var buildkiteQEMU = os.Getenv("BUILDKITE_AGENT_META_DATA_QEMU")
|
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() {
|
|
|
|
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) {
|
|
|
|
for _, a := range supportedArch {
|
|
|
|
if arch == a {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-11-07 07:59:24 +07:00
|
|
|
log.Fatal("Architecture is not supported. Please select one of " + strings.Join(supportedArch, ", ") + ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
func dockerBuildOfficialImage(arch string) error {
|
2019-11-03 19:07:02 +07:00
|
|
|
docker := &Docker{}
|
2020-05-02 12:06:39 +07:00
|
|
|
// Set default Architecture Dockerfile to amd64.
|
2019-11-07 07:59:24 +07:00
|
|
|
dockerfile := "Dockerfile"
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// If not the default value.
|
2019-11-07 07:59:24 +07:00
|
|
|
if arch != defaultArch {
|
|
|
|
dockerfile = fmt.Sprintf("%s.%s", dockerfile, arch)
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:53:20 +07:00
|
|
|
if arch == "arm32v7" || arch == "arm64v8" {
|
2020-05-02 23:20:40 +07:00
|
|
|
if buildkiteQEMU != stringTrue {
|
2019-12-27 18:07:53 +07:00
|
|
|
err := utils.CommandWithStdout("docker", "run", "--rm", "--privileged", "multiarch/qemu-user-static", "--reset", "-p", "yes").Run()
|
|
|
|
if err != nil {
|
2021-05-05 14:09:31 +07:00
|
|
|
log.Fatal(err)
|
2019-12-27 18:07:53 +07:00
|
|
|
}
|
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)
|
2019-11-07 07:59:24 +07:00
|
|
|
checkArchIsSupported(arch)
|
|
|
|
err := dockerBuildOfficialImage(arch)
|
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
|
|
|
// DockerPushCmd Command for pushing Authelia docker image to DockerHub.
|
2019-10-30 03:54:47 +07:00
|
|
|
var DockerPushCmd = &cobra.Command{
|
2019-11-07 07:59:24 +07:00
|
|
|
Use: "push-image",
|
2020-01-17 03:57:44 +07:00
|
|
|
Short: "Publish Authelia docker image to Docker Hub",
|
2019-10-30 03:54:47 +07:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2020-01-17 03:57:44 +07:00
|
|
|
log.Infof("Pushing Docker image %s to Docker Hub...", DockerImageName)
|
2019-11-07 07:59:24 +07:00
|
|
|
checkArchIsSupported(arch)
|
|
|
|
publishDockerImage(arch)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-01-17 03:57:44 +07:00
|
|
|
log.Infof("Pushing Docker manifest of %s to Docker Hub...", DockerImageName)
|
2019-11-07 07:59:24 +07:00
|
|
|
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-03-30 05:17:19 +07:00
|
|
|
func deploy(docker *Docker, tag, registry string) {
|
|
|
|
imageWithTag := registry + "/" + DockerImageName + ":" + tag
|
2019-11-10 01:42:36 +07:00
|
|
|
|
2021-03-30 05:17:19 +07:00
|
|
|
log.Infof("Docker image %s will be deployed on %s", imageWithTag, registry)
|
2019-10-30 03:54:47 +07:00
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
if err := docker.Tag(DockerImageName, imageWithTag); err != nil {
|
|
|
|
log.Fatal(err)
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
if err := docker.Push(imageWithTag); err != nil {
|
|
|
|
log.Fatal(err)
|
2019-11-07 07:59:24 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 05:17:19 +07:00
|
|
|
func deployManifest(docker *Docker, tag, amd64tag, arm32v7tag, arm64v8tag, registry string) {
|
|
|
|
dockerImagePrefix := registry + "/" + DockerImageName + ":"
|
2019-11-10 01:42:36 +07:00
|
|
|
|
2021-03-30 05:17:19 +07:00
|
|
|
log.Infof("Docker manifest %s%s will be deployed on %s", dockerImagePrefix, tag, registry)
|
2019-11-07 07:59:24 +07:00
|
|
|
|
2019-11-08 08:26:54 +07:00
|
|
|
err := docker.Manifest(dockerImagePrefix+tag, dockerImagePrefix+amd64tag, dockerImagePrefix+arm32v7tag, dockerImagePrefix+arm64v8tag)
|
2019-10-30 03:54:47 +07:00
|
|
|
|
|
|
|
if err != nil {
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Fatal(err)
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
2019-11-09 06:57:39 +07:00
|
|
|
|
|
|
|
tags := []string{amd64tag, arm32v7tag, arm64v8tag}
|
|
|
|
|
2021-03-30 05:17:19 +07:00
|
|
|
if registry == dockerhub {
|
|
|
|
for _, t := range tags {
|
|
|
|
log.Infof("Docker removing tag for %s%s on Docker Hub", dockerImagePrefix, t)
|
|
|
|
|
2021-05-05 14:09:31 +07:00
|
|
|
if err := utils.RunFuncWithRetry(3, 10*time.Second, func() (err error) {
|
|
|
|
err = docker.CleanTag(t)
|
|
|
|
return
|
|
|
|
}); err != nil {
|
|
|
|
log.Fatal(err)
|
2021-03-30 05:17:19 +07:00
|
|
|
}
|
2019-11-09 06:57:39 +07:00
|
|
|
}
|
|
|
|
}
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
|
2019-11-07 07:59:24 +07:00
|
|
|
func publishDockerImage(arch string) {
|
|
|
|
docker := &Docker{}
|
|
|
|
|
2021-03-30 05:17:19 +07:00
|
|
|
for _, registry := range registries {
|
|
|
|
switch {
|
|
|
|
case ciTag != "":
|
|
|
|
if len(tags) == 4 {
|
|
|
|
log.Infof("Detected tags: '%s' | '%s' | '%s'", tags[1], tags[2], tags[3])
|
|
|
|
login(docker, registry)
|
|
|
|
deploy(docker, tags[1]+"-"+arch, registry)
|
|
|
|
|
|
|
|
if !ignoredSuffixes.MatchString(ciTag) {
|
|
|
|
deploy(docker, tags[2]+"-"+arch, registry)
|
|
|
|
deploy(docker, tags[3]+"-"+arch, registry)
|
|
|
|
deploy(docker, "latest-"+arch, registry)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Fatal("Docker image will not be published, the specified tag does not conform to the standard")
|
2019-12-10 15:21:54 +07:00
|
|
|
}
|
2021-03-30 05:17:19 +07:00
|
|
|
case ciBranch != masterTag && !publicRepo.MatchString(ciBranch):
|
|
|
|
login(docker, registry)
|
|
|
|
deploy(docker, ciBranch+"-"+arch, registry)
|
|
|
|
case ciBranch != masterTag && publicRepo.MatchString(ciBranch):
|
|
|
|
login(docker, registry)
|
|
|
|
deploy(docker, "PR"+ciPullRequest+"-"+arch, registry)
|
|
|
|
case ciBranch == masterTag && ciPullRequest == stringFalse:
|
|
|
|
login(docker, registry)
|
|
|
|
deploy(docker, "master-"+arch, registry)
|
|
|
|
default:
|
|
|
|
log.Info("Docker image will not be published")
|
2019-11-28 17:58:17 +07:00
|
|
|
}
|
2019-11-07 07:59:24 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func publishDockerManifest() {
|
2019-10-30 03:54:47 +07:00
|
|
|
docker := &Docker{}
|
|
|
|
|
2021-03-30 05:17:19 +07:00
|
|
|
for _, registry := range registries {
|
|
|
|
switch {
|
|
|
|
case ciTag != "":
|
|
|
|
if len(tags) == 4 {
|
|
|
|
log.Infof("Detected tags: '%s' | '%s' | '%s'", tags[1], tags[2], tags[3])
|
|
|
|
login(docker, registry)
|
|
|
|
deployManifest(docker, tags[1], tags[1]+"-amd64", tags[1]+"-arm32v7", tags[1]+"-arm64v8", registry)
|
|
|
|
|
|
|
|
if registry == dockerhub {
|
|
|
|
publishDockerReadme(docker)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ignoredSuffixes.MatchString(ciTag) {
|
|
|
|
deployManifest(docker, tags[2], tags[2]+"-amd64", tags[2]+"-arm32v7", tags[2]+"-arm64v8", registry)
|
|
|
|
deployManifest(docker, tags[3], tags[3]+"-amd64", tags[3]+"-arm32v7", tags[3]+"-arm64v8", registry)
|
|
|
|
deployManifest(docker, "latest", "latest-amd64", "latest-arm32v7", "latest-arm64v8", registry)
|
|
|
|
|
|
|
|
if registry == dockerhub {
|
|
|
|
publishDockerReadme(docker)
|
|
|
|
updateMicroBadger(docker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Fatal("Docker manifest will not be published, the specified tag does not conform to the standard")
|
|
|
|
}
|
|
|
|
case ciBranch != masterTag && !publicRepo.MatchString(ciBranch):
|
|
|
|
login(docker, registry)
|
|
|
|
deployManifest(docker, ciBranch, ciBranch+"-amd64", ciBranch+"-arm32v7", ciBranch+"-arm64v8", registry)
|
|
|
|
case ciBranch != masterTag && publicRepo.MatchString(ciBranch):
|
|
|
|
login(docker, registry)
|
|
|
|
deployManifest(docker, "PR"+ciPullRequest, "PR"+ciPullRequest+"-amd64", "PR"+ciPullRequest+"-arm32v7", "PR"+ciPullRequest+"-arm64v8", registry)
|
|
|
|
case ciBranch == masterTag && ciPullRequest == stringFalse:
|
|
|
|
login(docker, registry)
|
|
|
|
deployManifest(docker, "master", "master-amd64", "master-arm32v7", "master-arm64v8", registry)
|
|
|
|
|
|
|
|
if registry == dockerhub {
|
2020-01-17 03:57:44 +07:00
|
|
|
publishDockerReadme(docker)
|
2019-12-10 15:21:54 +07:00
|
|
|
}
|
2021-03-30 05:17:19 +07:00
|
|
|
default:
|
|
|
|
log.Info("Docker manifest will not be published")
|
2019-11-28 17:58:17 +07:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func updateMicroBadger(docker *Docker) {
|
|
|
|
log.Info("Updating MicroBadger metadata from Docker Hub")
|
|
|
|
|
|
|
|
if err := docker.UpdateMicroBadger(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|