Skip to content

Commit

Permalink
ci: init
Browse files Browse the repository at this point in the history
  • Loading branch information
gitlawr committed Jun 17, 2024
1 parent 9fa61b9 commit f4a1ef4
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 0 deletions.
142 changes: 142 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
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
ci-hosted:
needs:
- 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: Unarchive 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
env:
COS_BUCKET: '${{ secrets.COS_BUCKET }}'
COS_REGION: '${{ secrets.COS_REGION }}'
- name: Release
uses: TencentCloud/cos-action@b0aa648235fb35a1bdd6a77f529eb0ac4c2f1c25
if: github.event_name == 'push'
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
remote_path: latest
accelerate: true
clean: false

ci-emebeded:
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: Setup Pnpm
uses: pnpm/action-setup@v4
with:
version: 9.3.0
- name: Setup Node
timeout-minutes: 5
uses: actions/setup-node@v4
with:
node-version: '${{ env.NODE_VERSION }}'
cache: 'pnpm'
- 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
8 changes: 8 additions & 0 deletions scripts/deps
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -ex

if [[ -n "${LOCK}" ]]; then
BASE_ARGS="--frozen-lockfile"
fi

pnpm install ${BASE_ARGS:-}
11 changes: 11 additions & 0 deletions scripts/package
Original file line number Diff line number Diff line change
@@ -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
60 changes: 60 additions & 0 deletions scripts/version
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit f4a1ef4

Please sign in to comment.