From 5a7e2605f5ed1af4bf7b5f5a6ec515b7e086d34a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rare=C8=99=20Cosma?= Date: Sat, 28 Sep 2024 14:27:03 +0300 Subject: [PATCH] ci: add github workflows --- .github/workflows/build.yml | 146 +++++++++++++++++++++++++++++++ .github/workflows/cron-cache.yml | 32 +++++++ .github/workflows/release.yml | 54 ++++++++++++ .github/workflows/test.yml | 51 +++++++++++ cliff.toml | 9 +- 5 files changed, 288 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/cron-cache.yml create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..db33122 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,146 @@ +name: build + +on: + push: + branches: + - ci + - "dev/build-yml" + - "!main" + +permissions: + contents: write + +jobs: + get-version: + name: get-version + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Grab the version from Cargo.toml + shell: bash + run: | + cargo_version="v$(head -n5 Cargo.toml | grep -oE "version = \"[0-9\.]+\"" | cut -d'"' -f2)" + echo "VERSION=$cargo_version" >> $GITHUB_ENV + outputs: + version: ${{ env.VERSION }} + + build-binaries: + name: build-binaries + needs: [ "get-version" ] + runs-on: ubuntu-latest + env: + CARGO: cross + TARGET_FLAGS: + TARGET_DIR: ./target + CROSS_VERSION: v0.2.5 + RUST_BACKTRACE: 1 + strategy: + fail-fast: false + matrix: + include: + - build: stable-x86_64 + target: x86_64-unknown-linux-musl + - build: stable-x86 + target: i686-unknown-linux-musl + - build: stable-aarch64 + target: aarch64-unknown-linux-musl + - build: stable-armv7 + target: armv7-unknown-linux-musleabi + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Cache sccache + id: cache_sccache + uses: actions/cache@v4 + with: + path: /home/runner/.cache/sccache + key: ${{ matrix.target }}-sccache-${{ hashFiles('.cargo/config.toml') }} + restore-keys: | + ${{ matrix.target }}-sccache-${{ hashFiles('.cargo/config.toml') }} + ${{ matrix.target }}-sccache- + + - name: Fix cache permissions + shell: bash + run: | + sudo mkdir -p /home/runner/.cache/sccache + sudo chown -R $(id -u) /home/runner/.cache/sccache + sudo chmod -R u+rwX /home/runner/.cache/sccache + + - name: Login to ghcr.io + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GHCR_TOKEN }} + + - name: Prepare Cross + shell: bash + run: | + dir="$RUNNER_TEMP/cross-download" + mkdir "$dir" + echo "$dir" >> $GITHUB_PATH + cd "$dir" + curl -LO "https://github.com/cross-rs/cross/releases/download/$CROSS_VERSION/cross-x86_64-unknown-linux-musl.tar.gz" + tar xf cross-x86_64-unknown-linux-musl.tar.gz + echo "TARGET_FLAGS=--target ${{ matrix.target }}" >> $GITHUB_ENV + echo "TARGET_DIR=./target/${{ matrix.target }}" >> $GITHUB_ENV + + - name: Show command used for Cargo + shell: bash + run: | + echo "cargo command is: ${{ env.CARGO }}" + echo "target flag is: ${{ env.TARGET_FLAGS }}" + echo "target dir is: ${{ env.TARGET_DIR }}" + + - name: Build release binary + shell: bash + run: | + export CROSS_CONTAINER_OPTS="-v /home/runner/.cache/sccache:/sccache:rw -e SCCACHE_DIR=/sccache" + ${{ env.CARGO }} build --release ${{ env.TARGET_FLAGS }} + + - name: Determine archive name + shell: bash + run: | + version="${{ needs.get-version.outputs.version }}" + echo "ARCHIVE=loglux-$version-${{ matrix.target }}" >> $GITHUB_ENV + + - name: Creating directory for archive + shell: bash + run: | + mkdir -p "$ARCHIVE" + cp "target/${{ matrix.target }}/release/loglux" "$ARCHIVE"/ + cp {README.md,UNLICENSE} "$ARCHIVE"/ + + - name: Build archive (Unix) + shell: bash + run: | + tar czf "$ARCHIVE.tar.gz" "$ARCHIVE" + shasum -a 256 "$ARCHIVE.tar.gz" > "$ARCHIVE.tar.gz.sha256" + echo "ASSET=$ARCHIVE.tar.gz" >> $GITHUB_ENV + echo "ASSET_SUM=$ARCHIVE.tar.gz.sha256" >> $GITHUB_ENV + + - name: Upload release archive + if: "github.ref == 'refs/heads/ci' && !contains(github.event.head_commit.message, '[cron]')" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + shell: bash + run: | + version="${{ needs.get-version.outputs.version }}" + gh release upload "$version" ${{ env.ASSET }} ${{ env.ASSET_SUM }} + + promote-release: + if: "github.ref == 'refs/heads/ci' && !contains(github.event.head_commit.message, '[cron]')" + name: promote-release + needs: [ "build-binaries", "get-version" ] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Promote release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + shell: bash + run: | + version="${{ needs.get-version.outputs.version }}" + gh release edit "$version" --draft=false --latest diff --git a/.github/workflows/cron-cache.yml b/.github/workflows/cron-cache.yml new file mode 100644 index 0000000..2ef208f --- /dev/null +++ b/.github/workflows/cron-cache.yml @@ -0,0 +1,32 @@ +name: cron-cache + +on: + push: + branches: + - "dev/cron-cache-yml" + schedule: + - cron: "0 0 */5 * *" + +permissions: + contents: write + +jobs: + create-release: + name: trigger-cron-ci + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + token: ${{ secrets.CI_TOKEN }} + - name: Push a special commit to the CI branch + env: + GITHUB_TOKEN: ${{ secrets.CI_TOKEN }} + shell: bash + run: | + ts="$(date "+%F@%T")" + git config --global user.name 'Github Actions' + git config --global user.email 'gh@getbetter.ro' + git branch -D ci-temp || true + git checkout -b ci-temp + git commit --allow-empty --message="[cron] $ts" + git push -u origin ci-temp:ci --force diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..012f9b2 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,54 @@ +name: release + +on: + push: + tags: + - "v[0-9]+.[0-9]+.[0-9]+" + +permissions: + contents: write + +jobs: + create-release: + name: create-release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + token: ${{ secrets.CI_TOKEN }} + - name: Fetch tags + run: git fetch --prune --unshallow --tags --force + - name: Get the release version from the tag + if: env.VERSION == '' + run: echo "VERSION=${{ github.ref_name }}" >> $GITHUB_ENV + - name: Show the version + run: | + echo "version is: $VERSION" + - name: Check that tag version and Cargo.toml version are the same + shell: bash + run: | + cargo_version="$(echo $VERSION | sed 's/^v//')" + if ! grep -q "version = \"$cargo_version\"" Cargo.toml; then + echo "version does not match Cargo.toml" >&2 + exit 1 + fi + - name: Generate a changelog + uses: orhun/git-cliff-action@v4 + id: git-cliff + with: + config: cliff.toml + args: --latest --strip all + env: + REPO_PREFIX: ${{ github.server_url }}/${{ github.repository }}/ + - name: Create GitHub release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: gh release create $VERSION --draft --verify-tag --title $VERSION --notes "${{ steps.git-cliff.outputs.content }}" + - name: Push to CI branch + env: + GITHUB_TOKEN: ${{ secrets.CI_TOKEN }} + shell: bash + run: | + git branch -D ci-temp || true + git checkout -b ci-temp + git push -u origin ci-temp:ci --force diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..a45240f --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,51 @@ +name: test + +on: + push: + branches: + - "main" + - "dev/test-yml" + +permissions: + contents: read + +jobs: + test: + name: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install rust + shell: bash + run: | + RUST_VERSION="$(grep -oP 'channel = "\K[^"]+' rust-toolchain.toml)" + rustup toolchain install $RUST_VERSION --profile minimal + - uses: extractions/setup-just@v2 + - name: Cache sccache + id: cache_sccache + uses: actions/cache@v4 + with: + path: /home/runner/.cache/sccache + key: test-sccache-v0-${{ hashFiles('.cargo/config.toml') }} + restore-keys: | + test-sccache-v0-${{ hashFiles('.cargo/config.toml') }} + test-sccache-v0- + - name: Prep sccache + shell: bash + run: | + curl --fail --location "https://github.com/mozilla/sccache/releases/download/${SCCACHE_VERSION}/sccache-${SCCACHE_VERSION}-x86_64-unknown-linux-musl.tar.gz" --output /tmp/sccache.tar.gz + tar --directory "/usr/local/bin" -xzvf "/tmp/sccache.tar.gz" --strip-components 1 --wildcards "*/sccache" + rm /tmp/sccache.tar.gz + + sudo mkdir -p /home/runner/.cache/sccache + sudo chown -R $(id -u) /home/runner/.cache/sccache + sudo chmod -R u+rwX /home/runner/.cache/sccache + echo "RUSTC_WRAPPER=sccache" >> $GITHUB_ENV + echo "SCCACHE_DIR=/home/runner/.cache/sccache" >> $GITHUB_ENV + env: + CARGO_UDEPS_VERSION: "v0.1.50" + SCCACHE_VERSION: "v0.8.1" + - name: Run the CI tests + run: | + just ci + sccache -s diff --git a/cliff.toml b/cliff.toml index 2d1d8c1..75f6e8c 100644 --- a/cliff.toml +++ b/cliff.toml @@ -1,5 +1,6 @@ [changelog] body = """ +{% set repo_prefix = get_env(name="REPO_PREFIX", default="") %} {% if version %}\ ## {{ version | trim_start_matches(pat="v") }} - {{ timestamp | date(format="%Y-%m-%d") }} {% else %}\ @@ -9,13 +10,13 @@ body = """ {% if previous.version and version %} ### Diff - [{{ previous.version }}...{{ version }}](compare/{{ previous.version }}...{{ version }}) + [{{ previous.version }}...{{ version }}]({{repo_prefix}}compare/{{ previous.version }}...{{ version }}) {% endif %}\ {% endif %}\ {% for group, commits in commits | group_by(attribute="group") %} ### {{ group | upper_first }} {% for commit in commits %} - - {{ commit.message | split(pat="\n") | first | trim }} [#{{ commit.id | truncate(length=7, end="") }}](commit/{{ commit.id }})\ + - {{ commit.message | split(pat="\n") | first | trim }} [#{{ commit.id | truncate(length=7, end="") }}]({{repo_prefix}}commit/{{ commit.id }})\ {% endfor %} {% endfor %}\n """ @@ -24,6 +25,6 @@ body = """ filter_unconventional = false conventional_commits = false commit_parsers = [ - { message = "^chore: Release", skip = true}, - { message = "^", group = "Changelog"}, + { message = "^chore: Release", skip = true }, + { message = "^", group = "Changelog" }, ]