Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Automatic deployment on merge to main and manuall trigger: https://starknet-explorer.lambdaclass.com/ #27

Merged
merged 15 commits into from
Jul 11, 2023
44 changes: 44 additions & 0 deletions .github/workflows/deploy-testing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Deploy to testing
on:
push:
branches:
- main
workflow_dispatch:

jobs:

build-deploy:
name: Build and deploy to testing
runs-on: ubuntu-latest
environment:
name: testing
url: https://starknet-explorer.lambdaclass.com/
steps:

- name: Checkout
uses: actions/[email protected]

- name: Create ssh private key file from env var
env:
SSH_KEY: ${{ secrets.SSH_KEY }}
run: |
set -ex
sed -E 's/(-+(BEGIN|END) OPENSSH PRIVATE KEY-+) *| +/\1\n/g' <<< "$SSH_KEY" > id_ed25519_testing
chmod 400 id_ed25519_testing

- name: Install ansible
run: |
pip install ansible

- name: "Deploy with ansible"
env:
MIX_ENV: ${{ vars.MIX_ENV }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
SECRET_KEY_BASE: ${{ secrets.SECRET_KEY_BASE }}
PHX_HOST: ${{ vars.PHX_HOST }}
PHX_SERVER: ${{ vars.PHX_SERVER }}
RPC_API_HOST: ${{ secrets.RPC_API_HOST }}
ANSIBLE_SSH_PKEY_DIR: "./id_ed25519_testing"
GIT_BRANCH: ${{ github.head_ref || github.ref_name }}
run: |
ansible-playbook -i ansible/inventory.yaml ansible/playbooks/deployment.yaml
8 changes: 8 additions & 0 deletions ansible/inventory.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
webservers:
hosts:
starknet_testing:
ansible_host: "157.90.154.208"
ansible_user: starknet_explorer
ansible_python_interpreter: /usr/bin/python3
ansible_ssh_private_key_file: "{{ lookup('ansible.builtin.env', 'ANSIBLE_SSH_PKEY_DIR') }}"
ansible_ssh_extra_args: '-o StrictHostKeyChecking=no'
85 changes: 85 additions & 0 deletions ansible/playbooks/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
- name: Deploy new version
hosts: webservers
serial: 1
gather_facts: false
any_errors_fatal: true
become: true
become_user: starknet_explorer

pre_tasks:

- name: Delete tmp repo
file:
state: absent
path: /home/starknet_explorer/tmp/starknet_explorer

- name: Clone repo in tmp dir
ansible.builtin.git:
repo: [email protected]:lambdaclass/starknet_explorer.git
version: "{{ lookup('ansible.builtin.env', 'GIT_BRANCH') }}"
dest: /home/starknet_explorer/tmp/starknet_explorer
ssh_opts: "-o StrictHostKeyChecking=no"
key_file: /home/starknet_explorer/.ssh/id_ed25519
register: clone_output

- name: Print clone repo output
debug: msg="{{ clone_output }}"

- name: Build
ansible.builtin.shell: |
set -ex
mix local.hex --force
mix local.rebar --force
mix deps.get --only $MIX_ENV
mix deps.compile
mix assets.deploy
mix phx.digest
mix compile
mix release
mix phx.gen.release
environment:
MIX_ENV: prod
args:
chdir: /home/starknet_explorer/tmp/starknet_explorer
register: build_output

- debug: msg="{{ build_output.stdout_lines + [ build_output.stderr_lines ] }}"

- name: Delete old repo
ansible.builtin.file:
state: absent
path: /home/starknet_explorer/starknet_explorer

- name: Copy repo from tmp dir
ansible.builtin.copy:
remote_src: true
src: /home/starknet_explorer/tmp/starknet_explorer
dest: /home/starknet_explorer/

- name: Create .env file
ansible.builtin.template:
src: .env.j2
dest: /home/starknet_explorer/.env
vars:
phx_host: "{{ lookup('ansible.builtin.env', 'PHX_HOST') }}"
secret_key_base: "{{ lookup('ansible.builtin.env', 'SECRET_KEY_BASE') }}"
database_url: "{{ lookup('ansible.builtin.env', 'DATABASE_URL') }}"
rpc_api_host: "{{ lookup('ansible.builtin.env', 'RPC_API_HOST') }}"

- name: Create user systemd directory
ansible.builtin.file:
state: directory
path: /home/starknet_explorer/.config/systemd/user

- name: Install systemd service
ansible.builtin.template:
src: starknet_explorer.service.j2
dest: /home/starknet_explorer/.config/systemd/user/starknet_explorer.service

- name: Restart starknet_explorer service
ansible.builtin.systemd:
scope: user
state: restarted
daemon_reload: true
enabled: true
name: starknet_explorer
5 changes: 5 additions & 0 deletions ansible/playbooks/templates/.env.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PHX_HOST={{ phx_host }}
PHX_SERVER=true
SECRET_KEY_BASE={{ secret_key_base }}
DATABASE_URL={{ database_url }}
RPC_API_HOST={{ rpc_api_host }}
15 changes: 15 additions & 0 deletions ansible/playbooks/templates/starknet_explorer.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[Unit]
Description=Starknet Explorer

[Service]
WorkingDirectory=/home/starknet_explorer/starknet_explorer
Restart=on-failure
ExecStart=/home/starknet_explorer/starknet_explorer/entrypoint.sh
ExecReload=/bin/kill -HUP
KillSignal=SIGTERM
EnvironmentFile=/home/starknet_explorer/.env
StandardOutput=append:/home/starknet_explorer/logs/starknet_explorer.log
StandardError=append:/home/starknet_explorer/logs/starknet_explorer.log

[Install]
WantedBy=multi-user.target
3 changes: 3 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

/home/starknet_explorer/starknet_explorer/_build/prod/rel/starknet_explorer/bin/starknet_explorer start
Loading