|
| 1 | +name: Deploy Raindex API |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | + inputs: |
| 5 | + scope: |
| 6 | + description: Deployment scope |
| 7 | + required: true |
| 8 | + default: service |
| 9 | + type: choice |
| 10 | + options: |
| 11 | + - service |
| 12 | + - all |
| 13 | + - system |
| 14 | +concurrency: |
| 15 | + group: deploy-raindex-api-production |
| 16 | + cancel-in-progress: false |
| 17 | +permissions: |
| 18 | + contents: read |
| 19 | +jobs: |
| 20 | + deploy: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + environment: Production |
| 23 | + env: |
| 24 | + RAINDEX_API_HOSTNAME: ${{ vars.RAINDEX_API_HOSTNAME }} |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v6 |
| 27 | + with: |
| 28 | + submodules: recursive |
| 29 | + - uses: nixbuild/nix-quick-install-action@v30 |
| 30 | + with: |
| 31 | + nix_conf: | |
| 32 | + accept-flake-config = true |
| 33 | + access-tokens = github.com=${{ secrets.GITHUB_TOKEN }} |
| 34 | + - uses: cachix/cachix-action@v15 |
| 35 | + continue-on-error: true |
| 36 | + with: |
| 37 | + name: rainlanguage |
| 38 | + authToken: ${{ secrets.CACHIX_AUTH_TOKEN }} |
| 39 | + useDaemon: false |
| 40 | + - uses: nix-community/cache-nix-action@v7 |
| 41 | + with: |
| 42 | + primary-key: nix-${{ runner.os }}-${{ hashFiles('**/*.nix', '**/flake.lock') }} |
| 43 | + restore-prefixes-first-match: nix-${{ runner.os }}- |
| 44 | + gc-max-store-size-linux: 8G |
| 45 | + - name: Validate deployment definitions |
| 46 | + run: | |
| 47 | + nix flake check --no-build --impure |
| 48 | + nix build .#raindex-api --no-link |
| 49 | + nix shell .#opentofu -c tofu -chdir=infra init -backend=false |
| 50 | + nix shell .#opentofu -c tofu -chdir=infra validate |
| 51 | + - name: Configure authenticated SSH |
| 52 | + env: |
| 53 | + SSH_KEY: ${{ secrets.RAINDEX_API_SSH_KEY }} |
| 54 | + run: | |
| 55 | + test -n "$SSH_KEY" |
| 56 | + install -d -m 700 ~/.ssh |
| 57 | + printf '%s\n' "$SSH_KEY" > ~/.ssh/id_ed25519 |
| 58 | + chmod 600 ~/.ssh/id_ed25519 |
| 59 | +
|
| 60 | + host_ip=$(nix run .#resolveIp -- -i ~/.ssh/id_ed25519) |
| 61 | + host_key=$(nix eval --raw --file keys.nix keys.host) |
| 62 | + test "$host_key" != "ssh-ed25519 REPLACE_AFTER_BOOTSTRAP" |
| 63 | + printf '%s %s\n' "$host_ip" "$host_key" > ~/.ssh/known_hosts |
| 64 | + - name: Deploy |
| 65 | + env: |
| 66 | + SCOPE: ${{ inputs.scope }} |
| 67 | + run: | |
| 68 | + case "$SCOPE" in |
| 69 | + service) nix run .#deployService -- -i ~/.ssh/id_ed25519 rest-api ;; |
| 70 | + system) nix run .#deployNixos -- -i ~/.ssh/id_ed25519 ;; |
| 71 | + all) nix run .#deployAll -- -i ~/.ssh/id_ed25519 ;; |
| 72 | + *) echo "Unsupported deployment scope: $SCOPE" >&2; exit 1 ;; |
| 73 | + esac |
| 74 | + - name: Verify deployment |
| 75 | + run: | |
| 76 | + api_hostname="${RAINDEX_API_HOSTNAME:-api.raindex.finance}" |
| 77 | + health_url="https://$api_hostname/health/detailed" |
| 78 | + for _ in $(seq 1 60); do |
| 79 | + if curl --fail --silent --show-error --max-time 5 "$health_url"; then |
| 80 | + exit 0 |
| 81 | + fi |
| 82 | + sleep 5 |
| 83 | + done |
| 84 | +
|
| 85 | + host_ip=$(nix run .#resolveIp -- -i ~/.ssh/id_ed25519) |
| 86 | + ssh -i ~/.ssh/id_ed25519 "root@$host_ip" \ |
| 87 | + 'systemctl status rest-api.service --no-pager || true; journalctl -u rest-api.service -n 200 --no-pager || true' |
| 88 | + exit 1 |
0 commit comments