From 09a76268dd1bc32dba02a2fcde2e7a6d35dcb362 Mon Sep 17 00:00:00 2001 From: niuhuan Date: Wed, 3 Apr 2024 16:34:04 +0800 Subject: [PATCH] :construction_worker: release core ci --- .github/workflows/Release.core.yml | 94 ++++++++++++++++++++++++++++++ ci/cmd/check_asset_core/main.go | 65 +++++++++++++++++++++ ci/cmd/upload_asset_core/main.go | 93 +++++++++++++++++++++++++++++ 3 files changed, 252 insertions(+) create mode 100644 .github/workflows/Release.core.yml create mode 100644 ci/cmd/check_asset_core/main.go create mode 100644 ci/cmd/upload_asset_core/main.go diff --git a/.github/workflows/Release.core.yml b/.github/workflows/Release.core.yml new file mode 100644 index 0000000..161f348 --- /dev/null +++ b/.github/workflows/Release.core.yml @@ -0,0 +1,94 @@ +name: Release-core +on: + workflow_dispatch: + +env: + GH_TOKEN: ${{ secrets.GH_TOKEN }} + GO_VERSION: "1.18" + +jobs: + ci-pass: + name: CI is green + runs-on: ubuntu-latest + needs: + - build_release_assets + steps: + - run: exit 0 + + build_release_assets: + name: Build and upload assets + strategy: + fail-fast: false + matrix: + config: + - target: ios + host: macos-latest + - target: android + host: ubuntu-latest + runs-on: ${{ matrix.config.host }} + env: + TARGET: ${{ matrix.config.target }} + steps: + - name: Setup golang + uses: actions/setup-go@v2 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Cache go modules (Linux) + if: matrix.config.host == 'ubuntu-latest' + uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ matrix.config.host }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ matrix.config.host }}-go- + + - name: Cache go modules (macOS) + if: matrix.config.host == 'macos-latest' + uses: actions/cache@v3 + with: + path: | + ~/Library/Caches/go-build + ~/go/pkg/mod + key: ${{ matrix.config.host }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ matrix.config.host }}-go- + + - name: Checkout + uses: actions/checkout@v3 + with: + ref: ${{ env.BRANCH }} + + - id: check_asset + name: Check asset + run: | + cd ci + go run ./cmd/check_asset_core + + - name: Checkout core + if: steps.check_asset.outputs.skip_build != 'true' + uses: actions/checkout@v3 + with: + repository: 'niuhuan/pikapika-go-core' + token: ${{ secrets.GH_TOKEN }} + path: 'go' + + - name: Build (ios) + if: steps.check_asset.outputs.skip_build != 'true' && matrix.config.target == 'ios' + run: | + sh scripts/bind-ios.sh + + - name: Build (android) + if: steps.check_asset.outputs.skip_build != 'true' && matrix.config.target == 'android' + run: | + sh scripts/bind-android-debug.sh + + - name: Upload Asset (All) + if: steps.check_asset.outputs.skip_build != 'true' + run: | + zip -r core.zip go/mobile/lib + cd ci + go run ./cmd/upload_asset_core + diff --git a/ci/cmd/check_asset_core/main.go b/ci/cmd/check_asset_core/main.go new file mode 100644 index 0000000..c3b246b --- /dev/null +++ b/ci/cmd/check_asset_core/main.go @@ -0,0 +1,65 @@ +package main + +import ( + "ci/commons" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "os" +) + +func main() { + // get ghToken + ghToken := os.Getenv("GH_TOKEN") + if ghToken == "" { + println("Env ${GH_TOKEN} is not set") + os.Exit(1) + } + // get version + version := commons.LoadVersion() + // get TARGET + target := os.Getenv("TARGET") + if target == "" { + println("Env ${TARGET} is not set") + os.Exit(1) + } + // + var releaseFileName = fmt.Sprintf("core-%v-%v.zip", version.Code, target) + // get version + getReleaseRequest, err := http.NewRequest( + "GET", + fmt.Sprintf("https://api.github.com/repos/%v/%v/releases/tags/%v", commons.Owner, commons.Repo, version.Code), + nil, + ) + if err != nil { + panic(err) + } + getReleaseRequest.Header.Set("User-Agent", commons.Ua) + getReleaseRequest.Header.Set("Authorization", "token "+ghToken) + getReleaseResponse, err := http.DefaultClient.Do(getReleaseRequest) + if err != nil { + panic(err) + } + defer getReleaseResponse.Body.Close() + if getReleaseResponse.StatusCode == 404 { + panic("NOT FOUND RELEASE") + } + buff, err := ioutil.ReadAll(getReleaseResponse.Body) + if err != nil { + panic(err) + } + var release commons.Release + err = json.Unmarshal(buff, &release) + if err != nil { + println(string(buff)) + panic(err) + } + for _, asset := range release.Assets { + if asset.Name == releaseFileName { + println("::set-output name=skip_build::true") + os.Exit(0) + } + } + print("::set-output name=skip_build::false") +} diff --git a/ci/cmd/upload_asset_core/main.go b/ci/cmd/upload_asset_core/main.go new file mode 100644 index 0000000..1e6d770 --- /dev/null +++ b/ci/cmd/upload_asset_core/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "ci/commons" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "os" + "path" +) + +func main() { + // get ghToken + ghToken := os.Getenv("GH_TOKEN") + if ghToken == "" { + println("Env ${GH_TOKEN} is not set") + os.Exit(1) + } + // get version + version := commons.LoadVersion() + // get TARGET + target := os.Getenv("TARGET") + if target == "" { + println("Env ${TARGET} is not set") + os.Exit(1) + } + // + var releaseFileName = fmt.Sprintf("core-%v-%v.zip", version.Code, target) + var releaseFilePath = "core.zip" + var contentLength int64 + releaseFilePath = path.Join("..", releaseFilePath) + info, err := os.Stat(releaseFilePath) + if err != nil { + panic(err) + } + contentLength = info.Size() + // get version + getReleaseRequest, err := http.NewRequest( + "GET", + fmt.Sprintf("https://api.github.com/repos/%v/%v/releases/tags/%v", commons.Owner, commons.Repo, version.Code), + nil, + ) + if err != nil { + panic(err) + } + getReleaseRequest.Header.Set("User-Agent", commons.Ua) + getReleaseRequest.Header.Set("Authorization", "token "+ghToken) + getReleaseResponse, err := http.DefaultClient.Do(getReleaseRequest) + if err != nil { + panic(err) + } + defer getReleaseResponse.Body.Close() + if getReleaseResponse.StatusCode == 404 { + panic("NOT FOUND RELEASE") + } + buff, err := ioutil.ReadAll(getReleaseResponse.Body) + if err != nil { + panic(err) + } + var release commons.Release + err = json.Unmarshal(buff, &release) + if err != nil { + println(string(buff)) + panic(err) + } + file, err := os.Open(releaseFilePath) + if err != nil { + panic(err) + } + defer file.Close() + uploadUrl := fmt.Sprintf("https://uploads.github.com/repos/%v/%v/releases/%v/assets?name=%v", commons.Owner, commons.Repo, release.Id, releaseFileName) + uploadRequest, err := http.NewRequest("POST", uploadUrl, file) + if err != nil { + panic(err) + } + uploadRequest.Header.Set("User-Agent", commons.Ua) + uploadRequest.Header.Set("Authorization", "token "+ghToken) + uploadRequest.Header.Set("Content-Type", "application/octet-stream") + uploadRequest.ContentLength = contentLength + uploadResponse, err := http.DefaultClient.Do(uploadRequest) + if err != nil { + panic(err) + } + if uploadResponse.StatusCode != 201 { + buff, err = ioutil.ReadAll(uploadResponse.Body) + if err != nil { + panic(err) + } + println(string(buff)) + panic("NOT 201") + } +}