Created
December 18, 2024 15:43
-
-
Save amkisko/feffa528957d9217e3ea72c567872412 to your computer and use it in GitHub Desktop.
Heroku deploy script for multi-stage docker containers with Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
app_name="my-heroku-app" | |
environment="production" | |
db_migrate_container="migrate" | |
app_containers="web worker" | |
app_url="https://amkisko.github.io" | |
github_org="amkisko" | |
github_repo="my-app" | |
docker_platform="linux/amd64" | |
function log() { | |
echo | |
echo -e "\033[1;32m$1\033[0m" | |
} | |
function run() { | |
log "$1" | |
eval "$1" | |
} | |
function run_local() { | |
if [[ -z "${REMOTE_DEPLOYMENT}" ]]; then | |
run "$1" | |
fi | |
} | |
uncommitted_changes=$(git status --porcelain) | |
if [[ -n "${uncommitted_changes}" ]]; then | |
echo "There are uncommitted changes. Please commit or stash them before deploying." | |
echo "${uncommitted_changes}" | |
exit 1 | |
fi | |
heroku_whoami=$(heroku auth:whoami) | |
run "heroku auth:token | docker login --username=${heroku_whoami} --password-stdin registry.heroku.com >/dev/null" | |
branch_name=$(git rev-parse --abbrev-ref HEAD) | |
system_dockerfile="usr/share/system/Dockerfile" | |
system_image="registry.heroku.com/${app_name}/system-${branch_name}" | |
run "docker-buildx build -t ${app_name}:system-${branch_name} -f ${system_dockerfile} --platform ${docker_platform} ." | |
run "docker tag ${app_name}:system-${branch_name} ${system_image}" | |
run "docker push ${system_image}" | |
release_tag="release/${environment}-$(date -u +"%Y-%m-%dT%H%M%SZ")" | |
git_commit_sha=$(git rev-parse HEAD) | |
rails_dockerfile="usr/share/rails/Dockerfile" | |
run "docker-buildx build -t ${app_name}:latest -f ${rails_dockerfile} --platform ${docker_platform} --build-arg base_image=${system_image} --build-arg environment=${environment} --build-arg git_commit_sha=${git_commit_sha} ." | |
for container in ${db_migrate_container} $(echo ${app_containers}); do | |
tag="registry.heroku.com/${app_name}/${container}" | |
run "docker tag ${app_name}:latest ${tag}" | |
run "docker push ${tag}" | |
done | |
run "heroku container:release ${db_migrate_container} --app ${app_name}" | |
run "heroku run \"/bin/bash -c 'bundle exec rails db:migrate'\" --type=${db_migrate_container} --app ${app_name} --exit-code" | |
run "heroku container:rm migrate --app ${app_name}" | |
run "heroku container:release ${app_containers} --app ${app_name}" | |
run "gh api -X POST /repos/${github_org}/${github_repo}/git/refs -f ref=refs/tags/${release_tag} -f sha=${git_commit_sha}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment