From cb1478198437d52b42682219f4324296060a5190 Mon Sep 17 00:00:00 2001 From: gitlawr Date: Sun, 16 Jun 2024 10:56:32 +0800 Subject: [PATCH] ci: init --- .github/workflows/ci.yml | 109 +++++++++++++++++++++++++++++++++++++++ scripts/build | 4 ++ scripts/deps | 8 +++ scripts/package | 11 ++++ scripts/version | 60 +++++++++++++++++++++ 5 files changed, 192 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100755 scripts/build create mode 100755 scripts/deps create mode 100755 scripts/package create mode 100755 scripts/version diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..bf8459e8 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,109 @@ +name: CI + +on: + push: + branches: + - 'main' + tags: + - 'v*.*.*' + pull_request: + branches: + - 'main' + +env: + NODE_VERSION: '21' + +jobs: + deps: + timeout-minutes: 30 + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 1 + persist-credentials: false + - name: Setup Pnpm + uses: pnpm/action-setup@v4 + with: + version: 9.3.0 + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: '${{ env.NODE_VERSION }}' + cache: 'pnpm' + - name: Deps + run: scripts/deps + env: + LOCK: true + - name: Archive Modules + timeout-minutes: 5 + uses: actions/cache/save@v4 + with: + key: node-modules-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }} + path: | + ${{ github.workspace }}/node_modules + + build: + needs: + - deps + if: github.event_name == 'pull_request' + timeout-minutes: 30 + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 1 + persist-credentials: false + - name: Unarchive Node Modules + timeout-minutes: 5 + uses: actions/cache/restore@v4 + with: + key: node-modules-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }} + path: | + ${{ github.workspace }}/node_modules + - name: Build + run: scripts/build + + build-publish: + needs: + - deps + if: github.event_name == 'push' + timeout-minutes: 30 + runs-on: ubuntu-22.04 + strategy: + matrix: + version: ['latest', '${{ github.ref_name }}'] + exclude: + - version: 'main' + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 1 + persist-credentials: false + - name: Unarchive Node Modules + timeout-minutes: 5 + uses: actions/cache/restore@v4 + with: + key: node-modules-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }} + path: | + ${{ github.workspace }}/node_modules + - name: Build + run: scripts/build + - name: Package + run: scripts/package + env: + VERSION: '${{ matrix.version }}' + - name: Release + uses: TencentCloud/cos-action@b0aa648235fb35a1bdd6a77f529eb0ac4c2f1c25 + with: + secret_id: ${{ secrets.CI_TECENTCOS_SECRET_ID }} + secret_key: ${{ secrets.CI_TECENTCOS_SECRET_KEY }} + cos_bucket: ${{ secrets.COS_BUCKET }} + cos_region: ${{ secrets.COS_REGION }} + local_path: dist/${{ matrix.version }}.tar.gz + remote_path: releases/${{ matrix.version }}.tar.gz + accelerate: true + clean: false diff --git a/scripts/build b/scripts/build new file mode 100755 index 00000000..b84955c1 --- /dev/null +++ b/scripts/build @@ -0,0 +1,4 @@ +#!/bin/bash +set -ex + +npm run build ${BASE_ARGS:-} \ No newline at end of file diff --git a/scripts/deps b/scripts/deps new file mode 100755 index 00000000..9260b975 --- /dev/null +++ b/scripts/deps @@ -0,0 +1,8 @@ +#!/bin/bash +set -ex + +if [[ -n "${LOCK}" ]]; then + BASE_ARGS="--frozen-lockfile" +fi + +pnpm install ${BASE_ARGS:-} \ No newline at end of file diff --git a/scripts/package b/scripts/package new file mode 100755 index 00000000..38569c9d --- /dev/null +++ b/scripts/package @@ -0,0 +1,11 @@ +#!/bin/bash +set -ex + +source scripts/version + +version::get_version_vars + +TARBALL=${GIT_VERSION}.tar.gz +echo "Compressing to ${TARBALL}..." +tar -czf ${TARBALL} dist +mv ${TARBALL} dist \ No newline at end of file diff --git a/scripts/version b/scripts/version new file mode 100755 index 00000000..08f2969a --- /dev/null +++ b/scripts/version @@ -0,0 +1,60 @@ +#!/bin/bash + +function version::get_version_vars() { + BUILD_DATE=$(date -u '+%Y-%m-%dT%H:%M:%SZ') + GIT_TREE_STATE="unknown" + GIT_COMMIT="unknown" + GIT_VERSION="unknown" + + # get the git tree state if the source was exported through git archive. + # shellcheck disable=SC2016,SC2050 + if [[ '$Format:%%$' == "%" ]]; then + GIT_TREE_STATE="archive" + GIT_COMMIT='$Format:%H$' + # when a 'git archive' is exported, the '$Format:%D$' below will look + # something like 'HEAD -> release-1.8, tag: v1.8.3' where then 'tag: ' + # can be extracted from it. + if [[ '$Format:%D$' =~ tag:\ (v[^ ,]+) ]]; then + GIT_VERSION="${BASH_REMATCH[1]}" + else + GIT_VERSION="${GIT_COMMIT:0:7}" + fi + # respect specified version. + GIT_VERSION="${VERSION:-${GIT_VERSION}}" + return + fi + + # return directly if not found git client. + if [[ -z "$(command -v git)" ]]; then + # respect specified version. + GIT_VERSION=${VERSION:-${GIT_VERSION}} + return + fi + + # find out git info via git client. + if GIT_COMMIT=$(git rev-parse "HEAD^{commit}" 2>/dev/null); then + # specify as dirty if the tree is not clean. + if git_status=$(git status --porcelain 2>/dev/null) && [[ -n ${git_status} ]]; then + GIT_TREE_STATE="dirty" + else + GIT_TREE_STATE="clean" + fi + + # specify with the tag if the head is tagged. + if GIT_VERSION="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"; then + if git_tag=$(git tag -l --contains HEAD 2>/dev/null | head -n 1 2>/dev/null) && [[ -n ${git_tag} ]]; then + GIT_VERSION="${git_tag}" + fi + fi + + # specify to dev if the tree is dirty. + if [[ "${GIT_TREE_STATE:-dirty}" == "dirty" ]]; then + GIT_VERSION="dev" + elif ! [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then + GIT_VERSION="dev" + fi + + # respect specified version + GIT_VERSION=${VERSION:-${GIT_VERSION}} + fi +} \ No newline at end of file