Skip to content

Commit

Permalink
Merge branch 'ft/deployment-setup' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
thepsalmist committed Apr 4, 2024
2 parents 2993707 + d69225e commit 3e0da20
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 29 deletions.
24 changes: 14 additions & 10 deletions .github/workflows/docker-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ on:
tags:
- '**'
env:
IMAGE_NAME: "codeforafrica/news-search-api"
IMAGE_NAME: "mcsystems/news-search-api"
BETA_VERSION: "b1"

jobs:

Expand All @@ -32,22 +33,25 @@ jobs:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Determine Tag
id: tag
run: |
if [ "${{ github.ref_type }}" = "tag" ] && [ "${{ github.ref }}" != "refs/heads/staging" ]; then
echo "tag_name=${{ github.ref_name }}" >> $GITHUB_OUTPUT
else
echo "tag_name=staging" >> $GITHUB_OUTPUT
fi
- name: Docker meta
id: meta
uses: docker/metadata-action@v3
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=sha
type=raw,value=staging-${{ env.BETA_VERSION }},enable=${{ endsWith(github.ref, 'staging') }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.tag_name }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
#registry defaults to dockerhub
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache
cache-to: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache,mode=max
67 changes: 55 additions & 12 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,38 @@

# Environment variables

IMAGE_TAG="latest" # Change this based on your use case
IMAGE_TAG="latest" # Change this based on deployment (staging, production, v1.0)
INDEXES="mc_search"
ESHOSTS="http://ramos.angwin:9200,http://woodward.angwin:9200,http://bradley.angwin:9200"
ESOPTS="{'timeout': 60, 'max_retries': 3}" # 'timeout' parameter is deprecated
ELASTICSEARCH_INDEX_NAME_PREFIX="mc_search-*"
TERMFIELDS="article_title,text_content"
TERMAGGRS="top,significant,rare"
APP_NAME="news-search-api"

# Check if running as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root."
is_root() {
if [ $(whoami) != "root" ]; then
echo "This script must be run as root."
exit 1
fi
}

is_root
echo "Running as root"

LOGIN_USER=$(who am i | awk '{ print $1 }')
if [ "x$LOGIN_USER" = x ]; then
# XXX fall back to whoami (look by uid)
echo could not find login user 1>&2
exit 1
fi

run_as_login_user() {
is_root
su $LOGIN_USER -c "$*"
}

help()
{
echo "Usage: ./deploy.sh [options]"
Expand All @@ -29,6 +47,11 @@ log()
{
echo "$1"
}

zzz() {
echo $1 | tr 'A-Za-z' 'N-ZA-Mn-za-m'
}

# Parse command-line options
while getopts :h:t optname; do
log "Option $optname set with value ${OPTARG}"
Expand All @@ -49,25 +72,45 @@ while getopts :h:t optname; do
done

# Create a directory for private configuration
PRIVATE_CONF_DIR="news_search_api"
PRIVATE_CONF_DIR="news_search_api_config"
rm -rf "$PRIVATE_CONF_DIR"
mkdir -p "$PRIVATE_CONF_DIR"
chmod go-rwx "$PRIVATE_CONF_DIR"
CONFIG_REPO_PREFIX=$(zzz tvg@tvguho.pbz:zrqvnpybhq)
CONFIG_REPO_NAME=$(zzz arjf-frnepu-ncv-pbasvt)
echo cloning $CONFIG_REPO_NAME repo 1>&2
if ! run_as_login_user "git clone $CONFIG_REPO_PREFIX/$CONFIG_REPO_NAME.git" >/dev/null 2>&1; then
echo "FATAL: could not clone config repo" 1>&2
exit 1
fi
PRIVATE_CONF_REPO=$(pwd)/$CONFIG_REPO_NAME
PRIVATE_CONF_FILE=$PRIVATE_CONF_REPO/$APP_NAME.sh
cd ..

if [ ! -f $PRIVATE_CONF_FILE ]; then
echo "FATAL: could not access $PRIVATE_CONF_FILE" 1>&2
exit 1
fi
#source private conf to load SENTRY_DSN
. $PRIVATE_CONF_FILE

# Fetch the docker-compose.yml file from GitHub
CONFIG_REPO_PREFIX="https://github.com/mediacloud" # Replace with your actual GitHub URL
CONFIG_REPO_NAME="news-search-api" # Change to your actual repo name
DOCKER_COMPOSE_FILE="docker-compose.yml" # Name of the Docker Compose file
echo "Fetching $DOCKER_COMPOSE_FILE from $CONFIG_REPO_NAME repo..."
if ! curl -sSfL "$CONFIG_REPO_PREFIX/$CONFIG_REPO_NAME/raw/main/$DOCKER_COMPOSE_FILE" -o "$PRIVATE_CONF_DIR/$DOCKER_COMPOSE_FILE"; then

INSTALL_DIR="news_search_api"
mkdir -p "$INSTALL_DIR"

GH_REPO_PREFIX="https://github.com/mediacloud"
GH_REPO_NAME="news-search-api"
DOCKER_COMPOSE_FILE="docker-compose.yml"
echo "Fetching $DOCKER_COMPOSE_FILE from $GH_REPO_NAME repo..."
if ! curl -sSfL "$GH_REPO_PREFIX/$GH_REPO_NAME/raw/$IMAGE_TAG/$DOCKER_COMPOSE_FILE" -o "$INSTALL_DIR/$DOCKER_COMPOSE_FILE"; then
echo "FATAL: Could not fetch $DOCKER_COMPOSE_FILE from config repo"
exit 1ls
exit 1

fi

# Deploy services using Docker Compose
echo "Deploying services with image tag: $IMAGE_TAG"
docker-compose -f "$PRIVATE_CONF_DIR/$DOCKER_COMPOSE_FILE" up -d
docker-compose -f "$INSTALL_DIR/$DOCKER_COMPOSE_FILE" up -d

# Additional steps (e.g., database migrations, cache clearing, etc.)
# ...
Expand Down
14 changes: 7 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
services:
api:
image: mcsystems/news-search-api:{IMAGE_TAG}
image: mcsystems/news-search-api:${IMAGE_TAG}
build: .
ports:
- 8000:8000
networks:
- story-indexer
- new-search-api
volumes:
- .:/app

ui:
image: mcsystems/news-search-api:{IMAGE_TAG}
image: mcsystems/news-search-api:${IMAGE_TAG}
build: .
environment:
APIURL: http://api:8000/v1
ports:
- 8001:8501
- 8501:8501
networks:
- story-indexer
- new-search-api
volumes:
- .:/app
depends_on:
- api
command: streamlit run ui.py

networks:
story-indexer:
name: story-indexer
new-search-api:
name: new-search-api

0 comments on commit 3e0da20

Please sign in to comment.