diff --git a/.github/workflows/deploy-testing.yaml b/.github/workflows/deploy-testing.yaml new file mode 100644 index 00000000..f6b09934 --- /dev/null +++ b/.github/workflows/deploy-testing.yaml @@ -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/checkout@v3.3.0 + + - 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 diff --git a/ansible/inventory.yaml b/ansible/inventory.yaml new file mode 100644 index 00000000..79a709ad --- /dev/null +++ b/ansible/inventory.yaml @@ -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' diff --git a/ansible/playbooks/deployment.yaml b/ansible/playbooks/deployment.yaml new file mode 100644 index 00000000..b38013e2 --- /dev/null +++ b/ansible/playbooks/deployment.yaml @@ -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: git@github.com: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 diff --git a/ansible/playbooks/templates/.env.j2 b/ansible/playbooks/templates/.env.j2 new file mode 100644 index 00000000..19b8d1e1 --- /dev/null +++ b/ansible/playbooks/templates/.env.j2 @@ -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 }} diff --git a/ansible/playbooks/templates/starknet_explorer.service.j2 b/ansible/playbooks/templates/starknet_explorer.service.j2 new file mode 100644 index 00000000..4ea87f44 --- /dev/null +++ b/ansible/playbooks/templates/starknet_explorer.service.j2 @@ -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 diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 00000000..f70bbe85 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +/home/starknet_explorer/starknet_explorer/_build/prod/rel/starknet_explorer/bin/starknet_explorer start