From ceb547f8f4a0b93c3a78a98f61e6895bd3633018 Mon Sep 17 00:00:00 2001 From: Will Date: Tue, 3 Sep 2024 15:04:13 +0900 Subject: [PATCH] WIP: publish kentik agent package --- .github/actions/bundle/action.mjs | 31 ++++++ .github/actions/bundle/action.yml | 33 +++++++ .github/actions/bundle/package.json | 10 ++ .github/actions/package/action.yml | 34 +++++++ .github/actions/version/action.mjs | 57 +++++++++++ .github/actions/version/action.yml | 23 +++++ .github/actions/version/package.json | 9 ++ .github/workflows/build.yml | 139 ++++++++++++++++----------- 8 files changed, 281 insertions(+), 55 deletions(-) create mode 100644 .github/actions/bundle/action.mjs create mode 100644 .github/actions/bundle/action.yml create mode 100644 .github/actions/bundle/package.json create mode 100644 .github/actions/package/action.yml create mode 100644 .github/actions/version/action.mjs create mode 100644 .github/actions/version/action.yml create mode 100644 .github/actions/version/package.json diff --git a/.github/actions/bundle/action.mjs b/.github/actions/bundle/action.mjs new file mode 100644 index 0000000..b2ec6ed --- /dev/null +++ b/.github/actions/bundle/action.mjs @@ -0,0 +1,31 @@ +import * as core from '@actions/core'; +import * as exec from '@actions/exec'; +import * as io from '@actions/io'; + +let binary = process.env.BINARY; +let version = process.env.VERSION; + +let [name, arch, _, os] = binary.split('-'); + +switch (arch) { + case 'aarch64': + arch = 'arm64'; + break; + case 'armv7': + arch = 'arm'; + break; + case 'x86_64': + arch = 'amd64'; + break; +} + +let bundle = `${name}_${version}_${os}_${arch}.tgz`; +let prefix = `${name}-${version}`; + +let dir = `${prefix}/bin`; +await io.mkdirP(dir); +await io.cp(name, `${dir}/${name}`); + +await exec.exec(`tar -czvf ${bundle} ${prefix}`); + +core.setOutput('bundle', bundle); diff --git a/.github/actions/bundle/action.yml b/.github/actions/bundle/action.yml new file mode 100644 index 0000000..a5e2fba --- /dev/null +++ b/.github/actions/bundle/action.yml @@ -0,0 +1,33 @@ +name: bundle + +inputs: + artifact: + required: true + version: + required: true + +outputs: + bundle: + value: ${{ steps.bundle.outputs.bundle }} + +runs: + using: composite + steps: + - uses: actions/download-artifact@v4 + with: + name: ${{ inputs.artifact }} + - uses: actions/setup-node@v4 + with: + node-version: 20 + - run: npm install $GITHUB_ACTION_PATH + shell: bash + - run: node $GITHUB_ACTION_PATH/action.mjs + env: + BINARY: ${{ inputs.artifact }} + VERSION: ${{ inputs.version }} + shell: bash + id: bundle + - uses: actions/upload-artifact@v4 + with: + name: ${{ steps.bundle.outputs.bundle }} + path: ${{ steps.bundle.outputs.bundle }} diff --git a/.github/actions/bundle/package.json b/.github/actions/bundle/package.json new file mode 100644 index 0000000..1da7711 --- /dev/null +++ b/.github/actions/bundle/package.json @@ -0,0 +1,10 @@ +{ + "name": "bundle", + "type": "module", + "version": "0.0.1", + "dependencies": { + "@actions/core": "^1.10.1", + "@actions/exec": "^1.1.1", + "@actions/io": "^1.1.3" + } +} diff --git a/.github/actions/package/action.yml b/.github/actions/package/action.yml new file mode 100644 index 0000000..0df8475 --- /dev/null +++ b/.github/actions/package/action.yml @@ -0,0 +1,34 @@ +name: package + +inputs: + artifact: + required: true + version: + required: true + arch: + required: true + format: + required: true + +outputs: + bundle: + value: ${{ steps.bundle.outputs.bundle }} + +runs: + using: composite + steps: + - uses: actions/download-artifact@v4 + with: + name: ${{ inputs.artifact }} + - uses: kentik/pkg@master + with: + name: ${{ github.event.repository.name }} + version: ${{ inputs.version }} + arch: ${{ inputs.arch }} + format: ${{ inputs.format }} + package: package.yml + id: package + - uses: actions/upload-artifact@v4 + with: + name: ${{ steps.package.outputs.package }} + path: ${{ steps.package.outputs.package }} diff --git a/.github/actions/version/action.mjs b/.github/actions/version/action.mjs new file mode 100644 index 0000000..56b37f1 --- /dev/null +++ b/.github/actions/version/action.mjs @@ -0,0 +1,57 @@ +import { env } from 'node:process'; +import { Octokit } from '@octokit/rest'; +import { notice, setOutput } from '@actions/core'; +import { inc, parse, valid } from 'semver'; + +let octokit = new Octokit({ auth: env.GITHUB_TOKEN }); +let github = octokit.rest; + +let [owner, repo] = env.GITHUB_REPOSITORY.split('/'); +let ref = env.GITHUB_REF; +let build = env.GITHUB_RUN_NUMBER ?? 0; + +let result = { + 'version': 'INVALID', + 'prerelease': false, + 'publish': [], + 'release': false, +} + +if (ref.startsWith('refs/tags/')) { + let [,, tag] = ref.split('/'); + let version = parse(`v${tag}`); + + if (version) { + result.version = version.version; + result.prerelease = version.prerelease.length > 0; + result.publish = ['bundle', 'package']; + result.release = true; + } +} else { + let branch = 'unknown'; + + if (ref.startsWith('refs/heads/')) { + branch = ref.split('/')[2]; + } + + let res = await github.repos.listTags({ owner, repo }); + let tag = res?.data?.[0]?.name ?? '0.0.0'; + let version = parse(`v${tag}`); + + if (version) { + version = inc(version, 'prerelease', `${branch}.${build}`, false); + result.version = version; + result.prerelease = true; + result.publish = ['bundle']; + result.release = false; + } +} + +notice(`version ${result.version}`); + +console.log(result); + +setOutput('version', result.version); +setOutput('prerelease', result.prerelease); +setOutput('publish', result.publish.join(',')); +setOutput('release', result.release); diff --git a/.github/actions/version/action.yml b/.github/actions/version/action.yml new file mode 100644 index 0000000..f041ae2 --- /dev/null +++ b/.github/actions/version/action.yml @@ -0,0 +1,23 @@ +name: version + +outputs: + version: + value: ${{ steps.version.outputs.version }} + prerelease: + value: ${{ steps.version.outputs.prerelease }} + publish: + value: ${{ steps.version.outputs.publish }} + release: + value: ${{ steps.version.outputs.release }} + +runs: + using: composite + steps: + - uses: actions/setup-node@v4 + with: + node-version: 20 + - run: npm install $GITHUB_ACTION_PATH + shell: bash + - run: node $GITHUB_ACTION_PATH/action.mjs + shell: bash + id: version diff --git a/.github/actions/version/package.json b/.github/actions/version/package.json new file mode 100644 index 0000000..84c8eed --- /dev/null +++ b/.github/actions/version/package.json @@ -0,0 +1,9 @@ +{ + "name": "av", + "type": "module", + "dependencies": { + "@actions/core": "^1.10.1", + "@octokit/rest": "^21.0.2", + "semver": "^7.6.3" + } +} diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 199c399..7832548 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -52,26 +52,36 @@ jobs: version: runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: ./.github/actions/version + id: version outputs: - version: ${{ steps.version.outputs.version }} + version: ${{ steps.version.outputs.version }} + prerelease: ${{ steps.version.outputs.prerelease }} + publish: ${{ steps.version.outputs.publish }} + release: ${{ steps.version.outputs.release }} + + bundle: + runs-on: ubuntu-latest + strategy: + matrix: + target: + - x86_64-unknown-linux-musl + - aarch64-unknown-linux-musl + - armv7-unknown-linux-musleabihf steps: - uses: actions/checkout@v4 with: fetch-depth: 0 - - id: version - run: | - git describe --always --tags > version - date -u +%Y%m%d%H%M%S >> version - case "${{ startsWith(github.ref, 'refs/tags') }}" in - true) VERSION=`sed -n 1p version` ;; - false) VERSION=`sed -n 2p version` ;; - esac - echo "::notice ::kprobe version: $VERSION" - echo "version=$VERSION" >> $GITHUB_OUTPUT - - uses: actions/upload-artifact@v4 + - uses: ./.github/actions/bundle with: - name: version - path: version + artifact: kprobe-${{ matrix.target }} + version: "v${{ needs.version.outputs.version }}" + if: ${{ contains(needs.version.outputs.publish, 'bundle') }} + needs: [build, version] package: runs-on: ubuntu-latest @@ -85,64 +95,83 @@ jobs: arch: armv7 - name: x86_64-unknown-linux-musl arch: x86_64 - continue-on-error: ${{ !startsWith(matrix.target, 'x86_64') }} steps: - uses: actions/checkout@v4 with: fetch-depth: 0 - - uses: actions/download-artifact@v4 - with: - name: kprobe-${{ matrix.target.name }} - - id: package - uses: kentik/pkg@master + - uses: ./.github/actions/package with: - name: ${{ github.event.repository.name }} + artifact: kprobe-${{ matrix.target.name }} version: ${{ needs.version.outputs.version }} arch: ${{ matrix.target.arch }} format: ${{ matrix.format }} - package: package.yml - - uses: actions/upload-artifact@v4 - with: - name: ${{ steps.package.outputs.package }} - path: ${{ steps.package.outputs.package }} + if: ${{ contains(needs.version.outputs.publish, 'package') }} needs: [build, version] - publish-packages: + publish-bundle: runs-on: ubuntu-latest steps: - - uses: ruby/setup-ruby@v1 + - uses: actions/checkout@v4 with: - ruby-version: 2.7 + fetch-depth: 0 - uses: actions/download-artifact@v4 + - run: ls -lR + - uses: actions/create-github-app-token@v1 with: - path: packages - - name: publish packages - run: | - ls -ltR packages + app-id: ${{ vars.KENTIK_MACHINERY_APP_ID }} + private-key: ${{ secrets.KENTIK_MACHINERY_PRIVATE_KEY }} + owner: ${{ github.repository_owner }} + repositories: packages + id: token + - uses: ncipollo/release-action@v1 + with: + token: ${{ steps.token.outputs.token }} + repo: packages + tag: "kprobe/v${{ needs.version.outputs.version }}" + prerelease: ${{ needs.version.outputs.prerelease }} + allowUpdates: false + removeArtifacts: true + artifacts: "kprobe*.tgz/kprobe*.tgz" + if: ${{ contains(needs.version.outputs.publish, 'bundle') }} + needs: [bundle, version] - gem install package_cloud + # publish-packages: + # runs-on: ubuntu-latest + # steps: + # - uses: ruby/setup-ruby@v1 + # with: + # ruby-version: 2.7 + # - uses: actions/download-artifact@v4 + # with: + # path: packages + # - name: publish packages + # run: | + # ls -ltR packages - case "${{ startsWith(github.ref, 'refs/tags') }}" in - true) REPO="${{ github.event.repository.name }}" ;; - false) REPO="${{ github.event.repository.name }}-dev" ;; - esac + # gem install package_cloud - for deb in packages/*.deb/*.deb; do - package_cloud push kentik/$REPO/debian/jessie $deb - package_cloud push kentik/$REPO/debian/stretch $deb - package_cloud push kentik/$REPO/debian/buster $deb - package_cloud push kentik/$REPO/debian/bullseye $deb + # case "${{ needs.version.outputs.prerelease }}" in + # true) REPO="${{ github.event.repository.name }}-dev" ;; + # false) REPO="${{ github.event.repository.name }}" ;; + # esac - package_cloud push kentik/$REPO/ubuntu/focal $deb - package_cloud push kentik/$REPO/ubuntu/bionic $deb - package_cloud push kentik/$REPO/ubuntu/jammy $deb - done + # for deb in packages/*.deb/*.deb; do + # package_cloud push kentik/$REPO/debian/jessie $deb + # package_cloud push kentik/$REPO/debian/stretch $deb + # package_cloud push kentik/$REPO/debian/buster $deb + # package_cloud push kentik/$REPO/debian/bullseye $deb - for rpm in packages/*.rpm/*.rpm; do - package_cloud push kentik/$REPO/el/7 $rpm - package_cloud push kentik/$REPO/el/8 $rpm - package_cloud push kentik/$REPO/el/9 $rpm - done - env: - PACKAGECLOUD_TOKEN: ${{ secrets.PACKAGECLOUD_TOKEN }} - needs: [package, version] + # package_cloud push kentik/$REPO/ubuntu/focal $deb + # package_cloud push kentik/$REPO/ubuntu/bionic $deb + # package_cloud push kentik/$REPO/ubuntu/jammy $deb + # done + + # for rpm in packages/*.rpm/*.rpm; do + # package_cloud push kentik/$REPO/el/7 $rpm + # package_cloud push kentik/$REPO/el/8 $rpm + # package_cloud push kentik/$REPO/el/9 $rpm + # done + # env: + # PACKAGECLOUD_TOKEN: ${{ secrets.PACKAGECLOUD_TOKEN }} + # if ${{ contains(needs.version.outputs.publish, 'package') }} + # needs: [package, version]