Skip to content

Instantly share code, notes, and snippets.

@zunda
Last active October 29, 2023 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zunda/2189218dbf77e7f22c99094c567813a5 to your computer and use it in GitHub Desktop.
Save zunda/2189218dbf77e7f22c99094c567813a5 to your computer and use it in GitHub Desktop.
HerokuアプリのMastodonを更新する
#!/bin/sh
usage='sh create-heroku-app.sh heroku-app-name'
#
# creates a Mastodon app with the specified app name
# following the memo at
# https://github.com/zunda/mastodon/wiki/CreateInstanceOnHeroku
#
set -e
run () {
echo \$ "$*"
eval "$*"
}
check_working_copy () {
echo -n "Verifying cleanness of current branch: "
branch=`git branch --format='%(refname)' --contains=HEAD`
echo $branch
git diff --quiet --exit-code && git diff --quiet --cached --exit-code || { git status; exit 1; }
}
prepare_heroku_app () {
echo Preparing the app
run heroku create
echo Ask Heroku support to raise slug size limit to 600MB
echo https://help.heroku.com/KUFMEES1/my-slug-size-is-too-large-how-can-i-make-it-smaller
run heroku buildpacks:add https://github.com/heroku/heroku-buildpack-apt
run heroku buildpacks:add heroku/nodejs
run heroku buildpacks:add heroku/ruby
run heroku addons:create heroku-postgresql
run heroku addons:create heroku-redis
run heroku config:set \
OTP_SECRET=`ruby -r securerandom -e 'puts SecureRandom.hex(64)'` \
NODE_OPTIONS=--openssl-legacy-provider \
RAILS_SERVE_STATIC_FILES=true \
LOCAL_DOMAIN=`heroku domains --json | jq -r '.[] | select(.kind=="heroku").hostname'`
run heroku pg:wait
run heroku redis:wait
}
deploy () {
echo Pushing the code
run git push "https://git.heroku.com/$HEROKU_APP.git" "$branch:master"
}
# TODO: Test this step
configure () {
echo Applying additional configuration
run heroku run rails db:migrate
run heroku config:set \
HEROKU=true \
SMTP_FROM_ADDRESS=notifications@localhost \
`heroku run rake mastodon:webpush:generate_vapid_key | grep ^VAPID_`
}
# command line
if [ $# -ne 1 ]; then
echo usage: $usage
exit 1
fi
export HEROKU_APP="$1"
echo Creating a Heorku app: "$HEROKU_APP"
check_working_copy
prepare_heroku_app
deploy
configure
#!/bin/sh
set -e
run () {
echo \$ "$*"
eval "$*"
}
confirm () {
echo
echo "$*"
cat
}
check_hash () {
/bin/echo -n Checking code versions.
local=$(git rev-parse HEAD)
for n in 1 2 3 4 5; do
/bin/echo -n .
running=$(curl -s https://$1/api/v1/instance | jq -r .version | grep -oe '[0-9a-f]\{3,\}' || echo version check exited with $?)
if ( echo "$local" | grep -q "$running"; ); then
break
fi
sleep 10
done
if ! ( echo "$local" | grep -q "$running"; ); then
echo
echo "WARNING: Local version $local doesn't match with running version: $running"
else
echo done
fi
}
upstream=https://github.com/mastodon/mastodon.git
branch=use-ruby33-react-router-5
staging=zundan-mastodon-ruby31
# https://mastodon.zunda.ninja/@zundan/106020054402307366
staging_streaming=zundan-mastodon-ruby31-str
production=zundan-mastodon
production_streaming=zundan-mastodon-streaming
production_domain=mastodon.zunda.ninja
# mastodon/mastodonから変更をもらってきて変更内容を確認する
run git fetch origin
git remote | grep -q upstream || run git remote add upstream $upstream
run git fetch upstream
run git log --oneline upstream/main...origin/master
confirm ^C to abort or ^D to proceed to rebase upstream/main
run git checkout master
run git pull --ff-only
run git rebase upstream/main
run git push
# アプリのブランチに変更内容をmergeする
run git checkout $branch
run git pull --ff-only
run git merge --no-edit master
confirm ^C to abort or ^D to proceed to push and deploy
# Heroku gitにpushしてステージングのデプロイを待つ
run git push https://git.heroku.com/$staging.git $branch:master
run heroku ps:scale web=1 -a $staging
run heroku ps:scale web=1 -a $staging_streaming
run sleep 10
# 同時に、必要なら、ステージングのストリーミングに昇格する
desc_staging=`heroku releases:info --json -a $staging | jq -r .description`
desc_staging_str=`heroku releases:info --json -a $staging_streaming | jq -r .description`
if [ "$desc_staging" != "$desc_staging_str" ]; then
run heroku pipelines:promote -a $staging -t $staging_streaming
fi
echo run: heroku logs -t -a $staging \& heroku logs -t -a $staging_streaming
run sleep 20
check_hash $staging.herokuapp.com
# ステージングの動作確認をしてプロダクションに昇格する
confirm ^C to abort or ^D to proceed to promote
run heroku ps:scale web=0 -a $staging
run heroku ps:scale web=0 -a $staging_streaming
run heroku pipelines:promote -a $staging -t $production,$production_streaming
run heroku releases:output -a $production
echo run: heroku logs -t -a $production \& heroku logs -t -a $production_streaming
# originにもコードを公開する
run git push origin $branch
# 結果の確認
run sleep 20
check_hash $production_domain
echo Merged mastodon/mastodon at $(git rev-parse master).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment