fix(ci): use pre-installed PostgreSQL and generate sqlite3.def on Win… #191
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CI | |
| on: | |
| pull_request: | |
| push: | |
| branches: | |
| - master | |
| jobs: | |
| build: | |
| name: Auto Build CI | |
| runs-on: ${{ matrix.os }} | |
| strategy: | |
| matrix: | |
| os: [ ubuntu-latest, windows-latest, macOS-latest ] | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@v4 | |
| - name: Install Rust toolchain | |
| run: | | |
| rustup update stable | |
| rustup default stable | |
| rustup component add rustfmt clippy | |
| - name: Cargo Clippy | |
| run: cargo clippy -- -D warnings | |
| - name: Cargo Fmt Check | |
| run: cargo fmt --all -- --check | |
| - name: Cargo Build | |
| run: cargo build | |
| - name: Setup PostgreSQL & MySQL & SQLite (for ubuntu) | |
| if: matrix.os == 'ubuntu-latest' | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get upgrade | |
| sudo apt-get install -y libpq-dev postgresql libmysqlclient-dev mysql-client libsqlite3-dev sqlite3 | |
| echo "host all all 127.0.0.1/32 md5" > sudo tee -a /etc/postgresql/10/main/pg_hba.conf | |
| sudo service postgresql restart && sleep 3 | |
| sudo -u postgres createuser casbin_rs | |
| sudo -u postgres createdb casbin | |
| sudo -u postgres psql -c "alter user casbin_rs with encrypted password 'casbin_rs'; grant all privileges on database casbin to casbin_rs;" | |
| sudo -u postgres psql -c "GRANT USAGE, CREATE ON SCHEMA public TO casbin_rs;" | |
| sudo -u postgres psql -c "GRANT ALL ON DATABASE casbin TO casbin_rs;" | |
| sudo -u postgres psql -c "ALTER DATABASE casbin OWNER TO casbin_rs;" | |
| sudo service postgresql restart && sleep 3 | |
| sudo systemctl start mysql.service | |
| mysql -e "create user 'casbin_rs'@'localhost' identified by 'casbin_rs'; create database casbin; grant all on \`casbin\`.* to 'casbin_rs'@'localhost';" -uroot -proot | |
| - name: Setup PostgreSQL & MySQL & SQLite (for macOS) | |
| if: matrix.os == 'macOS-latest' | |
| run: | | |
| brew update | |
| brew install postgresql mariadb | |
| brew services start postgresql | |
| # pg_ctl -D /usr/local/var/postgres start | |
| sleep 3 | |
| createuser casbin_rs | |
| createdb casbin | |
| psql postgres -c "alter user casbin_rs with encrypted password 'casbin_rs'; grant all privileges on database casbin to casbin_rs;" | |
| psql postgres -c "GRANT USAGE, CREATE ON SCHEMA public TO casbin_rs;" | |
| psql postgres -c "GRANT ALL ON DATABASE casbin TO casbin_rs;" | |
| psql postgres -c "ALTER DATABASE casbin OWNER TO casbin_rs;" | |
| #echo "/usr/local/opt/mariadb@10.5/bin" >> $GITHUB_PATH | |
| #/usr/local/opt/mariadb@10.5/bin/mysql_install_db | |
| #/usr/local/opt/mariadb@10.5/bin/mysql.server start | |
| brew services start mariadb | |
| sleep 3 | |
| mysql -e "create user 'casbin_rs'@'localhost' identified by 'casbin_rs'; create database casbin; grant all on \`casbin\`.* to 'casbin_rs'@'localhost';" -urunner | |
| #echo "MYSQLCLIENT_LIB_DIR=/usr/local/opt/mariadb@10.5/lib" >> $GITHUB_ENV | |
| - name: Setup PostgreSQL & MySQL & SQLite (for windows) | |
| if: matrix.os == 'windows-latest' | |
| shell: pwsh | |
| run: | | |
| # PostgreSQL - use the pre-installed binaries, init a fresh data dir | |
| $pgDir = (Get-ChildItem 'C:\Program Files\PostgreSQL' -Directory | | |
| Sort-Object Name -Descending | Select-Object -First 1).FullName | |
| $pgBin = "$pgDir\bin" | |
| $pgData = "C:\pgdata" | |
| & "$pgBin\initdb" -D "$pgData" -U postgres -E UTF8 --auth=trust | |
| & "$pgBin\pg_ctl" start -D "$pgData" -w -t 60 | |
| & "$pgBin\psql" -U postgres -c "CREATE USER casbin_rs WITH ENCRYPTED PASSWORD 'casbin_rs';" | |
| & "$pgBin\psql" -U postgres -c "CREATE DATABASE casbin OWNER casbin_rs;" | |
| & "$pgBin\psql" -U postgres -d casbin -c "GRANT USAGE, CREATE ON SCHEMA public TO casbin_rs;" | |
| & "$pgBin\psql" -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE casbin TO casbin_rs;" | |
| echo "PG_DIR=$pgDir" >> $env:GITHUB_ENV | |
| # MySQL - install, initialize data dir, start as background process (avoids service issues) | |
| choco install mysql -y | |
| $mysqlBin = 'C:\tools\mysql\current\bin' | |
| $mysqlData = 'C:\mysqldata' | |
| & "$mysqlBin\mysqld" --initialize-insecure --datadir="$mysqlData" | |
| Start-Process "$mysqlBin\mysqld" -ArgumentList "--datadir=$mysqlData --port=3306 --default-authentication-plugin=mysql_native_password" -WindowStyle Hidden | |
| Start-Sleep -Seconds 15 | |
| & "$mysqlBin\mysql" -uroot -h 127.0.0.1 -P 3306 -e "CREATE USER 'casbin_rs'@'localhost' IDENTIFIED WITH mysql_native_password BY 'casbin_rs'; CREATE DATABASE casbin; GRANT ALL ON ``casbin``.* TO 'casbin_rs'@'localhost'; FLUSH PRIVILEGES;" | |
| # SQLite - install, then build sqlite3.lib from sqlite3.dll via generated .def | |
| choco install sqlite -y | |
| $sqliteDir = 'C:\ProgramData\chocolatey\lib\SQLite\tools' | |
| # Find vcvars64.bat across VS editions | |
| $vcvars = @( | |
| 'C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat', | |
| 'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat', | |
| 'C:\Program Files\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat', | |
| 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat' | |
| ) | Where-Object { Test-Path $_ } | Select-Object -First 1 | |
| if (-not $vcvars) { throw "Could not find vcvars64.bat" } | |
| # Capture dumpbin output directly (avoids file-redirect issues) | |
| $dumpbinOut = cmd /c "`"$vcvars`" > nul 2>&1 && dumpbin /exports `"$sqliteDir\sqlite3.dll`"" | |
| $names = [regex]::Matches(($dumpbinOut -join "`n"), '^\s+\d+\s+[0-9A-Fa-f]+\s+[0-9A-Fa-f]+\s+(\w+)', 'Multiline') | | |
| ForEach-Object { $_.Groups[1].Value } | |
| (@("LIBRARY sqlite3", "EXPORTS") + $names) | Out-File "$sqliteDir\sqlite3.def" -Encoding ASCII | |
| # Build sqlite3.lib | |
| cmd /c "`"$vcvars`" > nul 2>&1 && lib /machine:x64 /def:`"$sqliteDir\sqlite3.def`" /out:`"$sqliteDir\sqlite3.lib`"" | |
| - name: Set environment variables (for windows) | |
| if: matrix.os == 'windows-latest' | |
| shell: pwsh | |
| run: | | |
| echo "$env:PG_DIR\bin" >> $env:GITHUB_PATH | |
| echo "PQ_LIB_DIR=$env:PG_DIR\lib" >> $env:GITHUB_ENV | |
| echo "MYSQLCLIENT_LIB_DIR=C:\tools\mysql\current\lib" >> $env:GITHUB_ENV | |
| echo "SQLITE3_LIB_DIR=C:\ProgramData\chocolatey\lib\SQLite\tools" >> $env:GITHUB_ENV | |
| - name: Create SQLite DB | |
| run: | | |
| touch casbin.db | |
| - name: Create PostgresSQL Table | |
| run: psql -c "CREATE TABLE IF NOT EXISTS casbin_rule ( | |
| id SERIAL PRIMARY KEY, | |
| ptype VARCHAR NOT NULL, | |
| v0 VARCHAR NOT NULL, | |
| v1 VARCHAR NOT NULL, | |
| v2 VARCHAR NOT NULL, | |
| v3 VARCHAR NOT NULL, | |
| v4 VARCHAR NOT NULL, | |
| v5 VARCHAR NOT NULL, | |
| CONSTRAINT unique_key_sqlx_adapter UNIQUE(ptype, v0, v1, v2, v3, v4, v5) | |
| );" postgres://casbin_rs:casbin_rs@127.0.0.1:5432/casbin | |
| - name: Create MySQL Table | |
| run: | | |
| mysql -ucasbin_rs -pcasbin_rs -e "USE casbin; CREATE TABLE IF NOT EXISTS casbin_rule ( | |
| id INT NOT NULL AUTO_INCREMENT, | |
| ptype VARCHAR(12) NOT NULL, | |
| v0 VARCHAR(128) NOT NULL, | |
| v1 VARCHAR(128) NOT NULL, | |
| v2 VARCHAR(128) NOT NULL, | |
| v3 VARCHAR(128) NOT NULL, | |
| v4 VARCHAR(128) NOT NULL, | |
| v5 VARCHAR(128) NOT NULL, | |
| PRIMARY KEY(id), | |
| CONSTRAINT unique_key_sqlx_adapter UNIQUE(ptype, v0, v1, v2, v3, v4, v5) | |
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8;" | |
| - name: Create SQLite Table | |
| run: | | |
| sqlite3 casbin.db -cmd "CREATE TABLE IF NOT EXISTS casbin_rule ( | |
| id INTEGER PRIMARY KEY, | |
| ptype VARCHAR(12) NOT NULL, | |
| v0 VARCHAR(128) NOT NULL, | |
| v1 VARCHAR(128) NOT NULL, | |
| v2 VARCHAR(128) NOT NULL, | |
| v3 VARCHAR(128) NOT NULL, | |
| v4 VARCHAR(128) NOT NULL, | |
| v5 VARCHAR(128) NOT NULL, | |
| CONSTRAINT unique_key_sqlx_adapter UNIQUE(ptype, v0, v1, v2, v3, v4, v5) | |
| );" | |
| - name: Cargo Test For PostgreSQL | |
| env: | |
| DATABASE_URL: postgres://casbin_rs:casbin_rs@localhost:5432/casbin | |
| run: | | |
| cargo test --no-default-features --features postgres,runtime-async-std-native-tls | |
| cargo test --no-default-features --features postgres,runtime-async-std-rustls | |
| cargo test --no-default-features --features postgres,runtime-tokio-native-tls | |
| cargo test --no-default-features --features postgres,runtime-tokio-rustls | |
| - name: Cargo Test For MySQL | |
| env: | |
| DATABASE_URL: mysql://casbin_rs:casbin_rs@localhost:3306/casbin | |
| run: | | |
| cargo test --no-default-features --features mysql,runtime-async-std-native-tls | |
| cargo test --no-default-features --features mysql,runtime-async-std-rustls | |
| cargo test --no-default-features --features mysql,runtime-tokio-native-tls | |
| cargo test --no-default-features --features mysql,runtime-tokio-rustls | |
| - name: Cargo Test For SQLite | |
| env: | |
| DATABASE_URL: sqlite:casbin.db | |
| run: | | |
| cargo test --no-default-features --features sqlite,runtime-async-std-native-tls | |
| cargo test --no-default-features --features sqlite,runtime-async-std-rustls | |
| cargo test --no-default-features --features sqlite,runtime-tokio-native-tls | |
| cargo test --no-default-features --features sqlite,runtime-tokio-rustls |