Skip to content

Commit b5b9589

Browse files
authored
Optimize version handling and add AWS image push action (HemeraProtocol#180)
* Optimize version handling and add AWS image push action - Improve version-related code - Add new action for automatic image push to AWS - Add command make development
1 parent ea34aeb commit b5b9589

File tree

7 files changed

+816
-686
lines changed

7 files changed

+816
-686
lines changed

.github/workflows/push-image.yaml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: Push image to AWS ECR
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
tags:
8+
- 'v*'
9+
workflow_dispatch:
10+
inputs:
11+
runPushAWS:
12+
description: 'Run push image to aws (yes/no)'
13+
required: true
14+
default: 'false'
15+
arch:
16+
required: false
17+
default: 'amd64'
18+
19+
jobs:
20+
build-and-push:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v2
24+
with:
25+
fetch-depth: 0
26+
- name: Set arch variable
27+
run: |
28+
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
29+
echo "ARCH=${{ github.event.inputs.arch }}" >> $GITHUB_ENV
30+
else
31+
echo "ARCH=amd64" >> $GITHUB_ENV
32+
fi
33+
34+
- name: Set up AWS credentials
35+
uses: aws-actions/configure-aws-credentials@v1
36+
with:
37+
aws-access-key-id: ${{ secrets.PROD_AWS_ACCESS_KEY_ID }}
38+
aws-secret-access-key: ${{ secrets.PROD_AWS_SECRET_ACCESS_KEY }}
39+
aws-region: ${{ secrets.AWS_REGION }}
40+
41+
- name: Configure AWS CLI profile
42+
run: |
43+
aws configure set aws_access_key_id ${{ secrets.PROD_AWS_ACCESS_KEY_ID }} --profile prod
44+
aws configure set aws_secret_access_key ${{ secrets.PROD_AWS_SECRET_ACCESS_KEY }} --profile prod
45+
46+
- name: Build and Push to AWS ECR
47+
if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/') || github.event.inputs.runPushAWS == 'yes'
48+
env:
49+
ECR_REGISTRY: ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com
50+
ECR_REPO: hemera-protocol
51+
run: |
52+
echo "Architecture: ${{ env.ARCH }}"
53+
echo "Building and pushing to AWS ECR"
54+
55+
if [[ $GITHUB_REF == refs/tags/* ]]; then
56+
# It's a tag push, use the tag as is
57+
TAG=${GITHUB_REF#refs/tags/}
58+
# Remove 'v' prefix if present
59+
TAG=${TAG#v}
60+
else
61+
# Use the original naming convention
62+
VERSION=$(grep '^version = ' pyproject.toml | sed 's/^version = //;s/"//g')
63+
if [[ $GITHUB_EVENT_NAME == "pull_request" ]]; then
64+
# It's a pull request
65+
BUILD=$(echo ${{ github.event.pull_request.head.sha }} | cut -c 1-7)
66+
else
67+
# It's a push to a branch (e.g., master)
68+
BUILD=$(git rev-parse --short=7 HEAD)
69+
fi
70+
TAG=$VERSION-$BUILD-${{ env.ARCH }}
71+
fi
72+
73+
echo "Tag: $TAG"
74+
75+
# Build the Docker image using make
76+
make image TAG=$TAG ARCH=${{ env.ARCH }}
77+
78+
# Login to ECR
79+
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} --profile prod | docker login --username AWS --password-stdin $ECR_REGISTRY
80+
81+
# Tag the image for ECR
82+
docker tag $ECR_REPO:$TAG $ECR_REGISTRY/$ECR_REPO:$TAG
83+
84+
# Push the image to ECR
85+
docker push $ECR_REGISTRY/$ECR_REPO:$TAG

Makefile

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
VERSION := $(shell poetry version -s)
1+
ARCH=amd64
2+
VERSION := $(shell grep '^version = ' pyproject.toml | sed 's/^version = //;s/"//g')
23
BUILD := `git rev-parse --short=7 HEAD`
3-
SERVICES =
4-
.PHONY: all build image test
4+
5+
TAG := $(VERSION)-$(BUILD)-$(ARCH)
6+
7+
8+
PRE_COMMIT_INSTALLED := $(shell command -v pre-commit > /dev/null 2>&1 && echo yes || echo no)
9+
VENV_DIR := .venv
10+
POETRY_INSTALLED := $(shell command -v poetry > /dev/null 2>&1 && echo yes || echo no)
11+
12+
IMAGE_FLAGS := $(IMAGE_FLAGS) --platform linux/$(ARCH)
513

614
RED=\033[31m
715
GREEN=\033[32m
816
YELLOW=\033[33m
917
RESET=\033[0m
1018

11-
image:
1219

13-
docker build $(IMAGE_FLAGS) --network host -t hemera-protocol:$(VERSION)-$(BUILD) . --no-cache
14-
echo "Built image hemera-protocol:$(VERSION)-$(BUILD)"
20+
.PHONY: format init_db development image test
21+
22+
image:
23+
@echo "Build tag: $(TAG)"
24+
@echo "Build flags: $(IMAGE_FLAGS)"
25+
docker buildx build $(IMAGE_FLAGS) --network host -t hemera-protocol:$(TAG) . --no-cache
26+
@echo "Built image hemera-protocol:$(TAG)"
1527

1628
test:
1729
@if [ "$(filter-out $@,$(MAKECMDGOALS))" = "" ]; then \
@@ -21,8 +33,6 @@ test:
2133
fi
2234

2335

24-
PRE_COMMIT_INSTALLED := $(shell command -v pre-commit > /dev/null 2>&1 && echo yes || echo no)
25-
2636
format:
2737
ifeq ($(PRE_COMMIT_INSTALLED),yes)
2838
@echo "$(YELLOW)Formatting code...$(RESET)"
@@ -33,4 +43,46 @@ endif
3343

3444
init_db:
3545
@echo "Initializing database..."
36-
poetry run python -m hemera.py init_db
46+
poetry run python -m hemera.py init_db
47+
48+
development:
49+
@echo "Setting up development environment..."
50+
@bash -c 'set -euo pipefail; \
51+
PYTHON_CMD=$$(command -v python3 || command -v python); \
52+
if [ -z "$$PYTHON_CMD" ] || ! "$$PYTHON_CMD" --version 2>&1 | grep -q "Python 3"; then \
53+
echo "Python 3 is not found. Please install Python 3 and try again."; \
54+
exit 1; \
55+
fi; \
56+
python_version=$$($$PYTHON_CMD -c "import sys; print(\"{}.{}\".format(sys.version_info.major, sys.version_info.minor))"); \
57+
if ! echo "$$python_version" | grep -qE "^3\.(8|9|10|11)"; then \
58+
echo "Python version $$python_version is not supported. Please use Python 3.8, 3.9, 3.10, or 3.11."; \
59+
exit 1; \
60+
fi; \
61+
echo "Using Python: $$($$PYTHON_CMD --version)"; \
62+
if [ ! -d ".venv" ]; then \
63+
echo "Creating virtual environment..."; \
64+
$$PYTHON_CMD -m venv .venv || { \
65+
echo "Failed to create virtual environment. Installing venv..."; \
66+
sudo apt-get update && sudo apt-get install -y python3-venv && $$PYTHON_CMD -m venv .venv; \
67+
}; \
68+
fi; \
69+
echo "Activating virtual environment..."; \
70+
. .venv/bin/activate; \
71+
if ! pip --version &> /dev/null; then \
72+
echo "Installing pip..."; \
73+
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py; \
74+
$$PYTHON_CMD get-pip.py; \
75+
rm get-pip.py; \
76+
fi; \
77+
if ! poetry --version &> /dev/null; then \
78+
echo "Installing Poetry..."; \
79+
pip install poetry; \
80+
else \
81+
echo "Poetry is already installed."; \
82+
fi; \
83+
echo "Installing project dependencies..."; \
84+
poetry install -v; \
85+
echo "Development environment setup complete."; \
86+
echo ""; \
87+
echo "To activate the virtual environment, run:"; \
88+
echo "source .venv/bin/activate"'

README.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ As of July 5, 2024, the initial open-source version of the Hemera Indexer offers
2424
- Logs
2525
- ERC20 / ERC721 / ERC1155 tokens
2626
- ERC20 / ERC721 / ERC1155 Token transfers
27-
- ERC20 / ERC721 / ERC1155 Token balance & holders
27+
- ERC20 / ERC721 / ERC1155 Token balance
2828
- Contracts
2929
- Traces / Internal transactions
3030
- L1 -> L2 Transactions
@@ -114,6 +114,7 @@ If you have trouble running the following commands, consider referring to
114114
the [official docker installation guide](https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository)
115115
for the latest instructions.
116116

117+
##### Ubuntu and Debian
117118
```bash
118119
# Add Docker's official GPG key:
119120
sudo apt-get update
@@ -128,11 +129,29 @@ echo \
128129
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
129130
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
130131
sudo apt-get update
131-
```
132132

133-
```bash
134133
# Install docker and docker compose
135134
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
135+
docker compose version
136+
```
137+
138+
##### RPM-based distros
139+
```bash
140+
sudo yum update -y
141+
sudo yum install docker -y
142+
sudo service docker start
143+
sudo systemctl enable docker
144+
sudo usermod -a -G docker ec2-user
145+
146+
newgrp docker
147+
docker --version
148+
docker run hello-world
149+
150+
DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
151+
mkdir -p $DOCKER_CONFIG/cli-plugins
152+
curl -SL https://github.com/docker/compose/releases/download/v2.29.6/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
153+
chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
154+
docker compose version
136155
```
137156

138157
#### Run the Docker Compose
@@ -169,33 +188,38 @@ Attaching to hemera-api, indexer, indexer-trace, postgresql, redis
169188

170189
### Run From Source Code
171190

172-
#### Install Python3 and Pip
191+
#### Install developer tools
173192

174193
Skip this step if you already have both installed.
175194

176195
```bash
177196
sudo apt update
178-
sudo apt install python3
179-
sudo apt install python3-pip
197+
sudo apt install make
180198
```
181199

182-
#### Initiate Python VENV
200+
#### Run development
183201

184-
Skip this step if you don't want to have a dedicated python venv for Hemera Indexer.
202+
To deploy your project, simply run:
185203

186204
```bash
187-
sudo apt install python3-venv
188-
python3 -m venv ./venv
205+
make development
189206
```
190207

191-
#### Install Pip Dependencies
208+
This command will:
209+
1. Create a Python virtual environment
210+
2. Activate the virtual environment
211+
3. Install necessary system packages
212+
4. Install Python dependencies
213+
214+
After running this command, your environment will be set up and ready to use.
215+
216+
Remember to activate the virtual environment (`source ./venv/bin/activate`) when you want to work on your project in the future.
192217

193218
```bash
194219
source ./venv/bin/activate
195-
sudo apt install libpq-dev
196-
pip install -e .
197220
```
198221

222+
199223
#### Prepare Your PostgreSQL Instance
200224

201225
Hemera Indexer requires a PostgreSQL database to store all indexed data. You may skip this step if you already have a PostgreSQL set up.

__init__.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)