Skip to content

Commit

Permalink
Add ci
Browse files Browse the repository at this point in the history
  • Loading branch information
galal-hussein committed Feb 21, 2024
1 parent 20a80fb commit bd935e5
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 8 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: release

on:
push:
tags:
- "v*"

permissions:
contents: write

jobs:
release-tag:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v4
with:
cache: false
go-version: "1.22"
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
with:
distribution: goreleaser
version: v1.23.0
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GORELEASER_CURRENT_TAG: ${{ github.ref_name }}
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
# Go workspace file
go.work

# ci
bin/

# Debug
__debug*
.vscode/
cattle-drive
cattle-drive
dist/
52 changes: 52 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
version: 1

snapshot:
name_template: '{{ trimprefix .Summary "v" }}'

before:
hooks:
- go mod tidy

builds:
- goos:
- linux
- darwin
goarch:
- amd64
- arm64
tags:
- netgo
- osusergo
ldflags:
- -s
- -w
- -X "galal-hussein/cattle-drive/pkg/version.GitCommit={{ .FullCommit }}"
- -X "galal-hussein/cattle-drive/pkg/version.Version=v{{ .Version }}"

archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of `uname`.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip

release:
github:
owner: galal-hussein
name: cattle-drive
prerelease: auto

changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM golang:1.22.0-alpine3.19 AS build
RUN apk add -U --no-cache make git bash
COPY . /src/cattle-drive
WORKDIR /src/cattle-drive
RUN ls -l
RUN make build

FROM alpine AS package
COPY --from=build /src/cattle-drive/bin /usr/local/bin/
ENTRYPOINT ["/usr/local/bin/cattle-drive"]
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
default: build
@echo "Run make help for info about other make targets"

build: ## Build using host go tools
./ops/build

test:
go test -v ./...
9 changes: 2 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,22 @@ import (
"galal-hussein/cattle-drive/cli/cmds/interactive"
"galal-hussein/cattle-drive/cli/cmds/migrate"
"galal-hussein/cattle-drive/cli/cmds/status"
"galal-hussein/cattle-drive/pkg/version"
"io"
"os"

"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

const (
program = "cattle-drive"
version = "dev"
gitCommit = "HEAD"
)

func main() {
app := cmds.NewApp()
app.Commands = []*cli.Command{
status.NewCommand(),
migrate.NewCommand(),
interactive.NewCommand(),
}
app.Version = version + " (" + gitCommit + ")"
app.Version = fmt.Sprintf("%s (%s)", version.Version, version.GitCommit)

logrus.SetOutput(io.Discard)
if err := app.Run(os.Args); err != nil {
Expand Down
20 changes: 20 additions & 0 deletions ops/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

source ./ops/version

BUILDTAGS="netgo osusergo"
GO_BUILDTAGS="${BUILD_TAGS} ${GO_BUILDTAGS} ${BUILDTAGS} ${DEBUG_TAGS}"

VERSION_FLAGS="
-X ${CATTLE_DRIVE_PKG}/pkg/version.GitCommit=${REVISION}
-X ${CATTLE_DRIVE_PKG}/pkg/version.Program=${PROG}
-X ${CATTLE_DRIVE_PKG}/pkg/version.Version=${VERSION}
"

STATIC_FLAGS='-extldflags "-static"'

go build \
-tags "${GO_BUILDTAGS}" \
-o bin/${PROG} \
-ldflags "${VERSION_FLAGS}" \
${GO_TAGS}
Empty file added ops/package
Empty file.
51 changes: 51 additions & 0 deletions ops/version
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash
set -x

CATTLE_DRIVE_PKG=galal-hussein/cattle-drive
PROG=cattle-drive
REGISTRY=docker.io
REPO=${REPO:-husseingalal}
GO=${GO-go}
GOARCH=${GOARCH:-$("${GO}" env GOARCH)}
GOOS=${GOOS:-$("${GO}" env GOOS)}
if [ -z "$GOOS" ]; then
if [ "${OS}" == "Windows_NT" ]; then
GOOS="windows"
else
UNAME_S=$(shell uname -s)
if [ "${UNAME_S}" == "Linux" ]; then
GOOS="linux"
elif [ "${UNAME_S}" == "Darwin" ]; then
GOOS="darwin"
elif [ "${UNAME_S}" == "FreeBSD" ]; then
GOOS="freebsd"
fi
fi
fi

TREE_STATE=clean
COMMIT=$DRONE_COMMIT
REVISION=$(git rev-parse HEAD)$(if ! git diff --no-ext-diff --quiet --exit-code; then echo .dirty; fi)
PLATFORM=${GOOS}-${GOARCH}
RELEASE=${PROG}.${PLATFORM}

if [ -d .git ]; then
if [ -z "$GIT_TAG" ]; then
GIT_TAG=$(git tag -l --contains HEAD | head -n 1)
fi
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
DIRTY="-dirty"
TREE_STATE=dirty
fi

COMMIT=$(git log -n3 --pretty=format:"%H %ae" | cut -f1 -d\ | head -1)
if [ -z "${COMMIT}" ]; then
COMMIT=$(git rev-parse HEAD || true)
fi
fi

if [[ -n "$GIT_TAG" ]]; then
VERSION=$GIT_TAG
else
VERSION="${VERSION}+dev.${COMMIT:0:8}$DIRTY"
fi
10 changes: 10 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package version

import "strings"

var (
Program = "cattle-drive"
ProgramUpper = strings.ToUpper(Program)
Version = "dev"
GitCommit = "HEAD"
)

0 comments on commit bd935e5

Please sign in to comment.