Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: support sub-accounts #184

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
307 changes: 216 additions & 91 deletions .codebuild/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,94 +9,219 @@ send_slack_message() {
-X POST https://slack.com/api/chat.postMessage;
}

echo "Building git ref ${GIT_REF_TO_DEPLOY}..."

exit=false;

# Checks whether there is a file called "rollback_mainnet_production", which is used by our other CodeBuild to indicate that this is a mainnet-production rollback
if [ -f "rollback_mainnet_production" ]; then
# Gets all env vars with `mainnet_` prefix and re-exports them without the prefix
for var in "${!mainnet_@}"; do
export ${var#mainnet_}="${!var}"
done
make deploy-lambdas-mainnet;
send_slack_message "Rollback performed on mainnet-production to: ${GIT_REF_TO_DEPLOY}";
exit=true;
fi;

# Checks whether there is a file called "rollback_testnet_production", which is used by our other CodeBuild to indicate that this is a testnet-production rollback
if [ -f "rollback_testnet_production" ]; then
# Gets all env vars with `testnet_` prefix and re-exports them without the prefix
for var in "${!testnet_@}"; do
export ${var#testnet_}="${!var}"
done
make deploy-lambdas-testnet;
send_slack_message "Rollback performed on testnet-production to: ${GIT_REF_TO_DEPLOY}";
exit=true;
fi;

if [ "$exit" = true ]; then
echo "Rollbacks performed successfully. Exiting now.";
exit 0;
fi

if expr "${GIT_REF_TO_DEPLOY}" : "master" >/dev/null; then
# Gets all env vars with `dev_` prefix and re-exports them without the prefix
for var in "${!dev_@}"; do
export ${var#dev_}="${!var}"
done

make migrate;
make build-daemon;
make deploy-lambdas-dev-testnet;
# The idea here is that if the lambdas deploy fail, the built image won't be pushed:
make push-daemon;

elif expr "${GIT_REF_TO_DEPLOY}" : "v[0-9]\+\.[0-9]\+\.[0-9]\+-rc\.[0-9]\+" >/dev/null; then
# Gets all env vars with `mainnet_staging_` prefix and re-exports them without the prefix
for var in "${!mainnet_staging_@}"; do
export ${var#mainnet_staging_}="${!var}"
done

echo $GIT_REF_TO_DEPLOY > /tmp/docker_image_tag
make migrate;
make build-daemon;
make deploy-lambdas-mainnet-staging;
make push-daemon;
send_slack_message "New version deployed to mainnet-staging: ${GIT_REF_TO_DEPLOY}"
elif expr "${GIT_REF_TO_DEPLOY}" : "v.*" >/dev/null; then
# Gets all env vars with `testnet_` prefix and re-exports them without the prefix
for var in "${!testnet_@}"; do
export ${var#testnet_}="${!var}"
done

echo $GIT_REF_TO_DEPLOY > /tmp/docker_image_tag
make migrate;
make build-daemon;
make deploy-lambdas-testnet;
make push-daemon;

# Unsets all the testnet env vars so we make sure they don't leak to the mainnet deploy below
for var in "${!testnet_@}"; do
unset ${var#testnet_}
done

# Gets all env vars with `mainnet_` prefix and re-exports them without the prefix
for var in "${!mainnet_@}"; do
export ${var#mainnet_}="${!var}"
done
make migrate;
make build-daemon;
make deploy-lambdas-mainnet;
make push-daemon;
send_slack_message "New version deployed to testnet-production and mainnet-production: ${GIT_REF_TO_DEPLOY}"
else
# Gets all env vars with `dev_` prefix and re-exports them without the prefix
for var in "${!dev_@}"; do
export ${var#dev_}="${!var}"
done
make migrate;
make build-daemon;
make deploy-lambdas-dev-testnet;
make push-daemon;
fi;
deploy_hathor_network_account() {
exit=false;

# Checks whether there is a file called "rollback_mainnet_production", which is used by our other CodeBuild to indicate that this is a mainnet-production rollback
if [ -f "rollback_mainnet_production" ]; then
# Gets all env vars with `mainnet_` prefix and re-exports them without the prefix
for var in "${!mainnet_@}"; do
export ${var#mainnet_}="${!var}"
done
make deploy-lambdas-mainnet;
send_slack_message "Rollback performed on mainnet-production to: ${GIT_REF_TO_DEPLOY}";
exit=true;
fi;

# Checks whether there is a file called "rollback_testnet_production", which is used by our other CodeBuild to indicate that this is a testnet-production rollback
if [ -f "rollback_testnet_production" ]; then
# Gets all env vars with `testnet_` prefix and re-exports them without the prefix
for var in "${!testnet_@}"; do
export ${var#testnet_}="${!var}"
done
make deploy-lambdas-testnet;
send_slack_message "Rollback performed on testnet-production to: ${GIT_REF_TO_DEPLOY}";
exit=true;
fi;

if [ "$exit" = true ]; then
echo "Rollbacks performed successfully. Exiting now.";
exit 0;
fi

if expr "${GIT_REF_TO_DEPLOY}" : "master" >/dev/null; then
# Gets all env vars with `dev_` prefix and re-exports them without the prefix
for var in "${!dev_@}"; do
export ${var#dev_}="${!var}"
done

make migrate;
make build-daemon;
make deploy-lambdas-dev-testnet;
# The idea here is that if the lambdas deploy fail, the built image won't be pushed:
make push-daemon;

elif expr "${GIT_REF_TO_DEPLOY}" : "v[0-9]\+\.[0-9]\+\.[0-9]\+-rc\.[0-9]\+" >/dev/null; then
# Gets all env vars with `mainnet_staging_` prefix and re-exports them without the prefix
for var in "${!mainnet_staging_@}"; do
export ${var#mainnet_staging_}="${!var}"
done

echo $GIT_REF_TO_DEPLOY > /tmp/docker_image_tag
make migrate;
make build-daemon;
make deploy-lambdas-mainnet-staging;
# The idea here is that if the lambdas deploy fail, the built image won't be pushed:
make push-daemon;
send_slack_message "New version deployed to mainnet-staging: ${GIT_REF_TO_DEPLOY}"
elif expr "${GIT_REF_TO_DEPLOY}" : "v.*" >/dev/null; then
echo $GIT_REF_TO_DEPLOY > /tmp/docker_image_tag
make build-daemon;

# --- Testnet ---
# Gets all env vars with `testnet_` prefix and re-exports them without the prefix
for var in "${!testnet_@}"; do
export ${var#testnet_}="${!var}"
done

make migrate;
make deploy-lambdas-testnet;

# Unsets all the testnet env vars so we make sure they don't leak to other deploys
for var in "${!testnet_@}"; do
unset ${var#testnet_}
done

# --- Mainnet ---
# Gets all env vars with `mainnet_` prefix and re-exports them without the prefix
for var in "${!mainnet_@}"; do
export ${var#mainnet_}="${!var}"
done
make migrate;
make deploy-lambdas-mainnet;

# Unsets all the mainnet env vars so we make sure they don't leak to other deploys
for var in "${!mainnet_@}"; do
unset ${var#mainnet_}
done

# The idea here is that if the lambdas deploy fail, the built image won't be pushed:
make push-daemon;
send_slack_message "New version deployed to testnet-production and mainnet-production: ${GIT_REF_TO_DEPLOY}"
else
# Gets all env vars with `dev_` prefix and re-exports them without the prefix
for var in "${!dev_@}"; do
export ${var#dev_}="${!var}"
done
make migrate;
make build-daemon;
make deploy-lambdas-dev-testnet;
# The idea here is that if the lambdas deploy fail, the built image won't be pushed:
make push-daemon;
fi;
}

deploy_nano_testnet() {
# Deploys the releases and release-candidates to our nano-testnet environment

# We deploy only the Lambdas here, because the daemon used in nano-testnet is the same as
# the one built in the hathor-network account, since it runs there as well

echo "Building git ref ${GIT_REF_TO_DEPLOY}..."

# This will match both releases and release-candidates
if expr "${GIT_REF_TO_DEPLOY}" : "v.*" >/dev/null; then
make migrate;
make deploy-lambdas-nano-testnet;

send_slack_message "New version deployed to nano-testnet: ${GIT_REF_TO_DEPLOY}"
elif expr "${MANUAL_DEPLOY}" : "true" >/dev/null; then
make migrate;
make deploy-lambdas-nano-testnet;

send_slack_message "Branch manually deployed to nano-testnet: ${GIT_REF_TO_DEPLOY}"
elif expr "${ROLLBACK}" : "true" >/dev/null; then
make migrate;
make deploy-lambdas-nano-testnet;

send_slack_message "Rollback performed on nano-tesnet to: ${GIT_REF_TO_DEPLOY}";
else
echo "We don't deploy ${GIT_REF_TO_DEPLOY} to nano-testnet. Nothing to do.";
fi;
}

deploy_ekvilibro_mainnet() {
# Deploys the releases to our ekvilibro-mainnet environment

# We deploy only the Lambdas here, because the daemon used in ekvilibro-testnet is the same as
# the one built in the hathor-network account, since it runs there as well

echo "Building git ref ${GIT_REF_TO_DEPLOY}..."

# This will match release-candidates
if expr "${GIT_REF_TO_DEPLOY}" : "v[0-9]\+\.[0-9]\+\.[0-9]\+-rc\.[0-9]\+" >/dev/null; then
echo "We don't deploy ${GIT_REF_TO_DEPLOY} to ekvilibro-mainnet. Nothing to do.";
# This will match releases only (since release-candidates are already matched above)
elif expr "${GIT_REF_TO_DEPLOY}" : "v.*" >/dev/null; then
make migrate;
make deploy-lambdas-ekvilibro-mainnet;

send_slack_message "New version deployed to ekvilibro-mainnet: ${GIT_REF_TO_DEPLOY}"
elif expr "${MANUAL_DEPLOY}" : "true" >/dev/null; then
make migrate;
make deploy-lambdas-ekvilibro-mainnet;

send_slack_message "Branch manually deployed to ekvilibro-mainnet: ${GIT_REF_TO_DEPLOY}"
elif expr "${ROLLBACK}" : "true" >/dev/null; then
make migrate;
make deploy-lambdas-ekvilibro-mainnet;

send_slack_message "Rollback performed on ekvilibro-mainnet to: ${GIT_REF_TO_DEPLOY}";
else
echo "We don't deploy ${GIT_REF_TO_DEPLOY} to ekvilibro-mainnet. Nothing to do.";
fi;

}

deploy_ekvilibro_testnet() {
# Deploys the release-candidates and releases to our ekvilibro-testnet environment

# We deploy only the Lambdas here, because the daemon used in ekvilibro-testnet is the same as
# the one built in the hathor-network account, since it runs there as well

echo "Building git ref ${GIT_REF_TO_DEPLOY}..."

# This will match release-candidates or releases
if expr "${GIT_REF_TO_DEPLOY}" : "v.*" >/dev/null; then
make migrate;
make deploy-lambdas-ekvilibro-testnet;

send_slack_message "New version deployed to ekvilibro-testnet: ${GIT_REF_TO_DEPLOY}"
elif expr "${MANUAL_DEPLOY}" : "true" >/dev/null; then
make migrate;
make deploy-lambdas-ekvilibro-testnet;

send_slack_message "Branch manually deployed to ekvilibro-testnet: ${GIT_REF_TO_DEPLOY}"
elif expr "${ROLLBACK}" : "true" >/dev/null; then
make migrate;
make deploy-lambdas-ekvilibro-testnet;

send_slack_message "Rollback performed on ekvilibro-testnet to: ${GIT_REF_TO_DEPLOY}";
else
echo "We don't deploy ${GIT_REF_TO_DEPLOY} to ekvilibro-testnet. Nothing to do.";
fi;
}


# Check the first argument for the desired deploy
option=$1

case $option in
# This will be triggered from /.codebuild/buildspec.yml in this repo
hathor-network)
deploy_hathor_network_account
;;
nano-testnet)
deploy_nano_testnet
;;
ekvilibro-testnet)
deploy_ekvilibro_testnet
;;
ekvilibro-mainnet)
deploy_ekvilibro_mainnet
;;
*)
echo "Invalid option: $option"
exit 1
;;
esac
2 changes: 1 addition & 1 deletion .codebuild/buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,4 @@ phases:
- export GIT_REF_TO_DEPLOY=$(cat git_ref_to_deploy)
build:
commands:
- bash .codebuild/build.sh
- bash .codebuild/build.sh hathor-network
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ build-daemon:
push-daemon:
bash scripts/push-daemon.sh

.PHONY: deploy-lambdas-nano
deploy-lambdas-nano:
.PHONY: deploy-lambdas-nano-testnet
deploy-lambdas-nano-testnet:
AWS_SDK_LOAD_CONFIG=1 yarn workspace wallet-service run serverless deploy --stage nano --region eu-central-1 --aws-profile nano-testnet

.PHONY: deploy-lambdas-ekvilibro-testnet
deploy-lambdas-ekvilibro-testnet:
AWS_SDK_LOAD_CONFIG=1 yarn workspace wallet-service run serverless deploy --stage ekvilibro --region eu-central-1 --aws-profile ekvilibro

.PHONY: deploy-lambdas-ekvilibro-mainnet
deploy-lambdas-ekvilibro-mainnet:
AWS_SDK_LOAD_CONFIG=1 yarn workspace wallet-service run serverless deploy --stage ekvi-main --region eu-central-1 --aws-profile ekvilibro

.PHONY: deploy-lambdas-dev-testnet
deploy-lambdas-dev-testnet:
AWS_SDK_LOAD_CONFIG=1 yarn workspace wallet-service run serverless deploy --stage dev-testnet --region eu-central-1
Expand Down
Loading