From d724b201149f77c4592bdf2e3d1122c229188775 Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Thu, 12 Sep 2024 13:16:49 -0300 Subject: [PATCH 1/2] ci: support sub-accounts --- .codebuild/build.sh | 277 ++++++++++++++++++++++++++------------- .codebuild/buildspec.yml | 2 +- Makefile | 12 +- 3 files changed, 197 insertions(+), 94 deletions(-) diff --git a/.codebuild/build.sh b/.codebuild/build.sh index 4675f4d1..adddb1b1 100644 --- a/.codebuild/build.sh +++ b/.codebuild/build.sh @@ -9,94 +9,189 @@ 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}" + 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}" + 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}" + 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 \ No newline at end of file diff --git a/.codebuild/buildspec.yml b/.codebuild/buildspec.yml index 46023dfb..1d167ee6 100644 --- a/.codebuild/buildspec.yml +++ b/.codebuild/buildspec.yml @@ -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 diff --git a/Makefile b/Makefile index f24072cb..c297758a 100644 --- a/Makefile +++ b/Makefile @@ -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 From 293a1bd7503671df67e3fb1ce31ab80cdb43d67e Mon Sep 17 00:00:00 2001 From: Luis Helder Date: Thu, 19 Sep 2024 15:17:55 -0300 Subject: [PATCH 2/2] chore: support manual deploys and rollbacks --- .codebuild/build.sh | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/.codebuild/build.sh b/.codebuild/build.sh index adddb1b1..c3fc85bc 100644 --- a/.codebuild/build.sh +++ b/.codebuild/build.sh @@ -125,6 +125,16 @@ deploy_nano_testnet() { 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; @@ -147,6 +157,16 @@ deploy_ekvilibro_mainnet() { 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; @@ -167,6 +187,16 @@ deploy_ekvilibro_testnet() { 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;