Skip to content

Commit

Permalink
releaser, air
Browse files Browse the repository at this point in the history
  • Loading branch information
Ujstor committed Jun 21, 2024
1 parent c92b75d commit 7609c1a
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 22 deletions.
46 changes: 46 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "./main"
cmd = "make build"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go", ".*_templ.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html", "templ"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false

[misc]
clean_on_exit = false

[screen]
clear_on_rebuild = false
keep_scroll = true
1 change: 1 addition & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ jobs:
context: .
push: true
tags: ${{ env.DOCKER_HUB_USERNAME }}/${{ env.DOCKER_REPO_NAME }}:${{ env.NEW_TAG }}
target: prod
30 changes: 30 additions & 0 deletions .github/workflows/goreleasaer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: goreleaser

on:
push:
tags:
- "v*.*.*"

permissions:
contents: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22.0'
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5.0.0
with:
distribution: goreleaser
version: ${{ env.GITHUB_REF_NAME }}
args: release --clean
workdir: ./
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
tmp/
*templ.go

main
42 changes: 42 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
before:
hooks:
- go mod tidy

env:
- PACKAGE_PATH=github.com/ujstor/portfolio-web/cmd

builds:
- binary: "{{ .ProjectName }}"
main: ./cmd/api
goos:
- darwin
- linux
- windows
goarch:
- amd64
- arm64
env:
- CGO_ENABLED=0
ldflags:
- -s -w -X {{.Env.PACKAGE_PATH}}={{.Version}}
release:
prerelease: auto

universal_binaries:
- replace: true

archives:
- name_template: >
{{- .ProjectName }}_{{- .Version }}_{{- title .Os }}_{{- if eq .Arch "amd64" }}x86_64{{- else if eq .Arch "386" }}i386{{- else }}{{ .Arch }}{{ end }}{{- if .Arm }}v{{ .Arm }}{{ end -}}
format_overrides:
- goos: windows
format: zip
builds_info:
group: root
owner: root
files:
- README.md
checksum:
name_template: 'checksums.txt'

14 changes: 9 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22-alpine as build
FROM golang:1.22-alpine as base

RUN apk add --no-cache make

Expand All @@ -9,11 +9,15 @@ RUN go mod download

COPY . .

RUN go install github.com/a-h/templ/cmd/templ@latest
RUN templ generate
RUN go build -o main cmd/api/main.go
FROM base as dev
RUN make build
EXPOSE 5000
CMD [ "sh", "-c", "echo 'y' | make watch" ]

FROM base as build
RUN make build

FROM alpine:3.20.1
FROM alpine:3.20.1 as prod
WORKDIR /app
COPY --from=build /app/main /app/main
EXPOSE 5000
Expand Down
35 changes: 30 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
# Build the application
all: build
all: build docker-build docker-run

build:
@echo "Building..."
@templ generate
@if command -v templ > /dev/null; then \
templ generate; \
else \
read -p "Go's 'templ' is not installed on your machine. Do you want to install it? [Y/n] " choice; \
if [ "$$choice" != "n" ] && [ "$$choice" != "N" ]; then \
go install github.com/a-h/templ/cmd/templ@latest; \
templ generate; \
else \
echo "You chose not to install templ. Exiting..."; \
exit 1; \
fi; \
fi
@go build -o main cmd/api/main.go

docker-build:
@docker build -t ujstor/portfolio-web-go .
@docker build -t ujstor/portfolio-web-go --target prod .

# Run the application
run:
Expand All @@ -25,4 +35,19 @@ clean:
@echo "Cleaning..."
@rm -f main

.PHONY: all build docker-build run docker-run push clean
watch:
@if command -v air > /dev/null; then \
air; \
echo "Watching...";\
else \
read -p "Go's 'air' is not installed on your machine. Do you want to install it? [Y/n] " choice; \
if [ "$$choice" != "n" ] && [ "$$choice" != "N" ]; then \
go install github.com/air-verse/air@latest; \
air; \
echo "Watching...";\
else \
echo "You chose not to install air. Exiting..."; \
exit 1; \
fi; \
fi
.PHONY: all build docker-build run docker-run push clean watch
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,57 @@ Deployment can be achieved through self-hosting service provided by [Collify](ht
## MakeFile

```bash
all: build
all: build docker-build docker-run

build:
@echo "Building..."
@templ generate
@if command -v templ > /dev/null; then \
templ generate; \
else \
read -p "Go's 'templ' is not installed on your machine. Do you want to install it? [Y/n] " choice; \
if [ "$$choice" != "n" ] && [ "$$choice" != "N" ]; then \
go install github.com/a-h/templ/cmd/templ@latest; \
templ generate; \
else \
echo "You chose not to install templ. Exiting..."; \
exit 1; \
fi; \
fi
@go build -o main cmd/api/main.go

docker-build:
@docker build -t ujstor/portfolio-web-go .
@docker build -t ujstor/portfolio-web-go --target prod .

# Run the application
run:
@go run cmd/api/main.go

docker-run:
@docker run -p 5000:5000 ujstor/portfolio-web-go

# Push app to DockerHub
push:
@docker push ujstor/portfolio-web-go

# Clean the binary
clean:
@echo "Cleaning..."
@rm -f main

.PHONY: all build docker-build run docker-run push clean
watch:
@if command -v air > /dev/null; then \
air; \
echo "Watching...";\
else \
read -p "Go's 'air' is not installed on your machine. Do you want to install it? [Y/n] " choice; \
if [ "$$choice" != "n" ] && [ "$$choice" != "N" ]; then \
go install github.com/air-verse/air@latest; \
air; \
echo "Watching...";\
else \
echo "You chose not to install air. Exiting..."; \
exit 1; \
fi; \
fi
.PHONY: all build docker-build run docker-run push clean watch
```
16 changes: 15 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
services:
web:
web-dev:
build:
context: .
dockerfile: Dockerfile
target: dev
ports:
- 5000:5000
volumes:
- ./:/app

web-prod:
build:
context: .
dockerfile: Dockerfile
target: prod
ports:
- 5000:5000

# docker compose up web-dev --build
# docker compose up web-prod --build
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ go 1.22.0
require (
github.com/a-h/templ v0.2.707
github.com/go-chi/chi/v5 v5.0.14
github.com/joho/godotenv v1.5.1
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ github.com/go-chi/chi/v5 v5.0.14 h1:PyEwo2Vudraa0x/Wl6eDRRW2NXBvekgfxyydcM0WGE0=
github.com/go-chi/chi/v5 v5.0.14/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

0 comments on commit 7609c1a

Please sign in to comment.