Skip to content

Commit

Permalink
ci: add github workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
rarescosma committed Sep 28, 2024
1 parent 4f2f1c6 commit 5a7e260
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 4 deletions.
146 changes: 146 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions .github/workflows/cron-cache.yml
Original file line number Diff line number Diff line change
@@ -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 '[email protected]'
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
54 changes: 54 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 5 additions & 4 deletions cliff.toml
Original file line number Diff line number Diff line change
@@ -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 %}\
Expand All @@ -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
"""
Expand All @@ -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" },
]

0 comments on commit 5a7e260

Please sign in to comment.