2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 19:08:55 +00:00

Update prepare-release

This commit is contained in:
Alexander Neumann 2018-10-13 13:05:13 +02:00
parent e64a0e0454
commit c5ec4efe91
2 changed files with 91 additions and 47 deletions

View File

@ -9,7 +9,6 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"sort"
"strings" "strings"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -25,8 +24,7 @@ var opts = struct {
IgnoreChangelogCurrent bool IgnoreChangelogCurrent bool
IgnoreDockerBuildGoVersion bool IgnoreDockerBuildGoVersion bool
tarFilename string OutputDir string
buildDir string
}{} }{}
var versionRegex = regexp.MustCompile(`^\d+\.\d+\.\d+$`) var versionRegex = regexp.MustCompile(`^\d+\.\d+\.\d+$`)
@ -38,6 +36,9 @@ func init() {
pflag.BoolVar(&opts.IgnoreChangelogReleaseDate, "ignore-changelog-release-date", false, "ignore missing subdir with date in changelog/") pflag.BoolVar(&opts.IgnoreChangelogReleaseDate, "ignore-changelog-release-date", false, "ignore missing subdir with date in changelog/")
pflag.BoolVar(&opts.IgnoreChangelogCurrent, "ignore-changelog-current", false, "ignore check if CHANGELOG.md is up to date") pflag.BoolVar(&opts.IgnoreChangelogCurrent, "ignore-changelog-current", false, "ignore check if CHANGELOG.md is up to date")
pflag.BoolVar(&opts.IgnoreDockerBuildGoVersion, "ignore-docker-build-go-version", false, "ignore check if docker builder go version is up to date") pflag.BoolVar(&opts.IgnoreDockerBuildGoVersion, "ignore-docker-build-go-version", false, "ignore check if docker builder go version is up to date")
pflag.StringVar(&opts.OutputDir, "output-dir", "", "use `dir` as output directory")
pflag.Parse() pflag.Parse()
} }
@ -251,6 +252,7 @@ func preCheckDockerBuilderGoVersion() {
} }
localVersion := strings.TrimSpace(string(buf)) localVersion := strings.TrimSpace(string(buf))
msg("update docker container restic/builder")
run("docker", "pull", "restic/builder") run("docker", "pull", "restic/builder")
buf, err = exec.Command("docker", "run", "--rm", "restic/builder", "go", "version").Output() buf, err = exec.Command("docker", "run", "--rm", "restic/builder", "go", "version").Output()
if err != nil { if err != nil {
@ -318,62 +320,92 @@ func addTag() {
run("git", "tag", "-a", "-s", "-m", tagname, tagname) run("git", "tag", "-a", "-s", "-m", tagname, tagname)
} }
func exportTar() { func exportTar(version, tarFilename string) {
cmd := fmt.Sprintf("git archive --format=tar --prefix=restic-%s/ v%s | gzip -n > %s", cmd := fmt.Sprintf("git archive --format=tar --prefix=restic-%s/ v%s | gzip -n > %s",
opts.Version, opts.Version, opts.tarFilename) version, version, tarFilename)
run("sh", "-c", cmd) run("sh", "-c", cmd)
msg("build restic-%s.tar.gz", opts.Version) msg("build restic-%s.tar.gz", version)
} }
func runBuild() { func extractTar(filename, outputDir string) {
msg("building binaries...") msg("extract tar into %v", outputDir)
run("docker", "run", "--rm", "--volume", getwd()+":/home/build", "restic/builder", "build.sh", opts.tarFilename) c := exec.Command("tar", "xz", "--strip-components=1", "-f", filename)
} c.Stdout = os.Stdout
c.Stderr = os.Stderr
func findBuildDir() string { c.Dir = outputDir
nameRegex := regexp.MustCompile(`restic-` + opts.Version + `-\d{8}-\d{6}`) err := c.Run()
f, err := os.Open(".")
if err != nil { if err != nil {
die("Open(.): %v", err) die("error extracting tar: %v", err)
}
}
func runBuild(sourceDir, outputDir string) {
msg("building binaries...")
run("docker", "run", "--rm",
"--volume", sourceDir+":/restic",
"--volume", outputDir+":/output",
"restic/builder")
}
func readdir(dir string) []string {
fis, err := ioutil.ReadDir(dir)
if err != nil {
die("readdir %v failed: %v", dir, err)
} }
entries, err := f.Readdirnames(-1) filenames := make([]string, 0, len(fis))
for _, fi := range fis {
filenames = append(filenames, fi.Name())
}
return filenames
}
func sha256sums(inputDir, outputFile string) {
msg("runnnig sha256sum in %v", inputDir)
filenames := readdir(inputDir)
f, err := os.Create(outputFile)
if err != nil { if err != nil {
die("Readdirnames(): %v", err) die("unable to create %v: %v", outputFile, err)
}
c := exec.Command("sha256sum", filenames...)
c.Stdout = f
c.Stderr = os.Stderr
c.Dir = inputDir
err = c.Run()
if err != nil {
die("error running sha256sums: %v", err)
} }
err = f.Close() err = f.Close()
if err != nil { if err != nil {
die("Close(): %v", err) die("close %v: %v", outputFile, err)
} }
sort.Slice(entries, func(i, j int) bool {
return entries[j] < entries[i]
})
for _, entry := range entries {
if nameRegex.MatchString(entry) {
msg("found restic build dir: %v", entry)
return entry
}
}
die("restic build dir not found")
return ""
} }
func signFiles() { func signFiles(filenames ...string) {
run("gpg", "--armor", "--detach-sign", filepath.Join(opts.buildDir, "SHA256SUMS")) for _, filename := range filenames {
run("gpg", "--armor", "--detach-sign", filepath.Join(opts.buildDir, opts.tarFilename)) run("gpg", "--armor", "--detach-sign", filename)
}
} }
func updateDocker() { func updateDocker(outputDir, version string) {
cmd := fmt.Sprintf("bzcat %s/restic_%s_linux_amd64.bz2 > restic", opts.buildDir, opts.Version) cmd := fmt.Sprintf("bzcat %s/restic_%s_linux_amd64.bz2 > restic", outputDir, version)
run("sh", "-c", cmd) run("sh", "-c", cmd)
run("chmod", "+x", "restic") run("chmod", "+x", "restic")
run("docker", "build", "--rm", "--tag", "restic/restic:latest", "-f", "docker/Dockerfile", ".") run("docker", "build", "--rm", "--tag", "restic/restic:latest", "-f", "docker/Dockerfile", ".")
run("docker", "tag", "restic/restic:latest", "restic/restic:"+opts.Version) run("docker", "tag", "restic/restic:latest", "restic/restic:"+version)
}
func tempdir(prefix string) string {
dir, err := ioutil.TempDir(getwd(), prefix)
if err != nil {
die("unable to create temp dir %q: %v", prefix, err)
}
return dir
} }
func main() { func main() {
@ -386,8 +418,6 @@ func main() {
die("invalid new version") die("invalid new version")
} }
opts.tarFilename = fmt.Sprintf("restic-%s.tar.gz", opts.Version)
preCheckBranchMaster() preCheckBranchMaster()
preCheckUncommittedChanges() preCheckUncommittedChanges()
preCheckVersionExists() preCheckVersionExists()
@ -396,19 +426,33 @@ func main() {
preCheckChangelogCurrent() preCheckChangelogCurrent()
preCheckChangelogVersion() preCheckChangelogVersion()
if opts.OutputDir == "" {
opts.OutputDir = tempdir("build-output-")
}
sourceDir := tempdir("source-")
msg("using output dir %v", opts.OutputDir)
msg("using source dir %v", sourceDir)
generateFiles() generateFiles()
updateVersion() updateVersion()
addTag() addTag()
updateVersionDev() updateVersionDev()
exportTar() tarFilename := filepath.Join(opts.OutputDir, fmt.Sprintf("restic-%s.tar.gz", opts.Version))
runBuild() exportTar(opts.Version, tarFilename)
opts.buildDir = findBuildDir()
signFiles()
updateDocker() extractTar(tarFilename, sourceDir)
runBuild(sourceDir, opts.OutputDir)
rmdir(sourceDir)
msg("done, build dir is %v", opts.buildDir) sha256sums(opts.OutputDir, filepath.Join(opts.OutputDir, "SHA256SUMS"))
signFiles(filepath.Join(opts.OutputDir, "SHA256SUMS"), tarFilename)
updateDocker(opts.OutputDir, opts.Version)
msg("done, output dir is %v", opts.OutputDir)
msg("now run:\n\ngit push --tags origin master\ndocker push restic/restic\n") msg("now run:\n\ngit push --tags origin master\ndocker push restic/restic\n")
} }