Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: go build with make targets #3

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: extract args
run: |
make print-vars | grep GO_VERSION >> $GITHUB_ENV

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

- name: Build
run: make build
3 changes: 2 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: goreleaser

on:
pull_request:
push:
# run only against tags
branches:
- main
tags:
- "v*"

Expand Down
32 changes: 26 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
SHELL: /bin/bash
SHELL := /bin/bash

GO_VERSION := $(shell awk '/^go / {print $$2}' go.mod)
GIT_VERSION := $(shell git describe --always --tags HEAD)
GIT_COMMIT := $(shell git rev-parse HEAD)
GIT_COMMIT_DATE := $(shell git log --pretty=%ct -1)
GIT_TREE_STATE := $(shell git diff --exit-code --quiet && echo clean || echo dirty)

# Print out only the variables declared in this makefile. Will be used
# by other tools like github workflows or any other build tool.
# note: the ldflags value is long with spaces and isn't a valid bash expression
# unless it is quoted. I did't add quotes to the 'echo' section here because it
# creates other problems when using valus in github action. So to source all
# vars in one-shot just ignore ldflags with a grep or specifically surround it
# single quotes. e.g. `make print-vars | grep -v ldflags=`
print-vars:
@$(foreach v,$(sort $(.VARIABLES)), \
$(if $(filter-out environment% default automatic, $(origin $v)), \
$(if $(filter-out .%,$(v)), \
echo $v=$($v);)))

install:
go install -ldflags="$(ldflags)" .
Expand All @@ -9,10 +28,11 @@ fmt:
vet:
go vet ./...

ldflags := -X github.com/rgolangh/pq/internal/version.Version=$(shell git describe --always --tags HEAD)
ldflags += -X github.com/rgolangh/pq/internal/version.Commit=$(shell git rev-parse HEAD)
ldflags += -X github.com/rgolangh/pq/internal/version.CommitDate=$(shell date --iso-8601=seconds)
ldflags += -X github.com/rgolangh/pq/internal/version.TreeState=$(shell git diff --exit-code --quiet && echo clean || echo dirty)
ldflags := -X github.com/rgolangh/pq/internal/version.Version=$(GIT_VERSION)
ldflags += -X github.com/rgolangh/pq/internal/version.Commit=$(GIT_COMMIT)
ldflags += -X github.com/rgolangh/pq/internal/version.CommitDate=$(GIT_COMMIT_DATE)
ldflags += -X github.com/rgolangh/pq/internal/version.TreeState=$(GIT_TREE_STATE)

build: fmt vet
go build -o bin/ -ldflags="$(ldflags)" ./...
go build -v -o bin/ -ldflags="$(ldflags)" ./...

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/rgolangh/pq

go 1.23
go 1.23.2

require (
github.com/spf13/cobra v1.8.0
Expand Down