Skip to content

Commit

Permalink
feat(ci): add tools for download
Browse files Browse the repository at this point in the history
  • Loading branch information
WaterLemons2k committed Feb 25, 2024
1 parent 4481571 commit 895fe17
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 15 deletions.
18 changes: 8 additions & 10 deletions .github/workflows/ddns-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: ddns-go
on:
push:
schedule:
- cron: '0 0 * * 0'
- cron: 0 0 * * 0

jobs:
cmp:
Expand All @@ -17,16 +17,14 @@ jobs:
uses: WaterLemons2k/.github/.github/workflows/git.yml@main
with:
run: |
# 向 README.md 和 README.zh-CN.md 的第 4 行写入版本号
sed -i "4c Version: ${{ needs.cmp.outputs.version }}" README.md
sed -i "4c 版本:${{ needs.cmp.outputs.version }}" README.zh-CN.md
VERSION="${{ needs.cmp.outputs.version }}"
# https://unix.stackexchange.com/a/82610
for i in 1 2 3; do wget $(curl https://api.github.com/repos/jeessy2/ddns-go/releases/tags/${{ needs.cmp.outputs.version }} | jq -r '.assets[].browser_download_url' | grep linux_x86_64) && break || echo "Retry $i" && sleep 1; done # 失败时重试 3 次
tar -xvzf *.tar.gz ddns-go
rm *.tar.gz
echo "${{ needs.cmp.outputs.version }}" > VERSION
commit_message: 'feat: bump ddns-go to ${{ needs.cmp.outputs.version }}'
sed -i "4c Version: $VERSION" README.md
sed -i "4c 版本:$VERSION" README.zh-CN.md
echo "$VERSION" > VERSION
go run -C tools . $VERSION
commit_message: "feat: bump ddns-go to ${{ needs.cmp.outputs.version }}"

docker:
needs: [cmp, git]
Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
FROM scratch
LABEL maintainer="waterlemons2k <[email protected]>"

COPY ca-certificates.crt /etc/ssl/certs/
COPY ddns-go /app/
COPY Shanghai /usr/share/zoneinfo/Asia/

ENV TZ=Asia/Shanghai
EXPOSE 9876

ENTRYPOINT ["/app/ddns-go"]
CMD ["-l", ":9876", "-f", "300"]
CMD ["-l", ":9876", "-f", "300"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ If you need the minimum image size and don't have the above requirements, you ca

```bash
docker run -d --name ddns-go --restart=always -p 9876:9876 -v /opt/ddns-go:/root waterlemons2k/ddns-go
```
```
6 changes: 3 additions & 3 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

## 使用

- 挂载主机目录, 使用docker host模式。可把 `/opt/ddns-go` 替换为你主机任意目录, 配置文件为隐藏文件
- 挂载主机目录, 使用 docker host 模式。可把 `/opt/ddns-go` 替换为你主机任意目录, 配置文件为隐藏文件

```bash
docker run -d --name ddns-go --restart=always --net=host -v /opt/ddns-go:/root waterlemons2k/ddns-go
Expand All @@ -29,8 +29,8 @@
docker run -d --name ddns-go --restart=always --net=host -v /opt/ddns-go:/root waterlemons2k/ddns-go -l :9877 -f 600
```

- [可选] 不使用docker host模式
- [可选] 不使用 docker host 模式

```bash
docker run -d --name ddns-go --restart=always -p 9876:9876 -v /opt/ddns-go:/root waterlemons2k/ddns-go
```
```
80 changes: 80 additions & 0 deletions tools/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"

"github.com/WaterLemons2k/Docker-ddns-go/tools/json"
"github.com/WaterLemons2k/Docker-ddns-go/tools/untar"
)

// Release represents a GitHub release.
//
// https://docs.github.com/en/rest/releases/releases#get-a-release-by-tag-name
type Release struct {
Assets []Asset `json:"assets"`
}

// Asset represents a GitHub release asset.
type Asset struct {
Name string `json:"name"`
URL string `json:"browser_download_url"`
}

const (
// assetName indicates that the file with assetName in the asset will be downloaded.
assetName = "linux_x86_64"

// repo is the name of the GitHub repository without the owner part.
repo = "ddns-go"
)

var client = &http.Client{Timeout: 10 * time.Second}

func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: go run -C tools . <version>")
return
}
version := os.Args[1]

url := fmt.Sprintf("https://api.github.com/repos/jeessy2/%s/releases/tags/v%s", repo, version)

body := getBody(url)
defer body.Close()

var release Release
if err := json.Parse(body, &release); err != nil {
log.Fatal(err)
}

for _, asset := range release.Assets {
// We only focus on asset with assetName
if !strings.Contains(asset.Name, assetName) {
continue
}

body := getBody(asset.URL)
defer body.Close()
fmt.Println(asset.Name, "downloaded. Extracting", repo, "from the tar.gz file...")

// Extracts the file with repo from the tar.gz file
untar.SpecificFile(body, repo)
break
}
}

// getBody gets the response body from the given url.
func getBody(url string) io.ReadCloser {
resp, err := client.Get(url)
if err != nil {
log.Fatal(err)
}

return resp.Body
}
3 changes: 3 additions & 0 deletions tools/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/WaterLemons2k/Docker-ddns-go/tools

go 1.20
16 changes: 16 additions & 0 deletions tools/json/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package json

import (
"encoding/json"
"io"
)

// Parse parses the JSON-encoded body and stores the result in the value pointed to by v.
func Parse(body io.Reader, v any) error {
data, err := io.ReadAll(body)
if err != nil {
return err
}

return json.Unmarshal(data, v)
}
50 changes: 50 additions & 0 deletions tools/untar/untar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Package untar untars a tarball to disk.
package untar

import (
"archive/tar"
"compress/gzip"
"io"
"log"
"os"
"path/filepath"
"strings"
)

// SpecificFile extracts the file with the specified filename from
// the tar.gz file in src and writes it to the file.
func SpecificFile(src io.Reader, filename string) {
gz, err := gzip.NewReader(src)
if err != nil {
log.Fatal(err)
}
defer gz.Close()

t := tar.NewReader(gz)
for {
header, err := t.Next()
if err != nil {
if err == io.EOF {
break
}

log.Fatal(err)
}

// Extracts only regular file with the specified filename
if !(header.Typeflag == tar.TypeReg && strings.Contains(header.Name, filename)) {
continue
}

// Write the extracted file to the parent directory of the executable
out, err := os.Create(filepath.Join("..", header.Name))
if err != nil {
log.Fatal(err)
}
defer out.Close()

if _, err = io.Copy(out, t); err != nil {
log.Fatal(err)
}
}
}

0 comments on commit 895fe17

Please sign in to comment.