2019-10-30 03:54:47 +07:00
|
|
|
package main
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
import (
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-11-02 21:32:58 +07:00
|
|
|
)
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// Docker a docker object.
|
2019-10-30 03:54:47 +07:00
|
|
|
type Docker struct{}
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// Build build a docker image.
|
2021-06-18 11:35:43 +07:00
|
|
|
func (d *Docker) Build(tag, dockerfile, target, ldflags string) error {
|
2019-12-08 22:51:12 +07:00
|
|
|
return utils.CommandWithStdout(
|
2021-06-18 11:35:43 +07:00
|
|
|
"docker", "build", "-t", tag, "-f", dockerfile,
|
|
|
|
"--build-arg", "LDFLAGS_EXTRA="+ldflags,
|
|
|
|
target).Run()
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tag tag a docker image.
|
|
|
|
func (d *Docker) Tag(image, tag string) error {
|
2019-11-02 21:32:58 +07:00
|
|
|
return utils.CommandWithStdout("docker", "tag", image, tag).Run()
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Login login to the dockerhub registry.
|
2021-03-30 05:17:19 +07:00
|
|
|
func (d *Docker) Login(username, password, registry string) error {
|
|
|
|
return utils.CommandWithStdout("docker", "login", registry, "-u", username, "-p", password).Run()
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push push a docker image to dockerhub.
|
|
|
|
func (d *Docker) Push(tag string) error {
|
2019-11-02 21:32:58 +07:00
|
|
|
return utils.CommandWithStdout("docker", "push", tag).Run()
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
2019-11-07 07:59:24 +07:00
|
|
|
|
|
|
|
// Manifest push a docker manifest to dockerhub.
|
2019-11-09 06:57:39 +07:00
|
|
|
func (d *Docker) Manifest(tag, amd64tag, arm32v7tag, arm64v8tag string) error {
|
2019-11-02 21:32:58 +07:00
|
|
|
err := utils.CommandWithStdout("docker", "manifest", "create", tag, amd64tag, arm32v7tag, arm64v8tag).Run()
|
2019-11-07 07:59:24 +07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
err = utils.CommandWithStdout("docker", "manifest", "annotate", tag, arm32v7tag, "--os", "linux", "--arch", "arm").Run()
|
2019-11-07 07:59:24 +07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
err = utils.CommandWithStdout("docker", "manifest", "annotate", tag, arm64v8tag, "--os", "linux", "--arch", "arm64", "--variant", "v8").Run()
|
2019-11-07 07:59:24 +07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
return utils.CommandWithStdout("docker", "manifest", "push", "--purge", tag).Run()
|
2019-11-09 06:57:39 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// CleanTag remove a tag from dockerhub.
|
2019-11-10 01:42:36 +07:00
|
|
|
func (d *Docker) CleanTag(tag string) error {
|
2019-12-09 05:50:03 +07:00
|
|
|
return utils.CommandWithStdout("bash", "-c", `token=$(curl -fs --retry 3 -H "Content-Type: application/json" -X "POST" -d '{"username": "'$DOCKER_USERNAME'", "password": "'$DOCKER_PASSWORD'"}' https://hub.docker.com/v2/users/login/ | jq -r .token) && curl -fs --retry 3 -o /dev/null -L -X "DELETE" -H "Authorization: JWT $token" https://hub.docker.com/v2/repositories/`+DockerImageName+"/tags/"+tag+"/").Run()
|
2019-11-10 01:42:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// PublishReadme push README.md to dockerhub.
|
|
|
|
func (d *Docker) PublishReadme() error {
|
2021-01-25 06:05:28 +07:00
|
|
|
return utils.CommandWithStdout("bash", "-c", `token=$(curl -fs --retry 3 -H "Content-Type: application/json" -X "POST" -d '{"username": "'$DOCKER_USERNAME'", "password": "'$DOCKER_PASSWORD'"}' https://hub.docker.com/v2/users/login/ | jq -r .token) && jq -n --arg msg "$(cat README.md | sed -r 's/(\<img\ src\=\")(\.\/)/\1https:\/\/github.com\/authelia\/authelia\/raw\/master\//' | sed 's/\.\//https:\/\/github.com\/authelia\/authelia\/blob\/master\//g' | sed '/start \[contributing\]/ a <a href="https://github.com/authelia/authelia/graphs/contributors"><img src="https://opencollective.com/authelia-sponsors/contributors.svg?width=890" /></a>' | sed '/Thanks goes to/,/### Backers/{/### Backers/!d}')" '{"registry":"registry-1.docker.io","full_description": $msg }' | curl -fs --retry 3 -o /dev/null -L -X "PATCH" -H "Content-Type: application/json" -H "Authorization: JWT $token" -d @- https://hub.docker.com/v2/repositories/authelia/authelia/`).Run()
|
2019-11-09 06:57:39 +07:00
|
|
|
}
|