From 5b8bee873693ea876dd7deb61d768f8a664a947c Mon Sep 17 00:00:00 2001 From: Daniel Pupius Date: Fri, 2 May 2025 14:39:38 -0700 Subject: [PATCH] Script for building and pushing docker to ECR --- .github/workflows/cicd.yml | 5 ---- .gitignore | 2 +- Makefile | 2 +- scripts/push-to-ecr.sh | 52 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100755 scripts/push-to-ecr.sh diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index 173d65b..331c5a4 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -27,11 +27,6 @@ jobs: run: | sudo apt-get update sudo apt-get install -y protobuf-compiler - - - name: Install protoc-gen-go - run: | - go install google.golang.org/protobuf/cmd/protoc-gen-go@latest - echo "$(go env GOPATH)/bin" >> $GITHUB_PATH - name: Generate Protocol Buffers run: make gen-proto diff --git a/.gitignore b/.gitignore index fb47bb3..e882abb 100644 --- a/.gitignore +++ b/.gitignore @@ -34,7 +34,6 @@ Thumbs.db # Dependency directories vendor/ -bin/tools/ # Log files *.log @@ -57,6 +56,7 @@ bin/tools/ # Local development files *.local .air.toml +*.local.sh # Node.js node_modules/ diff --git a/Makefile b/Makefile index 191004c..14e0ee4 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ baymesh: build @./dist/meshstream --verbose --mqtt-topic-prefix "msh/US/bayarea" 2>&1 | go tool github.com/dpup/logista # Generate Go code from Protocol Buffers -gen-proto: +gen-proto: tools @mkdir -p $(ROOT_DIR)/generated PATH="$(TOOLS_DIR):$$PATH" protoc \ -Iproto/ \ diff --git a/scripts/push-to-ecr.sh b/scripts/push-to-ecr.sh new file mode 100755 index 0000000..cefbd62 --- /dev/null +++ b/scripts/push-to-ecr.sh @@ -0,0 +1,52 @@ +#!/bin/bash +set -e + +# Load environment variables from .env file +if [ -f .env ]; then + echo "Loading environment variables from .env file" + export $(grep -v '^#' .env | xargs) +else + echo "Warning: .env file not found. Using default environment variables." +fi + +# AWS ECR repository configuration +if [ -z "${ECR_REPOSITORY}" ]; then + echo "Error: ECR_REPOSITORY environment variable is not set. Please set it in .env file." + exit 1 +fi + +AWS_REGION=${AWS_REGION:-"us-east-1"} +IMAGE_TAG=${IMAGE_TAG:-"latest"} + +# Check if AWS CLI is installed +if ! command -v aws &> /dev/null; then + echo "Error: AWS CLI is not installed. Please install it first." + exit 1 +fi + +# Check if Docker is installed +if ! command -v docker &> /dev/null; then + echo "Error: Docker is not installed. Please install it first." + exit 1 +fi + +echo "Building Docker image for Meshstream..." +docker build \ + --build-arg MESHSTREAM_API_BASE_URL="${MESHSTREAM_API_BASE_URL:-}" \ + --build-arg MESHSTREAM_APP_ENV="${MESHSTREAM_APP_ENV:-production}" \ + --build-arg MESHSTREAM_SITE_TITLE="${MESHSTREAM_SITE_TITLE:-Bay Area Mesh}" \ + --build-arg MESHSTREAM_SITE_DESCRIPTION="${MESHSTREAM_SITE_DESCRIPTION:-Meshtastic activity in the Bay Area region, CA.}" \ + --build-arg MESHSTREAM_GOOGLE_MAPS_ID="${MESHSTREAM_GOOGLE_MAPS_ID}" \ + --build-arg MESHSTREAM_GOOGLE_MAPS_API_KEY="${MESHSTREAM_GOOGLE_MAPS_API_KEY}" \ + -t meshstream:${IMAGE_TAG} . + +echo "Tagging image for ECR repository..." +docker tag meshstream:${IMAGE_TAG} ${ECR_REPOSITORY}:${IMAGE_TAG} + +echo "Logging in to AWS ECR..." +aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${ECR_REPOSITORY%/*} + +echo "Pushing image to ECR repository..." +docker push ${ECR_REPOSITORY}:${IMAGE_TAG} + +echo "Successfully pushed image to ${ECR_REPOSITORY}:${IMAGE_TAG}" \ No newline at end of file