# Makefile for releasing CoreDNS
#
# The release is controlled from coremain/version.go. The version found there is
# used to tag the git repo and to build the assets that are uploaded to GitHub.
#
# The release should be accompanied by release notes in the notes/ subdirectory.
# These are published on coredns.io. For example see: notes/coredns-1.5.1.md
# Use make -f Makefile.release notes to create a skeleton notes document.
#
# Be sure to prune the PR list a bit, not everything is worthy!
#
# As seen in notes/coredns-1.5.1.md we want to style the notes in the following manner:
#
# * important changes at the top
# * people who committed/review code (the latter is harder to get)
# * Slightly abbreviated list of pull requests merged for this release.
#
# Steps to release, first:
#
# 1. Up the version in coremain/version.go
# 2. Do a make -f Makefile.doc # This has been automated in GitHub, so you can probably skip this step
# 3. go generate
# 4. Send PR to get this merged.
#
# Then:
#
# 1. Open an issue for this release
# 2. In an issue give the command: /release master VERSION
#    Where VERSION is the version of the release - the release script double checks this with the
#    actual CoreDNS version in coremain/version.go
# 3. (to test as release /release -t master VERSION can be used.
#
# See https://github.com/coredns/release for documentation README on what needs to be setup for this to be
# automated (can still be done by hand if needed). Especially what environment variables need to be
# set! This further depends on Caddy being setup and [dreck](https://github.com/miekg/dreck) running as a plugin in Caddy.
#
# To release we run, these target from the this Makefile.release ordered like:
# * make release
# * make docker
# * make github-push
# * make docker-push
#
# Testing this is hard-ish as you don't want to accidentially release a coredns. If not executing the github-push target
# and using a non-coredns docker repo you should be fine.
# Testing docker is done e.g. via:
#
#  export DOCKER_PASSWORD=<pass>
# export DOCKER_LOGIN=miek
# make DOCKER=miek -f Makefile.release build docker-build docker-push

EMPTY:=
SPACE:=$(EMPTY) $(EMPTY)
COMMA:=$(EMPTY),$(EMPTY)

ifeq (, $(shell which curl))
    $(error "No curl in $$PATH, please install")
endif

# DOCKER is the docker image repo we need to push to.
DOCKER:=
NAME:=coredns
VERSION:=$(shell grep 'CoreVersion' coremain/version.go | awk '{ print $$3 }' | tr -d '"')
GITHUB:=coredns
# mips is not in LINUX_ARCH because it's not supported by docker manifest
LINUX_ARCH:=amd64 arm arm64 mips64le ppc64le s390x
DOCKER_IMAGE_NAME:=$(DOCKER)/$(NAME)
PLATFORMS:=$(subst $(SPACE),$(COMMA),$(foreach arch,$(LINUX_ARCH),linux/$(arch)))
DOCKER_IMAGE_LIST_VERSIONED:=$(shell echo $(LINUX_ARCH) | sed -e "s~[^ ]*~$(DOCKER_IMAGE_NAME)\-&:$(VERSION)~g")
DOCKER_IMAGE_LIST_LATEST:=$(shell echo $(LINUX_ARCH) | sed -e "s~[^ ]*~$(DOCKER_IMAGE_NAME)\-&:latest~g")

all:
	@echo Use the 'release' target to build a release, 'docker' for docker build.

release: build tar

docker: docker-build

.PHONY: build
build:
	@go version
	@echo Cleaning old builds
	@rm -rf build && mkdir build
	@echo Building: darwin/amd64 - $(VERSION)
	mkdir -p build/darwin/amd64 && $(MAKE) coredns BINARY=build/darwin/amd64/$(NAME) SYSTEM="GOOS=darwin GOARCH=amd64" CHECKS="" BUILDOPTS=""
	@echo Building: windows/amd64 - $(VERSION)
	mkdir -p build/windows/amd64 && $(MAKE) coredns BINARY=build/windows/amd64/$(NAME).exe SYSTEM="GOOS=windows GOARCH=amd64" CHECKS="" BUILDOPTS=""
	@echo Building: linux/mips - $(VERSION)
	mkdir -p build/linux/mips  && $(MAKE) coredns BINARY=build/linux/mips/$(NAME) SYSTEM="GOOS=linux GOARCH=mips" CHECKS="" BUILDOPTS=""
	@echo Building: linux/$(LINUX_ARCH) - $(VERSION) ;\
	for arch in $(LINUX_ARCH); do \
	    mkdir -p build/linux/$$arch  && $(MAKE) coredns BINARY=build/linux/$$arch/$(NAME) SYSTEM="GOOS=linux GOARCH=$$arch" CHECKS="" BUILDOPTS="" ;\
	done

.PHONY: tar
tar:
	@echo Cleaning old releases
	@rm -rf release && mkdir release
	tar -zcf release/$(NAME)_$(VERSION)_darwin_amd64.tgz -C build/darwin/amd64 $(NAME)
	tar -zcf release/$(NAME)_$(VERSION)_windows_amd64.tgz -C build/windows/amd64 $(NAME).exe
	tar -zcf release/$(NAME)_$(VERSION)_linux_mips.tgz -C build/linux/mips $(NAME)
	tar -zcf release/$(NAME)_$(VERSION)_linux_mips64le.tgz -C build/linux/mips64le $(NAME)
	for arch in $(LINUX_ARCH); do \
	    tar -zcf release/$(NAME)_$(VERSION)_linux_$$arch.tgz -C build/linux/$$arch $(NAME) ;\
	done

.PHONY: github-push
github-push:
ifeq ($(GITHUB_ACCESS_TOKEN),)
	$(error "Please set the GITHUB_ACCESS_TOKEN environment variable")
