Skip to content

Commit

Permalink
Add some release workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
mstmdev committed Nov 10, 2023
1 parent 483dff9 commit a8dee91
Show file tree
Hide file tree
Showing 12 changed files with 318 additions and 7 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Docker

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

jobs:

build:
strategy:
matrix:
os: [ 'ubuntu-latest' ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- name: Test Docker
run: chmod +x ./scripts/build-docker.sh && ./scripts/build-docker.sh
29 changes: 29 additions & 0 deletions .github/workflows/release.yml
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: Release

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

jobs:

build:
strategy:
matrix:
go: [ 'stable' ]
os: [ 'ubuntu-latest' ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}

- name: Test Release
run: chmod +x ./scripts/build-release.sh && ./scripts/build-release.sh
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM golang:latest AS build

WORKDIR /gowork

ARG GOPROXY
ARG CGO_ENABLED=0

COPY . .

RUN echo $(git rev-parse HEAD) >internal/version/commit

RUN go build -v -o . ./...

FROM alpine:latest

WORKDIR /app

ENV PATH=$PATH:/app

COPY --from=build /gowork/replacer .

WORKDIR /workspace
18 changes: 11 additions & 7 deletions cmd/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
)

var (
ConfigFile string
ConfigUrl string
Root string
Tag string
LogDir string
LogLevel int
Revert bool
ConfigFile string
ConfigUrl string
Root string
Tag string
LogDir string
LogLevel int
Revert bool
PrintVersion bool
PrintAbout bool
)

// InitFlags init built-in flags
Expand All @@ -25,5 +27,7 @@ func InitFlags() {
flag.StringVar(&LogDir, "log_dir", "./logs", "log directory")
flag.IntVar(&LogLevel, "log_level", int(level.DebugLevel), "log level")
flag.BoolVar(&Revert, "revert", false, "revert the replace operations")
flag.BoolVar(&PrintVersion, "v", false, "print the version info")
flag.BoolVar(&PrintAbout, "about", false, "print the about info")
flag.Parse()
}
13 changes: 13 additions & 0 deletions cmd/replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"github.com/no-src/log/level"
"github.com/no-src/nsgo/httputil"
"github.com/no-src/replacer"
"github.com/no-src/replacer/internal/about"
"github.com/no-src/replacer/internal/logger"
"github.com/no-src/replacer/internal/version"
)

// RunWithFlags start the replacer with built-in flags
Expand All @@ -18,6 +20,17 @@ func Run(conf, configUrl, root, tag, logDir string, logLevel int, revert bool) e
if err := logger.InitLogger("replacer", logDir, level.Level(logLevel)); err != nil {
return err
}

if PrintVersion {
version.PrintVersion("replacer", log.Log)
return nil
}

if PrintAbout {
about.PrintAbout(log.Log)
return nil
}

