joystream-node-docker.yml 7.6 KB

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