else
	@echo Releasing: $(VERSION)
	@$(eval RELEASE:=$(shell curl -s -d '{"tag_name": "v$(VERSION)", "name": "v$(VERSION)"}' -H "Authorization: token ${GITHUB_ACCESS_TOKEN}" "https://api.github.com/repos/$(GITHUB)/$(NAME)/releases" | grep -m 1 '"id"' | tr -cd '[[:digit:]]'))
	@echo ReleaseID: $(RELEASE)
	@( cd release; for asset in `ls -A *tgz`; do \
	    echo $$asset; \
	    curl -o /dev/null -X POST \
	      -H "Content-Type: application/gzip" \
	      -H "Authorization: token ${GITHUB_ACCESS_TOKEN}" \
	      --data-binary "@$$asset" \
	      "https://uploads.github.com/repos/$(GITHUB)/$(NAME)/releases/$(RELEASE)/assets?name=$${asset}" ; \
	done )
	@( cd release; for asset in `ls -A *tgz`; do \
	    sha256sum $$asset > $$asset.sha256; \
	done )
	@( cd release; for asset in `ls -A *sha256`; do \
	    echo $$asset; \
	    curl -o /dev/null -X POST \
	      -H "Content-Type: text/plain" \
	      -H "Authorization: token ${GITHUB_ACCESS_TOKEN}" \
	      --data-binary "@$$asset" \
	      "https://uploads.github.com/repos/$(GITHUB)/$(NAME)/releases/$(RELEASE)/assets?name=$${asset}" ; \
	done )
endif

.PHONY: docker-build
docker-build: tar
ifeq ($(DOCKER),)
	$(error "Please specify Docker registry to use. Use DOCKER=coredns for releases")
else
	@# Steps:
	@# 1. Copy appropriate coredns binary to build/docker/linux/<arch>
	@# 2. Copy Dockerfile to build/docker/linux/<arch>
	@rm -rf build/docker
	for arch in $(LINUX_ARCH); do \
	    mkdir -p build/docker/linux/$${arch} ;\
	    tar -xzf release/$(NAME)_$(VERSION)_linux_$${arch}.tgz -C build/docker/linux/$${arch} ;\
	    cp Dockerfile build/docker/linux/$${arch} ;\
	    docker build -t $(DOCKER_IMAGE_NAME)-$${arch}:$(VERSION) build/docker/linux/$${arch} ;\
	    docker tag $(DOCKER_IMAGE_NAME)-$${arch}:$(VERSION) $(DOCKER_IMAGE_NAME)-$${arch}:latest ;\
	done
endif

.PHONY: docker-push
docker-push:
ifeq ($(DOCKER),)
	$(error "Please specify Docker registry to use. Use DOCKER=coredns for releases")
else
	@# Experimental CLI is required for docker manifest to work
	@# Pushes coredns/coredns-$arch:$version images
	@# Creates manifest for multi-arch image
	@# Pushes multi-arch image to coredns/coredns:$version
	export DOCKER_CLI_EXPERIMENTAL=enabled
	@echo $(DOCKER_PASSWORD) | docker login -u $(DOCKER_LOGIN) --password-stdin
	@echo Pushing: $(VERSION) to $(DOCKER_IMAGE_NAME)
	for arch in $(LINUX_ARCH); do \
		docker push $(DOCKER_IMAGE_NAME)-$${arch}:$(VERSION) ;\
		docker push $(DOCKER_IMAGE_NAME)-$${arch}:latest ;\
	done
	docker manifest create --amend $(DOCKER_IMAGE_NAME):$(VERSION) $(DOCKER_IMAGE_LIST_VERSIONED)
	docker manifest create --amend $(DOCKER_IMAGE_NAME):latest $(DOCKER_IMAGE_LIST_LATEST)
	for arch in $(LINUX_ARCH); do \
		docker manifest annotate --arch $${arch} $(DOCKER_IMAGE_NAME):$(VERSION) $(DOCKER_IMAGE_NAME)-$${arch}:$(VERSION) ;\
		docker manifest annotate --arch $${arch} $(DOCKER_IMAGE_NAME):latest $(DOCKER_IMAGE_NAME)-$${arch}:latest ;\
	done
	docker manifest push --purge $(DOCKER_IMAGE_NAME):$(VERSION)
	docker manifest push --purge $(DOCKER_IMAGE_NAME):latest
endif

.PHONY: version
version:
	@echo $(VERSION)

.PHONY: clean
clean:
	rm -rf release
	rm -rf build

.PHONY: notes
notes:
	@$(MAKE) -s -f Makefile.release authors
	@echo
	@$(MAKE) -s -f Makefile.release prs

.PHONY: prs
prs:
	@echo "## Noteworthy Changes"
	@echo
	@git log $$(git describe --tags --abbrev=0)..HEAD --oneline | awk ' { $$1="";print } ' | sed 's/^ //' | sed -e 's|#\([0-9]\)|https://github.com/coredns/coredns/pull/\1|' | \
	    grep -v '^build(deps)' | \
	    grep -v '^auto go mod tidy' | grep -v '^auto remove' | grep -v '^auto make' | sed 's/^/* /'

.PHONY: authors
authors:
	@echo "## Brought to You By"
	@echo
	@git log --pretty=format:'%an' $$(git describe --tags --abbrev=0)..master | sort -u | grep -v '^coredns-auto' | grep -v '^coredns\[bot\]' | grep -v '^dependabot-preview' | \
	    tac | cat -n | sed -e 's/^[[:space:]]\+1[[:space:]]\+\(.*\)/\1./' | sed -e 's/^[[:space:]]\+[[:digit:]]\+[[:space:]]\+\(.*\)/\1,/' | tac # comma separate, with dot at the end
