deploy-node-network.yml 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. name: Deploy Network
  2. on:
  3. workflow_dispatch:
  4. inputs:
  5. jsonInput:
  6. description: 'Json input (refer to .pipelines/deploy-node-network-inputs.json)'
  7. required: true
  8. default: ''
  9. defaults:
  10. run:
  11. working-directory: devops/aws
  12. jobs:
  13. deploy-node-network:
  14. name: Create CloudFormation stack and run Ansible playbook
  15. runs-on: ubuntu-latest
  16. env:
  17. STACK_NAME: ga-deploy-node-network-${{ github.run_number }}
  18. steps:
  19. - name: Checkout
  20. uses: actions/checkout@v2
  21. - name: Set env variables
  22. id: myoutputs # set the outputs
  23. run: |
  24. jsonInput=$(jq -r '.inputs.jsonInput' $GITHUB_EVENT_PATH)
  25. export ENCRYPTION_KEY=$(echo $jsonInput | jq -r '.encryptionKey.value')
  26. echo ::set-output name=encryptionKey::$ENCRYPTION_KEY
  27. echo "::add-mask::$ENCRYPTION_KEY"
  28. echo ::set-output name=gitRepo::$(echo $jsonInput | jq -r '.gitRepo.value')
  29. echo ::set-output name=branchName::$(echo $jsonInput | jq -r '.branchName.value')
  30. echo ::set-output name=keyName::$(echo $jsonInput | jq -r '.keyName.value')
  31. echo ::set-output name=numberOfValidators::$(echo $jsonInput | jq -r '.numberOfValidators.value')
  32. echo ::set-output name=validatorInstanceType::$(echo $jsonInput | jq -r '.validatorInstanceType.value')
  33. echo ::set-output name=buildInstanceType::$(echo $jsonInput | jq -r '.buildInstanceType.value')
  34. echo ::set-output name=rpcInstanceType::$(echo $jsonInput | jq -r '.rpcInstanceType.value')
  35. echo ::set-output name=ec2AMI::$(echo $jsonInput | jq -r '.ec2AMI.value')
  36. echo ::set-output name=networkSuffix::$(echo $jsonInput | jq -r '.networkSuffix.value')
  37. echo ::set-output name=deploymentType::$(echo $jsonInput | jq -r '.deploymentType.value')
  38. initialBalancesFile=$(echo $jsonInput | jq -r '.initialBalancesFile.value')
  39. initialMembersFile=$(echo $jsonInput | jq -r '.initialMembersFile.value')
  40. if [ -z "$initialBalancesFile" ]
  41. then
  42. echo ::set-output name=initialBalancesFilePath::''
  43. else
  44. wget $initialBalancesFile -O initial-balances.json
  45. echo ::set-output name=initialBalancesFilePath::'initial-balances.json'
  46. fi
  47. if [ -z "$initialMembersFile" ]
  48. then
  49. echo ::set-output name=initialMembersFilePath::''
  50. else
  51. wget $initialMembersFile -O initial-members.json
  52. echo ::set-output name=initialMembersFilePath::'initial-members.json'
  53. fi
  54. - name: Install Ansible dependencies
  55. run: pipx inject ansible-core boto3 botocore
  56. - name: Configure AWS credentials
  57. uses: aws-actions/configure-aws-credentials@v1
  58. with:
  59. aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
  60. aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  61. aws-region: us-east-1
  62. - name: Deploy to AWS CloudFormation
  63. uses: aws-actions/aws-cloudformation-github-deploy@v1
  64. id: deploy_stack
  65. with:
  66. name: ${{ env.STACK_NAME }}
  67. template: devops/aws/cloudformation/infrastructure.yml
  68. no-fail-on-empty-changeset: '1'
  69. parameter-overrides: >-
  70. KeyName=${{ steps.myoutputs.outputs.keyName }},
  71. ValidatorEC2InstanceType=${{ steps.myoutputs.outputs.validatorInstanceType }},
  72. RPCEC2InstanceType=${{ steps.myoutputs.outputs.rpcInstanceType }},
  73. BuildEC2InstanceType=${{ steps.myoutputs.outputs.buildInstanceType }},
  74. EC2AMI=${{ steps.myoutputs.outputs.ec2AMI }},
  75. NumberOfValidators=${{ steps.myoutputs.outputs.numberOfValidators }}
  76. - name: Prepare inventory for Ansible
  77. run: |
  78. ASG=${{ steps.deploy_stack.outputs.AutoScalingId }}
  79. VALIDATORS=""
  80. INSTANCES=$(aws autoscaling describe-auto-scaling-instances \
  81. --query "AutoScalingInstances[?AutoScalingGroupName=='${ASG}'].InstanceId" --output text);
  82. for ID in $INSTANCES
  83. do
  84. IP=$(aws ec2 describe-instances --instance-ids $ID --query "Reservations[].Instances[].PublicIpAddress" --output text)
  85. VALIDATORS+="$IP\n"
  86. done
  87. echo -e "[build]\n${{ steps.deploy_stack.outputs.BuildPublicIp }}\n" >> inventory
  88. echo -e "[validators]\n$VALIDATORS" >> inventory
  89. echo -e "[rpc]\n${{ steps.deploy_stack.outputs.RPCPublicIp }}" >> inventory
  90. cat inventory
  91. - name: Run playbook to compile joystream-node on build server
  92. uses: dawidd6/action-ansible-playbook@v2
  93. if: steps.myoutputs.outputs.ec2AMI == ''
  94. with:
  95. playbook: build-code.yml
  96. directory: devops/aws
  97. requirements: requirements.yml
  98. key: ${{ secrets.SSH_PRIVATE_KEY }}
  99. options: |
  100. --inventory inventory
  101. --extra-vars "branch_name=${{ steps.myoutputs.outputs.branchName }} \
  102. git_repo=${{ steps.myoutputs.outputs.gitRepo }} data_path=mydata"
  103. - name: Run playbook to install additional utils on build server
  104. uses: dawidd6/action-ansible-playbook@v2
  105. if: steps.myoutputs.outputs.ec2AMI == ''
  106. with:
  107. playbook: setup-admin.yml
  108. directory: devops/aws
  109. requirements: requirements.yml
  110. key: ${{ secrets.SSH_PRIVATE_KEY }}
  111. options: |
  112. --inventory inventory
  113. - name: Run playbook to configure chain-spec and pioneer
  114. uses: dawidd6/action-ansible-playbook@v2
  115. with:
  116. playbook: chain-spec-pioneer.yml
  117. directory: devops/aws
  118. requirements: requirements.yml
  119. key: ${{ secrets.SSH_PRIVATE_KEY }}
  120. options: |
  121. --inventory inventory
  122. --extra-vars "network_suffix=${{ steps.myoutputs.outputs.networkSuffix }} data_path=mydata \
  123. number_of_validators=${{ steps.myoutputs.outputs.numberOfValidators }} \
  124. git_repo=${{ steps.myoutputs.outputs.gitRepo }} \
  125. deployment_type=${{ steps.myoutputs.outputs.deploymentType }} \
  126. bucket_name=${{ steps.deploy_stack.outputs.S3BucketName }} \
  127. branch_name=${{ steps.myoutputs.outputs.branchName }} \
  128. initial_members_file=${{ steps.myoutputs.outputs.initialMembersFilePath }} \
  129. initial_balances_file=${{ steps.myoutputs.outputs.initialBalancesFilePath }}"
  130. - name: Encrpyt the artifacts
  131. run: |
  132. 7z a -p${{ steps.myoutputs.outputs.encryptionKey }} chain-data.7z mydata/*
  133. - name: Save the output as an artifact
  134. uses: actions/upload-artifact@v2
  135. with:
  136. name: data-chainspec-auth
  137. path: devops/aws/chain-data.7z
  138. - name: Print output URL's
  139. run: |
  140. echo -e "Pioneer URL: https://${{ steps.deploy_stack.outputs.DomainName }}"
  141. echo -e "RPC: wss://${{ steps.deploy_stack.outputs.RPCPublicIp }}.nip.io/ws-rpc"
  142. - name: Delete CloudFormation Stack if any step failed
  143. if: failure()
  144. run: |
  145. echo "Empty the bucket"
  146. aws s3 rm s3://${{ steps.deploy_stack.outputs.S3BucketName }} --recursive || echo "No bucket"
  147. echo "Deleting ${{ env.STACK_NAME }} stack"
  148. aws cloudformation delete-stack --stack-name ${{ env.STACK_NAME }}
  149. echo "Waiting for ${{ env.STACK_NAME }} to be deleted..."
  150. aws cloudformation wait stack-delete-complete --stack-name ${{ env.STACK_NAME }}