Skip to content

Commit

Permalink
add: version cmd
Browse files Browse the repository at this point in the history
add: dockerfile
  • Loading branch information
nitezs committed Mar 12, 2024
1 parent 2a6e427 commit 49112dd
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 3 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Build and Push Docker

on:
push:
branches:
- dev
tags:
- "*"
workflow_dispatch:

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v3

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ secrets.DOCKER_HUB_USERNAME }}/${{ github.repository }}
ghcr.io/${{ github.repository }}
- name: Prepare tags and build args
id: prep
run: |
if [[ ${{ github.event_name }} == 'workflow_dispatch']]; then
TAGS="dev"
VERSION="${{ github.sha }}"
fi
if [[ "$GITHUB_REF" == 'refs/heads/dev' ]]; then
TAGS="dev"
VERSION="${{ github.sha }}"
fi
if [[ "$GITHUB_REF" == refs/tags/* ]]; then
TAG_NAME=${GITHUB_REF#refs/tags/}
TAGS="latest,$TAG_NAME"
VERSION=$TAG_NAME
fi
echo "::set-output name=tags::$TAGS"
echo "::set-output name=version::$VERSION"
- name: Build and push Docker image to GHCR and Docker Hub
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
build-args: version=${{ steps.prep.outputs.version }}
push: true
platforms: linux/amd64,linux/arm,linux/arm64
tags: ${{ steps.prep.outputs.tags }}
2 changes: 1 addition & 1 deletion .github/workflows/goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: build
name: Build and Release

on:
push:
Expand Down
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.21 as builder
LABEL authors="nite07"

WORKDIR /app

COPY . .
RUN go mod download

ARG version

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -X sub2clash/config.Version=${version}" -o sub2sing-box main.go

FROM alpine:latest

COPY --from=builder /app/sub2sing-box /app/sub2sing-box

ENTRYPOINT ["/app/sub2sing-box","server"]
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Nite07

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 5 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cmd

import "github.com/spf13/cobra"
import (
"github.com/spf13/cobra"
)

var RootCmd = &cobra.Command{}

func init() {
func SetVersion(version string) {
RootCmd.Version = version
}
20 changes: 20 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print version",
Long: "Print version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("version: " + RootCmd.Version)
},
}

func init() {
RootCmd.AddCommand(versionCmd)
}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import (
"sub2sing-box/cmd"
)

var Version string

func init() {
Version = "dev"
cmd.SetVersion(Version)
}

func main() {
if err := cmd.RootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down

0 comments on commit 49112dd

Please sign in to comment.