Skip to content

Commit

Permalink
ci(docker): add goreleaser docker GitHub Container Registry support
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Smith <derek@clokwork.net>
  • Loading branch information
clok committed Apr 2, 2021
1 parent 90b511c commit 601b392
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 30 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/edge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: docker

on:
push:
branches:
- main

env:
GO_VERSION: "1.16"
DOCKER_REGISTRY: "ghcr.io"

jobs:
edge:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}

- name: Login to GitHub Packages Docker Registry
uses: docker/login-action@v1
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and Push docker image
run: |
DOCKER_TAG=edge make docker
docker push ${{ env.DOCKER_REGISTRY }}/clok/hev-cli:edge
15 changes: 14 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
tags:
- '*'

env:
GO_VERSION: "1.16"
DOCKER_REGISTRY: "ghcr.io"

jobs:
goreleaser:
runs-on: ubuntu-20.04
Expand All @@ -13,10 +17,19 @@ jobs:
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: ${{ env.GO_VERSION }}

- name: Login to GitHub Packages Docker Registry
uses: docker/login-action@v1
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
# vendor/
build/
bin/
dist/
dist/
hev
hev-cli
7 changes: 7 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ changelog:

snapshot:
name_template: "{{ .Tag }}-next"

dockers:
- ids:
- hev
image_templates:
- "ghcr.io/clok/hev-cli:{{ .RawVersion }}"
- "ghcr.io/clok/hev-cli:latest"
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM alpine:3.13.4

COPY hev /usr/local/bin/hev
RUN chmod +x /usr/local/bin/hev

RUN mkdir /workdir
WORKDIR /workdir

ENTRYPOINT [ "/usr/local/bin/hev" ]
114 changes: 97 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,106 @@
NAME=hev
# Build Variables
NAME = hev
VERSION ?= $(shell git describe --tags --always)

VERSION?=$$(git describe --tags --always)
SHORT_VERSION=$$(git describe --tags --always | awk -F '-' '{print $$1}')
# Go variables
GO ?= go
GOOS ?= $(shell $(GO) env GOOS)
GOARCH ?= $(shell $(GO) env GOARCH)
GOHOST ?= GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO)

LDFLAGS=-ldflags=all="-X main.version=${SHORT_VERSION}"
LDFLAGS ?= "-X main.version=$(VERSION)"

all: build
.PHONY: all
all: help

build:
###############
##@ Development

.PHONY: clean
clean: ## Clean workspace
@ $(MAKE) --no-print-directory log-$@
rm -rf bin/
rm -rf build/
rm -rf dist/
rm -rf cover.out
rm -f ./$(NAME)
go mod tidy

.PHONY: test
test: ## Run tests
@ $(MAKE) --no-print-directory log-$@
$(GOHOST) test -covermode atomic -coverprofile cover.out -v ./...

.PHONY: lint
lint: ## Run linters
@ $(MAKE) --no-print-directory log-$@
golangci-lint run

#########
##@ Build

.PHONY: build
build: clean ## Build gwsm
@ $(MAKE) --no-print-directory log-$@
@mkdir -p bin/
go get -t ./...
go test -v ./...
go build ${LDFLAGS} -o bin/${NAME} ./main.go
CGO_ENABLED=0 $(GOHOST) build -ldflags=$(LDFLAGS) -o bin/$(NAME) ./main.go

alpine: clean ## Build binary for alpine docker image
@ $(MAKE) --no-print-directory log-$@
env GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags=$(LDFLAGS) -o $(NAME) ./main.go

.PHONY: docker
docker: DOCKER_TAG ?= dev
docker: alpine ## Build Docker image
@ $(MAKE) --no-print-directory log-$@
docker build --pull --tag ghcr.io/clok/hev-cli:$(DOCKER_TAG) .
make clean

###########
##@ Release

.PHONY: changelog
changelog: ## Generate changelog
@ $(MAKE) --no-print-directory log-$@
git-chglog --next-tag $(VERSION) -o CHANGELOG.md

.PHONY: release
release: ## Release a new tag
@ $(MAKE) --no-print-directory log-$@
./release.sh $(VERSION)

docs:
DOCS_MD=1 go run ./main.go > docs/${NAME}.md
DOCS_MAN=1 go run ./main.go > docs/${NAME}.8
.PHONY: docs
docs: ## Generate new docs
@ $(MAKE) --no-print-directory log-$@
DOCS_MD=1 go run ./main.go > docs/$(NAME).md
DOCS_MAN=1 go run ./main.go > docs/$(NAME).8

clean:
@rm -rf bin/ && rm -rf build/ && rm -rf dist/
########
##@ Help

lint:
@golangci-lint run
.PHONY: help
help: ## Display this help
@awk \
-v "col=\033[36m" -v "nocol=\033[0m" \
' \
BEGIN { \
FS = ":.*##" ; \
printf "Usage:\n make %s<target>%s\n", col, nocol \
} \
/^[a-zA-Z_-]+:.*?##/ { \
printf " %s%-12s%s %s\n", col, $$1, nocol, $$2 \
} \
/^##@/ { \
printf "\n%s%s%s\n", nocol, substr($$0, 5), nocol \
} \
' $(MAKEFILE_LIST)

.PHONY: all tools build clean docs lint
log-%:
@grep -h -E '^$*:.*?## .*$$' $(MAKEFILE_LIST) | \
awk \
'BEGIN { \
FS = ":.*?## " \
}; \
{ \
printf "\033[36m==> %s\033[0m\n", $$2 \
}'
29 changes: 18 additions & 11 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

set +e

NAME=hev-cli

#
# Set Colors
#
Expand Down Expand Up @@ -64,29 +66,34 @@ if [ "x${VERSION}x" = "xx" ]; then
exit 1
fi

h1 "Preparing release of $VERSION"
if [[ "$(git tag -l | grep -c "$VERSION" 2>/dev/null)" != "0" ]]; then
error "Tag $VERSION already exists in this repo. Please use a different version."
exit 1
fi

h1 "Preparing release of $VERSION for $NAME"

h2 "Updating docs"
make docs
if [[ "$(git status -s docs/hev.* 2>/dev/null | wc -l)" == "0" ]]; then
if [[ "$(git status -s docs/${NAME}.* 2>/dev/null | wc -l)" == "0" ]]; then
note "No changes to docs"
else
note "Committing changes to docs"
git add docs/hev.*
git add docs/${NAME}.*
git commit -m "chore(docs): updating docs for version $VERSION"
fi

h2 "Updating CHANGELOG.md"
git-chglog --next-tag $VERSION -o CHANGELOG.md && git add CHANGELOG.md
git commit -m "feat(release): $VERSION"
make changelog
git add CHANGELOG.md
git commit -m "chore(release): $VERSION"

h2 "Tagging version: $VERSION"
git tag $VERSION
git tag "$VERSION"

note "Pushing branch: git push origin $(git rev-parse --abbrev-ref HEAD)"
git push origin $(git rev-parse --abbrev-ref HEAD)
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
note "Pushing branch: git push origin $BRANCH"
git push origin "$BRANCH"

note "Pushing tag: git push origin $VERSION"
git push origin $VERSION

success "Done!"
git push origin "$VERSION"

0 comments on commit 601b392

Please sign in to comment.