Skip to content

Commit

Permalink
Makefile updates (#5)
Browse files Browse the repository at this point in the history
* Add GitHub related targets

- github-create-release
- github-upload-assets
- github-delete-release

* Add comment for make help

* Minorupdates in Makefile
  • Loading branch information
asokolov365 authored Jan 13, 2024
1 parent 0e8d0a1 commit 0c64655
Showing 1 changed file with 78 additions and 20 deletions.
98 changes: 78 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ DOCKER_IMAGE_LIST := docker image ls --format '{{.Repository}}:{{.Tag}}'
GO_BUILDER_IMAGE := golang:1.21.6-alpine
BUILDER_IMAGE := local/go-builder:$(shell echo $(GO_BUILDER_IMAGE) | tr ':/' '--')-1

.PHONY: $(MAKECMDGOALS)
GITHUB_RELEASE_SPEC_FILE="/tmp/yp-github-release"
GITHUB_DEBUG_FILE="/tmp/yp-github-debug"

# include release/Makefile
.PHONY: $(MAKECMDGOALS)

default: all

Expand Down Expand Up @@ -92,8 +93,8 @@ build-openbsd-amd64-local:
build-goos-goarch-local:
mkdir -p $(ROOT)/build
# rm needed due to signature caching (https://apple.stackexchange.com/a/428388)
rm -f "$(ROOT)/build/$(APP_NAME)-$(GOOS)-$(GOARCH)"
CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -ldflags "$(GO_BUILDINFO)" -tags "osusergo" -o "$(ROOT)/build/$(APP_NAME)-$(GOOS)-$(GOARCH)" .
rm -f "$(ROOT)/build/$(APP_NAME)-$(GOOS)-$(GOARCH)-dev"
CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -ldflags "$(GO_BUILDINFO)" -tags "osusergo" -o "$(ROOT)/build/$(APP_NAME)-$(GOOS)-$(GOARCH)-dev" .

build-linux-amd64-docker:
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 $(MAKE) build-goos-goarch-docker
Expand Down Expand Up @@ -183,19 +184,10 @@ release-goos-goarch: \
rm -f $(APP_NAME)-$(GOOS)-$(GOARCH)-prod


##@ Publish

# publish: publish-yp

publish-release:
rm -rf $(ROOT)/build/*
git checkout $(TAG) && $(MAKE) release && $(MAKE) publish


##@ Clean

clean: ## Remove produced binaries, libraries, and temp files
@rm -rf $(ROOT)/build/* $(ROOT)/docker-gocache $(ROOT)/tmp
@rm -rf $(ROOT)/build/* $(ROOT)/release/* $(ROOT)/docker-gocache $(ROOT)/tmp
@rm -f coverage.txt

##@ Checks
Expand Down Expand Up @@ -303,6 +295,76 @@ install-wwhrd:
@which wwhrd || echo "--> Installing wwhrd@latest"; \
go install github.com/frapposelli/wwhrd@latest

##@ GitHub

github-token-check:
ifndef GITHUB_TOKEN
$(error missing GITHUB_TOKEN env var. It should be a github token for YamlPartitioner project obtained from https://github.com/settings/tokens)
endif

github-tag-check:
ifndef TAG
$(error missing TAG env var. It must contain github release tag to create)
endif

github-create-release: github-token-check github-tag-check ## Create release draft
@result=$$(curl -o $(GITHUB_RELEASE_SPEC_FILE) -s -w "%{http_code}" \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: token $(GITHUB_TOKEN)" \
https://api.github.com/repos/asokolov365/YamlPartitioner/releases \
-d '{"tag_name":"$(TAG)","name":"$(TAG)","body":"TODO: put here the changelog for $(TAG) release from CHANGELOG.md","draft":true,"prerelease":false,"generate_release_notes":false}'); \
if [ $${result} = 201 ]; then \
release_id=$$(cat $(GITHUB_RELEASE_SPEC_FILE) | grep '"id"' -m 1 | sed -E 's/.* ([[:digit:]]+)\,/\1/'); \
printf "Created release $(TAG) with id=$${release_id}\n"; \
else \
printf "Failed to create release $(TAG)\n"; \
cat $(GITHUB_RELEASE_SPEC_FILE); \
exit 1; \
fi

github-upload-assets: ## Upload assets
@release_id=$$(cat $(GITHUB_RELEASE_SPEC_FILE) | grep '"id"' -m 1 | sed -E 's/.* ([[:digit:]]+)\,/\1/'); \
$(foreach file, $(wildcard release/*.tar.gz), FILE=$(file) RELEASE_ID=$${release_id} CONTENT_TYPE="application/x-gzip" $(MAKE) github-upload-asset || exit 1;) \
$(foreach file, $(wildcard release/*_checksums.txt), FILE=$(file) RELEASE_ID=$${release_id} CONTENT_TYPE="text/plain" $(MAKE) github-upload-asset || exit 1;)

github-upload-asset: github-token-check
ifndef FILE
$(error missing FILE env var. It must contain path to file to upload to github release)
endif
@printf "Uploading $(FILE)\n"
@result=$$(curl -o $(GITHUB_DEBUG_FILE) -w "%{http_code}" \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: token $(GITHUB_TOKEN)" \
-H "Content-Type: $(CONTENT_TYPE)" \
--data-binary "@$(FILE)" \
https://uploads.github.com/repos/asokolov365/YamlPartitioner/releases/$(RELEASE_ID)/assets?name=$(notdir $(FILE))); \
if [ $${result} = 201 ]; then \
printf "Upload OK: $${result}\n"; \
elif [ $${result} = 422 ]; then \
printf "Asset already uploaded, you need to delete it from UI if you want to re-upload it\n"; \
else \
printf "Upload failed: $${result}\n"; \
cat $(GITHUB_DEBUG_FILE); \
exit 1; \
fi

github-delete-release: github-token-check ## Delete release
@release_id=$$(cat $(GITHUB_RELEASE_SPEC_FILE) | grep '"id"' -m 1 | sed -E 's/.* ([[:digit:]]+)\,/\1/'); \
result=$$(curl -o $(GITHUB_DEBUG_FILE) -s -w "%{http_code}" \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: token $(GITHUB_TOKEN)" \
https://api.github.com/repos/asokolov365/YamlPartitioner/releases/$${release_id}); \
if [ $${result} = 204 ]; then \
printf "Deleted release with id=$${release_id}\n"; \
else \
printf "Failed to delete release with id=$${release_id}\n"; \
cat $(GITHUB_DEBUG_FILE); \
exit 1; \
fi

##@ Help

# The help target prints out all targets with their descriptions organized
Expand All @@ -317,11 +379,7 @@ install-wwhrd:
# http://linuxcommand.org/lc3_adv_awk.php
help: ## Display this help.
@echo -e "\033[32m"
@echo "Targets in this Makefile build and test YamlPartitioner in a build container in"
@echo "Docker. For testing (only), use the 'local' prefix target to run targets directly"
@echo "on your workstation (ex. 'make local test'). You will need to have its GOPATH set"
@echo "and have already run 'make tools'. Set GOOS=linux to build binaries for Docker."
@echo "Do not use 'make local' for building binaries for public release!"
@echo "Before packaging always run 'make clean build test integration'!"
@echo "Do not use 'make crossbuild' for building binaries for public release!"
@echo "Before packaging always run 'make clean release'!"
@echo
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

0 comments on commit 0c64655

Please sign in to comment.