Skip to content

Commit

Permalink
updated build
Browse files Browse the repository at this point in the history
  • Loading branch information
mguptahub committed Oct 31, 2024
1 parent 825e423 commit 4814984
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
47 changes: 39 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ on:
workflow_dispatch:
push:
tags: [ 'v*' ]
# branches: [ "master" ]
# pull_request:
# branches: [ "master" ]

permissions:
contents: write
Expand All @@ -20,28 +17,44 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Required for getting git information

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

- name: Set version
id: version
run: |
if [[ $GITHUB_REF == refs/tags/* ]]; then
echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
else
echo "VERSION=$(git rev-parse --abbrev-ref HEAD)-$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
fi
- name: Install dependencies
run: go mod download

- name: Run tests with coverage
run: go test -cover ./...

- name: Build
run: go build -v -o nanodns ./cmd/server
run: |
go build -v \
-ldflags="-X main.version=${{ steps.version.outputs.VERSION }}" \
-o nanodns ./cmd/server
docker:
needs: build
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand All @@ -56,6 +69,15 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set version
id: version
run: |
if [[ $GITHUB_REF == refs/tags/* ]]; then
echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
else
echo "VERSION=$(git rev-parse --abbrev-ref HEAD)-$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
fi
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
Expand All @@ -77,6 +99,8 @@ jobs:
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
VERSION=${{ steps.version.outputs.VERSION }}
- name: Make package public
run: |
Expand All @@ -95,19 +119,26 @@ jobs:
if: startsWith(github.ref, 'refs/tags/')
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

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

- name: Set version
id: version
run: |
echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
- name: Build Release Binaries
run: |
GOOS=linux GOARCH=amd64 go build -o nanodns-linux-amd64 ./cmd/server
GOOS=linux GOARCH=arm64 go build -o nanodns-linux-arm64 ./cmd/server
GOOS=darwin GOARCH=amd64 go build -o nanodns-darwin-amd64 ./cmd/server
GOOS=darwin GOARCH=arm64 go build -o nanodns-darwin-arm64 ./cmd/server
GOOS=linux GOARCH=amd64 go build -ldflags="-X main.version=${{ steps.version.outputs.VERSION }}" -o nanodns-linux-amd64 ./cmd/server
GOOS=linux GOARCH=arm64 go build -ldflags="-X main.version=${{ steps.version.outputs.VERSION }}" -o nanodns-linux-arm64 ./cmd/server
GOOS=darwin GOARCH=amd64 go build -ldflags="-X main.version=${{ steps.version.outputs.VERSION }}" -o nanodns-darwin-amd64 ./cmd/server
GOOS=darwin GOARCH=arm64 go build -ldflags="-X main.version=${{ steps.version.outputs.VERSION }}" -o nanodns-darwin-arm64 ./cmd/server
- name: Create Release
uses: softprops/action-gh-release@v1
Expand Down
10 changes: 9 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# Build stage
FROM golang:1.22-alpine AS builder

# Build args
ARG VERSION="dev"

WORKDIR /app
COPY . .
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux go build -o nanodns ./cmd/server

# Build with version information
RUN CGO_ENABLED=0 GOOS=linux go build \
-ldflags="-X main.version=${VERSION}" \
-o nanodns ./cmd/server

# Final stage
FROM alpine:latest
Expand Down
7 changes: 3 additions & 4 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func init() {
}

func main() {
flag.Parse()
if len(os.Args) > 1 {
switch flag.Arg(0) {
case "start":
Expand All @@ -86,19 +85,19 @@ func main() {
case "help":
flag.Usage()
return
default:
flag.Usage()
return
}
}
flag.Parse()

if showHelp {
flag.Usage()
return
}

if showVersion {
fmt.Println("")
fmt.Printf("NanoDNS Version: %s\n", version)
fmt.Println("")
return
}

Expand Down

0 comments on commit 4814984

Please sign in to comment.