2023-11-18 20:02:23 +01:00
|
|
|
name: CI/CD Pipeline
|
|
|
|
|
|
|
|
on:
|
|
|
|
push:
|
|
|
|
branches:
|
|
|
|
- main
|
|
|
|
- staging
|
|
|
|
|
|
|
|
env:
|
|
|
|
CARGO_TARGET: x86_64-unknown-linux-musl
|
|
|
|
SSH_HOST: ${{ secrets.SSH_HOST }}
|
|
|
|
SSH_USER: ${{ secrets.SSH_USER }}
|
|
|
|
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
|
|
|
|
|
|
jobs:
|
2023-11-18 21:46:00 +01:00
|
|
|
setup-environment:
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
outputs:
|
|
|
|
cache-key: ${{ steps.cache-keys.outputs.cache-key }}
|
|
|
|
steps:
|
|
|
|
- name: Calculate Cache Keys
|
|
|
|
id: cache-keys
|
|
|
|
run: echo "::set-output name=cache-key::$(date +%s)"
|
|
|
|
|
2023-11-18 20:02:23 +01:00
|
|
|
build:
|
|
|
|
runs-on: ubuntu-latest
|
2023-11-18 21:46:00 +01:00
|
|
|
needs: setup-environment
|
|
|
|
container: my-custom-container:latest
|
2023-11-18 20:02:23 +01:00
|
|
|
|
|
|
|
steps:
|
|
|
|
- name: Checkout
|
|
|
|
uses: actions/checkout@v3
|
|
|
|
|
2023-11-18 21:46:00 +01:00
|
|
|
- name: Restore Cache
|
|
|
|
uses: actions/cache@v3
|
|
|
|
with:
|
|
|
|
path: |
|
|
|
|
~/.cargo
|
|
|
|
./frontend/node_modules
|
|
|
|
key: ${{ needs.setup-environment.outputs.cache-key }}
|
|
|
|
|
2023-11-18 20:02:23 +01:00
|
|
|
- name: Run Test DB Script
|
|
|
|
run: ./test_db.sh
|
|
|
|
|
|
|
|
- name: Build
|
|
|
|
run: |
|
|
|
|
cargo build --release --target $CARGO_TARGET
|
|
|
|
strip target/$CARGO_TARGET/release/rot
|
|
|
|
cd frontend && npm install && npm run build
|
|
|
|
|
|
|
|
- name: Upload Artifacts
|
|
|
|
uses: actions/upload-artifact@v3
|
|
|
|
with:
|
|
|
|
name: build-artifacts
|
|
|
|
path: |
|
|
|
|
target/$CARGO_TARGET/release/rot
|
|
|
|
static
|
|
|
|
retention-days: 1
|
|
|
|
|
|
|
|
test:
|
|
|
|
runs-on: ubuntu-latest
|
2023-11-18 21:46:00 +01:00
|
|
|
needs: build
|
|
|
|
container: my-custom-container:latest
|
2023-11-18 20:02:23 +01:00
|
|
|
|
|
|
|
steps:
|
|
|
|
- name: Checkout
|
|
|
|
uses: actions/checkout@v3
|
|
|
|
|
2023-11-18 21:46:00 +01:00
|
|
|
- name: Restore Cache
|
|
|
|
uses: actions/cache@v3
|
2023-11-18 21:18:07 +01:00
|
|
|
with:
|
2023-11-18 21:46:00 +01:00
|
|
|
path: ~/.cargo
|
|
|
|
key: ${{ needs.setup-environment.outputs.cache-key }}
|
2023-11-18 21:18:07 +01:00
|
|
|
|
2023-11-18 20:02:23 +01:00
|
|
|
- name: Run Tests
|
|
|
|
run: cargo test --verbose
|
|
|
|
|
|
|
|
deploy-staging:
|
|
|
|
runs-on: ubuntu-latest
|
2023-11-18 21:46:00 +01:00
|
|
|
needs: test
|
2023-11-18 20:02:23 +01:00
|
|
|
if: github.ref == 'refs/heads/staging'
|
2023-11-18 20:23:07 +01:00
|
|
|
|
2023-11-18 21:46:00 +01:00
|
|
|
steps:
|
2023-11-18 20:02:23 +01:00
|
|
|
- name: Checkout
|
|
|
|
uses: actions/checkout@v3
|
2023-11-18 21:46:00 +01:00
|
|
|
|
2023-11-18 21:18:07 +01:00
|
|
|
- name: Download Artifacts
|
|
|
|
uses: actions/download-artifact@v3
|
|
|
|
with:
|
|
|
|
name: build-artifacts
|
2023-11-18 20:02:23 +01:00
|
|
|
|
|
|
|
- name: Deploy to Staging
|
|
|
|
run: |
|
|
|
|
scp target/$CARGO_TARGET/release/rot $SSH_USER@$SSH_HOST:/home/k004373/rowing-staging/rot-updating
|
|
|
|
scp staging-diff.sql $SSH_USER@$SSH_HOST:/home/k004373/rowing-staging/
|
|
|
|
scp -r static $SSH_USER@$SSH_HOST:/home/k004373/rowing-staging/
|
|
|
|
scp -r templates $SSH_USER@$SSH_HOST:/home/k004373/rowing-staging/
|
|
|
|
scp -r svelte $SSH_USER@$SSH_HOST:/home/k004373/rowing-staging/
|
|
|
|
ssh $SSH_USER@$SSH_HOST 'sudo systemctl stop rotstaging'
|
|
|
|
ssh $SSH_USER@$SSH_HOST 'rm /home/k004373/rowing-staging/db.sqlite && cp /home/k004373/rowing/db.sqlite /home/k004373/rowing-staging/db.sqlite && mkdir -p /home/k004373/rowing-staging/svelte/build && mkdir -p /home/k004373/rowing-staging/data-ergo/thirty && mkdir -p /home/k004373/rowing-staging/data-ergo/dozen && sqlite3 /home/k004373/rowing-staging/db.sqlite < /home/k004373/rowing-staging/staging-diff.sql'
|
|
|
|
ssh $SSH_USER@$SSH_HOST 'mv /home/k004373/rowing-staging/rot-updating /home/k004373/rowing-staging/rot'
|
2023-11-18 21:46:00 +01:00
|
|
|
ssh $SSH_USER@$SSH_HOST 'sudo systemctl start rotstaging'
|
2023-11-18 20:02:23 +01:00
|
|
|
env:
|
|
|
|
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
|
|
SSH_HOST: ${{ secrets.SSH_HOST }}
|
|
|
|
SSH_USER: ${{ secrets.SSH_USER }}
|
|
|
|
|
|
|
|
deploy-main:
|
|
|
|
runs-on: ubuntu-latest
|
2023-11-18 21:46:00 +01:00
|
|
|
needs: test
|
2023-11-18 20:02:23 +01:00
|
|
|
if: github.ref == 'refs/heads/main'
|
2023-11-18 20:23:07 +01:00
|
|
|
|
2023-11-18 21:46:00 +01:00
|
|
|
steps:
|
2023-11-18 20:02:23 +01:00
|
|
|
- name: Checkout
|
|
|
|
uses: actions/checkout@v3
|
2023-11-18 21:18:07 +01:00
|
|
|
|
|
|
|
- name: Download Artifacts
|
|
|
|
uses: actions/download-artifact@v3
|
|
|
|
with:
|
|
|
|
name: build-artifacts
|
2023-11-18 20:02:23 +01:00
|
|
|
|
|
|
|
- name: Deploy to Main
|
|
|
|
run: |
|
|
|
|
scp target/$CARGO_TARGET/release/rot $SSH_USER@$SSH_HOST:/home/k004373/rowing/rot-updating
|
|
|
|
scp -r static $SSH_USER@$SSH_HOST:/home/k004373/rowing/
|
|
|
|
scp -r templates $SSH_USER@$SSH_HOST:/home/k004373/rowing/
|
|
|
|
scp -r svelte $SSH_USER@$SSH_HOST:/home/k004373/rowing/
|
|
|
|
ssh $SSH_USER@$SSH_HOST 'mkdir -p /home/k004373/rowing/svelte/build && mkdir -p /home/k004373/rowing/data-ergo/thirty && mkdir -p /home/k004373/rowing/data-ergo/dozen'
|
|
|
|
ssh $SSH_USER@$SSH_HOST 'sudo systemctl stop rot'
|
|
|
|
ssh $SSH_USER@$SSH_HOST 'mv /home/k004373/rowing/rot-updating /home/k004373/rowing/rot'
|
2023-11-18 21:46:00 +01:00
|
|
|
ssh $SSH_USER@$SSH_HOST 'sudo systemctl start rot'
|
2023-11-18 20:02:23 +01:00
|
|
|
env:
|
|
|
|
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
|
|
SSH_HOST: ${{ secrets.SSH_HOST }}
|
|
|
|
SSH_USER: ${{ secrets.SSH_USER }}
|