Skip to content

ci: add cross-rs build #14

ci: add cross-rs build

ci: add cross-rs build #14

Workflow file for this run

name: Main
on:
push:
branches: ["main"]
tags:
- "*-?v[0-9]+*"
pull_request:
branches: ["main"]
types: [opened, synchronize]
env:
GHCR_REGISTRY: ghcr.io
permissions:
contents: write
packages: write
jobs:
check-commit-message:
runs-on: ubuntu-latest
outputs:
should-run: ${{ steps.check.outputs.should-run }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Check commit message
id: check
run: |
if [[ "${{ github.event_name }}" == "push" ]]; then
echo "should-run=true" >> $GITHUB_OUTPUT
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
COMMIT_MSG=$(git log --format=%B ${{ github.event.pull_request.head.sha }})
if [[ "$COMMIT_MSG" == *"Run CI"* ]]; then
echo "should-run=true" >> $GITHUB_OUTPUT
else
echo "should-run=false" >> $GITHUB_OUTPUT
fi
else
echo "should-run=false" >> $GITHUB_OUTPUT
fi
build-frontend:
needs: check-commit-message
if: needs.check-commit-message.outputs.should-run == 'true'
runs-on: ubuntu-latest
env:
MOON_TOOLCHAIN_FORCE_GLOBALS: true
steps:
- uses: actions/checkout@v4
- uses: moonrepo/setup-toolchain@v0
with:
auto-install: true
- name: Build frontend
run: |
moon run frontend:build transactional:build
- name: Upload templates artifact
uses: actions/upload-artifact@v4
with:
name: templates
path: |
apps/backend/templates/
retention-days: 1
build-backend:
needs:
- check-commit-message
- build-frontend
if: needs.check-commit-message.outputs.should-run == 'true'
strategy:
fail-fast: false
matrix:
platform:
- target: x86_64-unknown-linux-gnu
command: cargo
- target: aarch64-unknown-linux-gnu
command: cross
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: templates
path: ${{ github.workspace }}/apps/backend/templates/
- name: Extract build information
id: build
env:
TARGET: ${{ matrix.platform.target }}
run: |
echo "version=${GITHUB_REF##*/}" >> "$GITHUB_OUTPUT"
echo "docker-arch=${{ startsWith(matrix.platform.target, 'x86_64') && 'amd64' || 'arm64' }}" >> "$GITHUB_OUTPUT"
echo "profile=${{ github.event.ref_type == 'tag' && 'release' || 'dev' }}" >> "$GITHUB_OUTPUT"
echo "output-dir=${{ github.event.ref_type == 'tag' && 'release' || 'debug' }}" >> "$GITHUB_OUTPUT"
- name: Extract rust toolchain
id: toolchain
run: |
echo "channel=$(grep channel rust-toolchain.toml | awk -F' = ' '{printf $2}' | tr -d '\"')" >> "$GITHUB_OUTPUT"
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ steps.toolchain.outputs.channel }}
targets: ${{ matrix.platform.target }}
- uses: Swatinem/rust-cache@v2
with:
key: ${{ matrix.platform.target }}-${{ steps.build.outputs.profile }}
save-if: ${{ github.event_name != 'pull_request' }}
- name: Install cross
if: ${{ matrix.platform.command == 'cross' }}
uses: taiki-e/cache-cargo-install-action@v2
with:
tool: cross
git: https://github.com/cross-rs/cross
rev: 19be83481fd3e50ea103d800d72e0f8eddb1c90c
locked: false
- name: Build
env:
APP_VERSION: ${{ steps.build.outputs.version }}
DEFAULT_TMDB_ACCESS_TOKEN: ${{ secrets.DEFAULT_TMDB_ACCESS_TOKEN }}
DEFAULT_MAL_CLIENT_ID: ${{ secrets.DEFAULT_MAL_CLIENT_ID }}
DEFAULT_GOOGLE_BOOKS_API_KEY: ${{ secrets.DEFAULT_GOOGLE_BOOKS_API_KEY }}
run: |
touch .env
${{ matrix.platform.command }} build --locked --target ${{ matrix.platform.target }} --profile ${{ steps.build.outputs.profile }}
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: backend-${{ steps.build.outputs.docker-arch }}
path: ${{ github.workspace }}/target/${{ matrix.platform.target }}/${{ steps.build.outputs.output-dir }}/ryot
retention-days: 1
build-docker:
needs:
- check-commit-message
- build-backend
if: needs.check-commit-message.outputs.should-run == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Download build artifact for docker
uses: actions/download-artifact@v4
with:
path: ${{ github.workspace }}/artifact/
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to the ghcr container registry
uses: docker/login-action@v3
with:
registry: ${{ env.GHCR_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to the docker hub container registry
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: |
name=${{ env.GHCR_REGISTRY }}/${{ github.actor }}/${{ github.event.repository.name }}
name=${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }},enable={{is_default_branch}}
tags: |
type=ref,event=pr
type=semver,pattern=v{{version}},enable={{is_default_branch}}
type=semver,pattern=v{{major}}.{{minor}},enable={{is_default_branch}}
type=semver,pattern=v{{major}},enable={{is_default_branch}}
type=raw,value=develop,enable={{is_default_branch}}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}