Skip to content

Instantly share code, notes, and snippets.

@rkrishnasanka
Last active March 25, 2024 13:33
Show Gist options
  • Save rkrishnasanka/a702eb8f26bc90a697ea9e4f31251819 to your computer and use it in GitHub Desktop.
Save rkrishnasanka/a702eb8f26bc90a697ea9e4f31251819 to your computer and use it in GitHub Desktop.
Simple Docker Compose -> EC2 Deploy
#!/usr/bin/env bash
TARGET='main'
ACTION='\033[1;90m'
NOCOLOR='\033[0m'
cd ~/<fill the right code location> || exit
if [[ -z "$1" && $1 != "--force" ]]; then
# Checking if we are on the main branch
echo -e "${ACTION}"Checking Git repo
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$BRANCH" != ${TARGET} ]
then
exit 0
fi
# Checking if the repository is up to date.
git fetch
HEADHASH=$(git rev-parse HEAD)
UPSTREAMHASH=$(git rev-parse ${TARGET}@{upstream})
if [ "$HEADHASH" == "$UPSTREAMHASH" ]
then
echo -e "${FINISHED}"Current branch is up to date with origin/${TARGET}."${NOCOLOR}"
exit 0
fi
# check diskspace
DISK_SIZE_FREE=$(df -k . | tail -n1 | awk '{print $4}')
if [[ $DISK_SIZE_FREE -lt 524288 ]];
then
# less than 0.5GB
docker system prune -a;
fi;
# Force reset, checkout, and then pull the latest changes.
git reset --hard;
git checkout -- "$(git status | grep modified: | cut -c14-)";
git pull -f origin main;
fi
echo "Stop Dockering containers"
make stop;
sleep 1;
echo "Building and starting prod containers"
make prod;
# this moves over any static files
echo "Building front-end code"
make npm-build;
echo "Moving over static file."
docker-compose exec web ./manage.py collectstatic --noinput;
echo "DONE!"
exit 0;
def get_package_version():
try:
toml_path = Path(__file__).parent.joinpath("pyproject.toml")
with open(toml_path.absolute(), "r", encoding="utf-8") as file:
data = toml.load(file)
version_info = data.get("tool", {}).get("poetry", {}).get("version")
if version_info is not None:
return version_info
else:
raise KeyError("Could not find version information in pyproject.toml.")
except FileNotFoundError:
raise FileNotFoundError("pyproject.toml file not found.")
@app.route("/")
def healthcheck():
return f"Backend Version: {get_package_version()}"
name: Deploy to Staging on AWS EC2
on:
push:
branches:
- staging
# workflow_run:
# workflows: ["build_front-end", "tests"]
# types:
# - completed
jobs:
deploy:
name: Deploy Staging
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- name: Deploying Application
uses: appleboy/ssh-action@master
# run: |
# echo "${{ secrets.SSH_AWS_SERVER_IP }}" | sed 's/./& /g'
# echo "${{ secrets.SSH_SERVER_USER }}" | sed 's/./& /g'
# echo "${{ secrets.SSH_PRIVATE_KEY }}" | sed 's/./& /g'
with:
host: ${{ secrets.SSH_AWS_SERVER_IP }}
username: ${{ secrets.SSH_SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd upp_bio/scripts
./docker-deploy.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment