Skip to content

Commit

Permalink
Instance setup + CI/CD
Browse files Browse the repository at this point in the history
  • Loading branch information
beeinger committed May 3, 2024
1 parent 46249b9 commit 27b6f20
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 4 deletions.
87 changes: 87 additions & 0 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Rust CI

on:
pull_request:
branches:
- develop
- main

push:
branches:
- main

env:
CARGO_TERM_COLOR: always

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.lock') }}

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

- name: Toolchain info
run: |
cargo --version --verbose
rustc --version
cargo clippy --version
- name: Install prebuild binaries of cargo-make
uses: taiki-e/install-action@v2
with:
tool: cargo-make

- name: Run Format
run: cargo make format

- name: Run Build
run: cargo make build

deploy:
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- uses: actions/checkout@v2
- name: Get Commit SHA
run: |
echo "Commit SHA: ${{ github.sha }}"
- name: rsync deployments
uses: burnett01/[email protected]
with:
switches: -avzr --delete --exclude=.github --exclude=data --exclude=target --exclude=.env --exclude=.env.dist --exclude=.env.test --exclude=mocks --exclude=.git
path: .
remote_path: /root/l2-indexer
remote_host: ${{ secrets.SSH_HOST }}
remote_user: root
remote_key: ${{ secrets.SSH_DEPLOY_KEY }}

# - name: Run docker-compose on server
- name: Setup envs and run docker-compose on server
run: |
echo "${{ secrets.SSH_DEPLOY_KEY }}" > ./l2-indexer.pem &&\
chmod 600 ./l2-indexer.pem &&\
ssh -o StrictHostKeyChecking=no -i ./l2-indexer.pem root@${{ secrets.SSH_HOST }} "\
echo -e '## Git sha: ${{ github.sha }}\nDate UNIX: $(date +%s)\n' >> version.md &&\
echo '${{ secrets.ENV_CONFIG }}' > /root/l2-indexer/.env &&\
docker image prune -f &&\
docker builder prune -f &&\
cargo make setup"
shell: bash
57 changes: 57 additions & 0 deletions .ubuntu/setup-copy-paste.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
echo "DOCKER" &&\
sudo apt update &&\
sudo apt install docker.io -y &&\
sudo systemctl enable docker &&\
sudo systemctl start docker &&\
sudo usermod -aG docker ${USER} &&\

echo "DOCKER-COMPOSE" &&\
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose &&\
sudo chmod +x /usr/local/bin/docker-compose &&\

echo "RUST" &&\
sudo apt install build-essential -y &&\
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh &&\
source "$HOME/.cargo/env" &&\
cargo install --force cargo-make &&\

echo "PM2" &&\
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash &&\
source ~/.bashrc &&\
nvm install 22 &&\
npm install pm2 -g &&\
pm2 startup &&\

echo "NGINX" &&\
sudo apt update -y &&\
sudo apt install nginx git make -y &&\
sudo systemctl enable nginx &&\
sudo systemctl start nginx &&\

echo "CERTBOT" &&\
sudo apt install python3-venv python3-pip -y &&\
sudo python3 -m venv /opt/certbot/ &&\
sudo /opt/certbot/bin/pip install --upgrade pip &&\
sudo /opt/certbot/bin/pip install certbot certbot-nginx &&\
sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot &&\
sudo certbot --version &&\

echo "HTTPS (NGINX + CERTBOT)" &&\
echo "user www-data;
worker_processes auto;
events {
worker_connections 1024;
}
http {
server {
server_name l2-indexer.api.herodotus.cloud;
location / {
proxy_set_header X-Forwarded-For \$remote_addr;
proxy_set_header Host \$http_host;
proxy_pass \"http://0.0.0.0:8000\";
}
}
}" > tmp.conf && sudo cp tmp.conf /etc/nginx/nginx.conf && rm tmp.conf &&\
sudo certbot --nginx -d l2-indexer.api.herodotus.cloud --non-interactive --agree-tos --email [email protected]
4 changes: 0 additions & 4 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ docker-compose up --build -d
echo "Building the Rust application..."
cargo build --release

# Step 3: Check if the database exists and create it if it does not
echo "Waiting for PostgreSQL to start..."
sleep 10 # This might need adjustment depending on your system

# Step 4: Start the application using PM2
echo "Starting the application using PM2..."
pm2 start pm2.config.js
Expand Down

0 comments on commit 27b6f20

Please sign in to comment.