joystream-node-docker.yml 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # Production runtime build of joystream-node
  2. name: joystream-node-docker
  3. on: push
  4. env:
  5. REPOSITORY: joystream/node
  6. KEY_NAME: joystream-github-action-key-new
  7. jobs:
  8. push-amd64:
  9. name: Build joystream/node Docker image for amd64
  10. runs-on: ubuntu-latest
  11. outputs:
  12. tag_shasum: ${{ steps.compute_shasum.outputs.shasum }}
  13. image_exists: ${{ steps.compute_main_image_exists.outputs.image_exists }}
  14. steps:
  15. - name: Checkout
  16. uses: actions/checkout@v2
  17. - uses: actions/setup-node@v1
  18. with:
  19. node-version: '14.x'
  20. - id: compute_shasum
  21. name: Compute runtime code shasum
  22. run: |
  23. export RUNTIME_CODE_SHASUM=`scripts/runtime-code-shasum.sh`
  24. echo "::set-output name=shasum::${RUNTIME_CODE_SHASUM}"
  25. - name: Login to DockerHub
  26. uses: docker/login-action@v1
  27. with:
  28. username: ${{ secrets.DOCKERHUB_USERNAME }}
  29. password: ${{ secrets.DOCKERHUB_PASSWORD }}
  30. - name: Check if we have already have the manifest on Dockerhub
  31. id: compute_main_image_exists
  32. # Will output 0 if image exists and 1 if does not exists
  33. run: |
  34. export IMAGE_EXISTS=$(docker manifest inspect ${{ env.REPOSITORY }}:${{ steps.compute_shasum.outputs.shasum }} > /dev/null ; echo $?)
  35. echo "::set-output name=image_exists::${IMAGE_EXISTS}"
  36. - name: Check if we have pre-built image on Dockerhub
  37. id: compute_image_exists
  38. # Will output 0 if image exists and 1 if does not exists
  39. run: |
  40. export IMAGE_EXISTS=$(docker manifest inspect ${{ env.REPOSITORY }}:${{ steps.compute_shasum.outputs.shasum }}-amd64 > /dev/null ; echo $?)
  41. echo "::set-output name=image_exists::${IMAGE_EXISTS}"
  42. - name: Build and push
  43. uses: docker/build-push-action@v2
  44. with:
  45. context: .
  46. file: joystream-node.Dockerfile
  47. platforms: linux/amd64
  48. push: true
  49. tags: ${{ env.REPOSITORY }}:${{ steps.compute_shasum.outputs.shasum }}-amd64
  50. if: ${{ steps.compute_image_exists.outputs.image_exists == 1 }}
  51. push-arm:
  52. name: Build joystream/node Docker image for arm
  53. runs-on: ubuntu-latest
  54. strategy:
  55. matrix:
  56. platform: ['linux/arm64', 'linux/arm/v7']
  57. include:
  58. - platform: 'linux/arm64'
  59. platform_tag: 'arm64'
  60. file: 'joystream-node.Dockerfile'
  61. - platform: 'linux/arm/v7'
  62. platform_tag: 'arm'
  63. file: 'joystream-node-armv7.Dockerfile'
  64. env:
  65. STACK_NAME: build-joystream-node-docker-ga-${{ github.run_number }}-${{ matrix.platform_tag }}
  66. steps:
  67. - name: Extract branch name
  68. shell: bash
  69. run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
  70. id: extract_branch
  71. - name: Checkout
  72. uses: actions/checkout@v2
  73. - uses: actions/setup-node@v1
  74. with:
  75. node-version: '14.x'
  76. - name: Install Ansible dependencies
  77. run: pipx inject ansible-core boto3 botocore
  78. - id: compute_shasum
  79. name: Compute runtime code shasum
  80. run: |
  81. export RUNTIME_CODE_SHASUM=`scripts/runtime-code-shasum.sh`
  82. echo "::set-output name=shasum::${RUNTIME_CODE_SHASUM}"
  83. - name: Login to DockerHub
  84. uses: docker/login-action@v1
  85. with:
  86. username: ${{ secrets.DOCKERHUB_USERNAME }}
  87. password: ${{ secrets.DOCKERHUB_PASSWORD }}
  88. - name: Check if we have pre-built image on Dockerhub
  89. id: compute_image_exists
  90. # Will output 0 if image exists and 1 if does not exists
  91. run: |
  92. export IMAGE_EXISTS=$(docker manifest inspect ${{ env.REPOSITORY }}:${{ steps.compute_shasum.outputs.shasum }}-${{ matrix.platform_tag }} > /dev/null ; echo $?)
  93. echo "::set-output name=image_exists::${IMAGE_EXISTS}"
  94. - name: Configure AWS credentials
  95. uses: aws-actions/configure-aws-credentials@v1
  96. with:
  97. aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
  98. aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  99. aws-region: us-east-1
  100. if: ${{ steps.compute_image_exists.outputs.image_exists == 1 }}
  101. - name: Deploy to AWS CloudFormation
  102. uses: aws-actions/aws-cloudformation-github-deploy@v1
  103. id: deploy_stack
  104. with:
  105. name: ${{ env.STACK_NAME }}
  106. template: devops/aws/cloudformation/single-instance-docker.yml
  107. no-fail-on-empty-changeset: '1'
  108. parameter-overrides: 'KeyName=${{ env.KEY_NAME }},EC2AMI=ami-00d1ab6b335f217cf,EC2InstanceType=t4g.xlarge'
  109. if: ${{ steps.compute_image_exists.outputs.image_exists == 1 }}
  110. - name: Run playbook
  111. uses: dawidd6/action-ansible-playbook@v2
  112. with:
  113. playbook: build-arm64-playbook.yml
  114. directory: devops/aws
  115. requirements: requirements.yml
  116. key: ${{ secrets.SSH_PRIVATE_KEY }}
  117. inventory: |
  118. [all]
  119. ${{ steps.deploy_stack.outputs.PublicIp }}
  120. options: |
  121. --extra-vars "git_repo=https://github.com/${{ github.repository }} \
  122. branch_name=${{ steps.extract_branch.outputs.branch }} \
  123. docker_username=${{ secrets.DOCKERHUB_USERNAME }} \
  124. docker_password=${{ secrets.DOCKERHUB_PASSWORD }} \
  125. tag_name=${{ steps.compute_shasum.outputs.shasum }}-${{ matrix.platform_tag }} \
  126. repository=${{ env.REPOSITORY }} dockerfile=${{ matrix.file }} \
  127. platform=${{ matrix.platform }}"
  128. if: ${{ steps.compute_image_exists.outputs.image_exists == 1 }}
  129. - name: Delete CloudFormation Stack
  130. if: always()
  131. continue-on-error: true
  132. run: |
  133. echo "Deleting ${{ env.STACK_NAME }} stack"
  134. aws cloudformation delete-stack --stack-name ${{ env.STACK_NAME }}
  135. echo "Waiting for ${{ env.STACK_NAME }} to be deleted..."
  136. aws cloudformation wait stack-delete-complete --stack-name ${{ env.STACK_NAME }}
  137. push-manifest:
  138. name: Create manifest using both the arch images
  139. needs: [push-amd64, push-arm]
  140. # Only run this job if the image does not exist with tag equal to the shasum
  141. if: needs.push-amd64.outputs.image_exists == 1
  142. runs-on: ubuntu-latest
  143. env:
  144. TAG_SHASUM: ${{ needs.push-amd64.outputs.tag_shasum }}
  145. steps:
  146. - name: Login to DockerHub
  147. uses: docker/login-action@v1
  148. with:
  149. username: ${{ secrets.DOCKERHUB_USERNAME }}
  150. password: ${{ secrets.DOCKERHUB_PASSWORD }}
  151. - name: Create manifest for multi-arch images
  152. run: |
  153. # get artifacts from previous steps
  154. IMAGE=${{ env.REPOSITORY }}:${{ env.TAG_SHASUM }}
  155. echo $IMAGE
  156. docker pull $IMAGE-amd64
  157. docker pull $IMAGE-arm64
  158. docker pull $IMAGE-arm
  159. docker manifest create $IMAGE $IMAGE-amd64 $IMAGE-arm64 $IMAGE-arm
  160. docker manifest annotate $IMAGE $IMAGE-amd64 --arch amd64
  161. docker manifest annotate $IMAGE $IMAGE-arm64 --arch arm64
  162. docker manifest annotate $IMAGE $IMAGE-arm --arch arm
  163. docker manifest push $IMAGE
  164. - name: Create manifest with latest tag for master
  165. if: github.ref == 'refs/heads/master'
  166. run: |
  167. IMAGE=${{ env.REPOSITORY }}:${{ env.TAG_SHASUM }}
  168. LATEST_TAG=${{ env.REPOSITORY }}:latest
  169. docker manifest create $LATEST_TAG $IMAGE-amd64 $IMAGE-arm64 $IMAGE-arm
  170. docker manifest annotate $LATEST_TAG $IMAGE-amd64 --arch amd64
  171. docker manifest annotate $LATEST_TAG $IMAGE-arm64 --arch arm64
  172. docker manifest annotate $LATEST_TAG $IMAGE-arm --arch arm
  173. docker manifest push $LATEST_TAG