if len(configUrl) > 0 {
client, err := httputil.NewHttpClient(true, "", false)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions internal/about/about.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package about

import (
"github.com/no-src/replacer/internal/version"
)

const logo = `
________ _______ ________ ___ ________ ________ _______ ________
|\ __ \|\ ___ \ |\ __ \|\ \ |\ __ \|\ ____\|\ ___ \ |\ __ \
\ \ \|\ \ \ __/|\ \ \|\ \ \ \ \ \ \|\ \ \ \___|\ \ __/|\ \ \|\ \
\ \ _ _\ \ \_|/_\ \ ____\ \ \ \ \ __ \ \ \ \ \ \_|/_\ \ _ _\
\ \ \\ \\ \ \_|\ \ \ \___|\ \ \____\ \ \ \ \ \ \____\ \ \_|\ \ \ \\ \|
\ \__\\ _\\ \_______\ \__\ \ \_______\ \__\ \__\ \_______\ \_______\ \__\\ _\
\|__|\|__|\|_______|\|__| \|_______|\|__|\|__|\|_______|\|_______|\|__|\|__|
`
const (
openSourceUrl = "https://github.com/no-src/replacer"
documentationUrl = "https://pkg.go.dev/github.com/no-src/replacer@" + version.VERSION
releaseUrl = "https://github.com/no-src/replacer/releases"
dockerImageUrl = "https://hub.docker.com/r/nosrc/replacer"
)

// PrintAbout print the program logo and basic info
func PrintAbout(out func(format string, args ...any)) {
out(logo)
out("The replacer is a configuration-based file replace tool")
out("Open source repository at: <%s>", openSourceUrl)
out("Download the latest version at: <%s>", releaseUrl)
out("The docker image repository address at: <%s>", dockerImageUrl)
out("Full documentation at: <%s>", documentationUrl)
}
15 changes: 15 additions & 0 deletions internal/version/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Version Info

## Version

The `VERSION` constant records the current version info.

## Commit

The `commit` file records the commit hash value of release, it is used for release build.

Please do not commit the `commit` file after it changes, and keep it empty in the git repository.

## GoVersion

Print the version of Go being used, as well as the operating system and architecture targets of the running program.
Empty file added internal/version/commit
Empty file.
26 changes: 26 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package version

import (
_ "embed"
"fmt"
"runtime"
"strings"
)

// VERSION the current program version info
const VERSION = "v0.0.1"

// Commit the commit file records the last commit hash value, used by release
//
//go:embed commit
var Commit string

// PrintVersion print the current version info, and append the commit info if the commit file is not empty
func PrintVersion(name string, out func(format string, args ...any)) {
v := fmt.Sprintf("%s version %s", name, VERSION)
if commit := strings.TrimSpace(Commit); len(commit) > 0 {
v += fmt.Sprintf("\ngit commit %s", commit)
}
v += fmt.Sprintf("\ngo version %s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH)
out(v)
}
46 changes: 46 additions & 0 deletions scripts/build-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

# usage
# build the latest image
# ./scripts/build-docker.sh
# build the image with a specified tag
# ./scripts/build-docker.sh v0.1.0

echo "current branch is $(git branch --show-current)"

# update git repository
# git pull --no-rebase

# update the latest golang image
docker pull golang:latest

# set GOPROXY environment variable
# GOPROXY=https://goproxy.cn

SOFT_NAME=replacer
# docker image name
SOFT_IMAGE_NAME=nosrc/${SOFT_NAME}
# docker image tag
SOFT_IMAGE_TAG=latest

# reset SOFT_IMAGE_TAG to the value of the first parameter provided by the user
if [ -n "$1" ]; then
SOFT_IMAGE_TAG=$1
fi

# remove the existing old image
docker rmi -f $SOFT_IMAGE_NAME:$SOFT_IMAGE_TAG

# build Dockerfile
docker build --build-arg GOPROXY=$GOPROXY -t $SOFT_IMAGE_NAME:$SOFT_IMAGE_TAG .

# remove dangling images
docker image prune -f

docker images | grep ${SOFT_NAME}

# run a container to print the soft version
docker run --rm --name running-${SOFT_NAME}-version $SOFT_IMAGE_NAME:$SOFT_IMAGE_TAG ${SOFT_NAME} -v

# push the image to the DockerHub
# docker push $SOFT_IMAGE_NAME:$SOFT_IMAGE_TAG
100 changes: 100 additions & 0 deletions scripts/build-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env bash

echo "current branch is $(git branch --show-current)"

# update repository
#git pull --no-rebase

# update the last git commit
echo -e "$(git rev-parse HEAD)\c" >internal/version/commit

# set GOPROXY environment variable
# export GOPROXY=https://goproxy.cn

export SOFT_RELEASE_GO_VERSION
export SOFT_RELEASE_VERSION
export SOFT_NAME="replacer"
export SOFT_PREFIX="${SOFT_NAME}_"

function init_version {
go build -v -o . ./...

SOFT_RELEASE_GO_VERSION=$(go version | awk '{print $3}')
SOFT_RELEASE_VERSION=$(./${SOFT_NAME} -v | awk 'NR==1 {print $3}')
}

function build_release {
# release path, for example, replacer_go1.21.1_arm64_linux_v0.1.0
SOFT_RELEASE="${SOFT_PREFIX}${SOFT_RELEASE_GO_VERSION}_${GOARCH}_${GOOS}_${SOFT_RELEASE_VERSION}"

rm -rf "$SOFT_RELEASE"
mkdir "$SOFT_RELEASE"

# build
go build -v -o . ./...

if [ "$GOOS" == "windows" ]; then
mv ${SOFT_NAME}.exe "$SOFT_RELEASE/"
# windows release archive
zip -r "$SOFT_RELEASE.zip" "$SOFT_RELEASE"
else
mv ${SOFT_NAME} "$SOFT_RELEASE/"
# release archive
tar -zcvf "$SOFT_RELEASE.tar.gz" "$SOFT_RELEASE"
fi
rm -rf "$SOFT_RELEASE"
}

init_version

############################## linux-amd64-release ##############################

# set go env
export GOOS=linux
export GOARCH=amd64

build_release

############################## linux-amd64-release ##############################

############################## linux-arm64-release ##############################

export GOOS=linux
export GOARCH=arm64

build_release

############################## linux-arm64-release ##############################

############################# windows-release #############################

# set go env
export GOOS=windows
export GOARCH=amd64

build_release

############################# windows-release #############################

############################## macOS-amd64-release ##############################

export GOOS=darwin
export GOARCH=amd64

build_release

############################## macOS-amd64-release ##############################

############################## macOS-arm64-release ##############################

export GOOS=darwin
export GOARCH=arm64

build_release

############################## macOS-arm64-release ##############################

# reset commit file
echo -e "\c" >internal/version/commit

ls -alh | grep ${SOFT_PREFIX}
4 changes: 4 additions & 0 deletions scripts/clear-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

rm -rf logs
rm -rf replacer_go*

0 comments on commit a8dee91

Please sign in to comment.