mirror of
https://github.com/rodneyosodo/gophercon-africa-2024.git
synced 2026-06-23 04:10:06 +00:00
chore: initialize with golang requirements
- Add CI/CD - Add linter - Add Makefile - Add Dockerfile Signed-off-by: Rodney Osodo <socials@rodneyosodo.com>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
name: Create and publish Docker image
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
tags:
|
||||
- "v*"
|
||||
paths:
|
||||
- ".github/workflows/cd.yaml"
|
||||
- "**.go"
|
||||
|
||||
workflow_dispatch:
|
||||
|
||||
env:
|
||||
REGISTRY: ghcr.io
|
||||
IMAGE_NAME: ${{ github.repository }}
|
||||
|
||||
jobs:
|
||||
build-and-push-image:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Fetch tags for the build
|
||||
run: |
|
||||
git fetch --prune --unshallow --tags
|
||||
|
||||
- name: Set up Docker Build
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Log in to the Container registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Build and push Docker image
|
||||
run: |
|
||||
make docker-push
|
||||
@@ -0,0 +1,43 @@
|
||||
name: Continuous Integration
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
paths:
|
||||
- ".github/workflows/ci.yaml"
|
||||
- "**.go"
|
||||
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
paths:
|
||||
- ".github/workflows/ci.yaml"
|
||||
- "**.go"
|
||||
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
lint-and-build:
|
||||
name: Lint and Build
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: 1.23.x
|
||||
cache-dependency-path: "go.sum"
|
||||
|
||||
- name: golangci-lint
|
||||
uses: golangci/golangci-lint-action@v6
|
||||
with:
|
||||
version: v1.61.0
|
||||
args: --config ./.golangci.yaml
|
||||
|
||||
- name: Build
|
||||
run: |
|
||||
make all
|
||||
@@ -23,3 +23,6 @@ go.work.sum
|
||||
|
||||
# env file
|
||||
.env
|
||||
|
||||
# Build
|
||||
build
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
run:
|
||||
timeout: 3m
|
||||
|
||||
issues:
|
||||
max-issues-per-linter: 100
|
||||
max-same-issues: 100
|
||||
|
||||
linters-settings:
|
||||
gocritic:
|
||||
enabled-tags:
|
||||
- diagnostic
|
||||
- performance
|
||||
- style
|
||||
- experimental
|
||||
- opinionated
|
||||
|
||||
linters:
|
||||
enable-all: true
|
||||
disable:
|
||||
- execinquery # deprecated
|
||||
- gomnd # deprecated
|
||||
- exportloopref # deprecated
|
||||
- depguard
|
||||
- exhaustruct
|
||||
- ireturn
|
||||
- funlen
|
||||
- varnamelen
|
||||
- wrapcheck
|
||||
- wsl
|
||||
- nonamedreturns
|
||||
- cyclop
|
||||
- errorlint
|
||||
@@ -0,0 +1,63 @@
|
||||
CGO_ENABLED ?= 0
|
||||
GOOS ?= $(shell go env GOOS)
|
||||
GOARCH ?= $(shell go env GOARCH)
|
||||
GOARM ?= $(shell go env GOARM)
|
||||
|
||||
BUILD_DIR ?= ./build
|
||||
SVC = gophercon
|
||||
DOCKER_IMAGE_NAME ?= ghcr.io/rodneyosodo/gophercon
|
||||
VERSION ?= $(shell git describe --abbrev=0 --tags 2>/dev/null || echo 'v0.0.0')
|
||||
|
||||
define compile_service
|
||||
CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) GOARM=$(GOARM) \
|
||||
go build -ldflags "-s -w " -o ${BUILD_DIR}/$(SVC) cmd/main.go
|
||||
endef
|
||||
|
||||
define make_docker
|
||||
docker build \
|
||||
--build-arg SVC=$(SVC) \
|
||||
--no-cache \
|
||||
--tag=$(DOCKER_IMAGE_NAME):$(VERSION) \
|
||||
--tag=$(DOCKER_IMAGE_NAME):latest \
|
||||
-f docker/Dockerfile .
|
||||
endef
|
||||
|
||||
define docker_push
|
||||
docker push $(DOCKER_IMAGE_NAME):$(VERSION)
|
||||
docker push $(DOCKER_IMAGE_NAME):latest
|
||||
endef
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
@mkdir -p ${BUILD_DIR}
|
||||
$(call compile_service)
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -rf ${BUILD_DIR}
|
||||
|
||||
.PHONY: docker
|
||||
docker: build
|
||||
$(call make_docker)
|
||||
|
||||
.PHONY: docker-push
|
||||
docker-push: docker
|
||||
$(call docker_push)
|
||||
|
||||
.PHONY: lint
|
||||
lint:
|
||||
golangci-lint run --config .golangci.yaml
|
||||
|
||||
.PHONY: all
|
||||
all: build docker
|
||||
|
||||
.PHONY: help
|
||||
help:
|
||||
@echo "Makefile for gophercon"
|
||||
@echo "Usage:"
|
||||
@echo " make build - Build the binary"
|
||||
@echo " make docker - Build the docker image"
|
||||
@echo " make docker-push - Push the docker image"
|
||||
@echo " make lint - Lint the code"
|
||||
@echo " make all - Build the binary and docker image"
|
||||
@echo " make clean - Clean the build directory"
|
||||
@@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "log"
|
||||
|
||||
func main() {
|
||||
log.Println("Hello World")
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
FROM golang:1.23-alpine AS builder
|
||||
ARG SVC
|
||||
WORKDIR /go/src/github.com/rodneyosodo/gophercon
|
||||
COPY . .
|
||||
RUN apk update \
|
||||
&& apk add make\
|
||||
&& make build \
|
||||
&& mv build/${SVC} /exe
|
||||
|
||||
FROM scratch
|
||||
LABEL org.opencontainers.image.source=https://github.com/rodneyosodo/gophercon-africa-2024
|
||||
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
||||
COPY --from=builder /exe /
|
||||
ENTRYPOINT ["/exe"]
|
||||
Reference in New Issue
Block a user