From e353df87e7a3c109e5904412cb17f2ce4d7893ee Mon Sep 17 00:00:00 2001 From: yuchanns Date: Fri, 2 Aug 2024 12:58:29 +0800 Subject: [PATCH] ci: schedule with cron to auto release. Signed-off-by: Hanchin Hsieh --- .github/scripts/setup_version.js | 40 +++++++++++++++ .github/workflows/build_artifacts.yaml | 67 ++++++++++++++++++-------- 2 files changed, 86 insertions(+), 21 deletions(-) create mode 100644 .github/scripts/setup_version.js diff --git a/.github/scripts/setup_version.js b/.github/scripts/setup_version.js new file mode 100644 index 0000000..81cf448 --- /dev/null +++ b/.github/scripts/setup_version.js @@ -0,0 +1,40 @@ +const semver = require('semver') + +const getLatestTag = async (github, owner, repo, tag_format) => { + const { status, data } = await github.rest.git.listMatchingRefs({ + owner, + repo, + ref: `tags/${tag_format}`, + }) + if (status != 200) { + throw new Error(`Failed to fetch tags for ${owner}/${repo}`) + } + const tags = data.map((ref) => { + const version = ref.ref.replace(`refs/tags/${tag_format}`, '') + return version + }) + if (tags.length === 0) { + return '0.0.0' + } + max_version = tags.reduce((a, b) => semver.gt(a, b) ? a : b) ?? '0.0.0' + return max_version +} + + +module.exports = async ({ github }) => { + const bindingTag = await getLatestTag(github, 'apache', 'opendal', 'bindings/go/v') + const serviceTag = await getLatestTag(github, 'apache', 'opendal-go-services', 'v') + + const opendalGoVersion = semver.gt(bindingTag, serviceTag) ? `v${bindingTag}` : '' + const opendalCoreVersion = `v${await getLatestTag(github, "apache", "opendal", "v")}` + + console.log(`opendal_core_version=${opendalCoreVersion}`) + console.log(`opendal_go_version=${opendalGoVersion}`) + + const fs = require('fs') + const output = process.env.GITHUB_OUTPUT + + fs.appendFileSync(output, `opendal_core_version=${opendalCoreVersion}\n`) + fs.appendFileSync(output, `opendal_go_version=${opendalGoVersion}\n`) +} + diff --git a/.github/workflows/build_artifacts.yaml b/.github/workflows/build_artifacts.yaml index 82ee2a3..6bc9fb8 100644 --- a/.github/workflows/build_artifacts.yaml +++ b/.github/workflows/build_artifacts.yaml @@ -1,20 +1,34 @@ name: build_artifacts on: - repository_dispatch: - types: [ build ] workflow_dispatch: - inputs: - opendal_core_version: - description: "Version of Apache OpenDAL Core" - required: true - type: string - opendal_go_version: - description: "Version of Apache OpenDAL Go Binding" - required: false - type: string + schedule: + - cron: '*/5 * * * *' + jobs: + version_check: + runs-on: ubuntu-latest + outputs: + opendal_core_version: ${{ steps.set-version.outputs.opendal_core_version }} + opendal_go_version: ${{ steps.set-version.outputs.opendal_go_version }} + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v3 + with: + node-version: '20.x' + - name: Install Dependencies + run: npm install semver + - uses: actions/github-script@v7 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + script: | + const script = require('.github/scripts/setup_version.js') + await script({github}) + matrix: + needs: [ version_check ] runs-on: ubuntu-latest + if: ${{ needs.version_check.outputs.opendal_go_version != '' }} outputs: matrix: ${{ steps.set-matrix.outputs.matrix }} steps: @@ -27,7 +41,7 @@ jobs: echo "$MATRIX" | jq . echo "matrix=$MATRIX" >> $GITHUB_OUTPUT build: - needs: [ matrix ] + needs: [ version_check, matrix ] runs-on: ${{ matrix.os }} strategy: fail-fast: false @@ -36,7 +50,7 @@ jobs: - uses: actions/checkout@v4 with: repository: "apache/opendal" - ref: ${{ inputs.opendal_core_version || github.event.client_payload.opendal_core_version }} + ref: ${{ needs.version_check.outputs.opendal_core_version }} - uses: actions/checkout@v4 with: path: "tools" @@ -78,12 +92,12 @@ jobs: zstd -22 ./target/$TARGET/release/libopendal_c.so -o ./libopendal_c.$TARGET.so.zst - uses: actions/upload-artifact@v4 with: - name: "libopendal_c_${{ inputs.opendal_core_version || github.event.client_payload.opendal_core_version }}_${{ matrix.service }}_${{ matrix.build.target }}" + name: "libopendal_c_${{ needs.version_check.outputs.opendal_core_version }}_${{ matrix.service }}_${{ matrix.build.target }}" if-no-files-found: "error" path: "bindings/c/libopendal_c.${{ matrix.build.target }}.so.zst" overwrite: "true" generate: - needs: [ matrix, build ] + needs: [ version_check, matrix, build ] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -92,13 +106,14 @@ jobs: - name: Generate Template env: MATRIX: ${{ needs.matrix.outputs.matrix }} - VERSION: ${{ inputs.opendal_core_version || github.event.client_payload.opendal_core_version }} + VERSION: ${{ needs.version_check.outputs.opendal_core_version }} working-directory: internal/generate run: | go run generate.go - name: Auto Commit env: - VERSION: ${{ inputs.opendal_core_version || github.event.client_payload.opendal_core_version }} + VERSION: ${{ needs.version_check.outputs.opendal_core_version }} + TAG: ${{ needs.version_check.outputs.opendal_go_version }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | git config --global user.name "Github Actions" @@ -106,20 +121,30 @@ jobs: git add -A git commit --allow-empty -m "Auto commit by GitHub Actions $VERSION" git push -f --set-upstream origin main + - name: Auto Tag + if: ${{ needs.version_check.outputs.opendal_go_version != '' }} + env: + TAG: ${{ needs.version_check.outputs.opendal_go_version }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + # The tag is used for version_check only. + git tag $TAG + git push -f origin $TAG tag: - needs: [ matrix, build, generate ] + needs: [ version_check, matrix, build, generate ] runs-on: ubuntu-latest + if: ${{ needs.version_check.outputs.opendal_go_version != '' }} strategy: fail-fast: false matrix: service: ${{ fromJson(needs.matrix.outputs.matrix).service }} steps: - uses: actions/checkout@v4 - - name: Auto Tag - if: ${{ inputs.opendal_go_version != '' || github.event.client_payload.opendal_go_version != '' }} + - name: Auto Service Tag env: SERVICE: ${{ matrix.service }} - VERSION: ${{ inputs.opendal_go_version || github.event.client_payload.opendal_go_version }} + VERSION: ${{ needs.version_check.outputs.opendal_go_version }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | SERVICE=$(echo $SERVICE | tr '-' '_') TAG="$SERVICE/$VERSION"