joystream-node-docker.yml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_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. - name: Set up QEMU
  20. uses: docker/setup-qemu-action@v1
  21. - name: Set up Docker Buildx
  22. uses: docker/setup-buildx-action@v1
  23. - id: compute_shasum
  24. name: Compute runtime code shasum
  25. run: |
  26. export RUNTIME_CODE_SHASUM=`scripts/runtime-code-shasum.sh`
  27. echo "::set-output name=shasum::${RUNTIME_CODE_SHASUM}"
  28. - name: Login to DockerHub
  29. uses: docker/login-action@v1
  30. with:
  31. username: ${{ secrets.DOCKERHUB_USERNAME }}
  32. password: ${{ secrets.DOCKERHUB_PASSWORD }}
  33. - name: Check if we have pre-built image on Dockerhub
  34. id: compute_image_exists
  35. # Will output 0 if image exists and 1 if does not exists
  36. run: |
  37. export IMAGE_EXISTS=$(docker manifest inspect ${{ env.REPOSITORY }}:${{ steps.compute_shasum.outputs.shasum }}-amd64 > /dev/null ; echo $?)
  38. echo "::set-output name=image_exists::${IMAGE_EXISTS}"
  39. - name: Build and push
  40. uses: docker/build-push-action@v2
  41. with:
  42. context: .
  43. file: joystream-node.Dockerfile
  44. platforms: linux/amd64
  45. push: true
  46. tags: ${{ env.REPOSITORY }}:${{ steps.compute_shasum.outputs.shasum }}-amd64
  47. if: ${{ steps.compute_image_exists.outputs.image_exists == 1 }}
  48. push-arm:
  49. name: Build joystream/node Docker image for arm
  50. runs-on: ubuntu-latest
  51. env:
  52. STACK_NAME: joystream-ga-docker-${{ github.run_number }}
  53. outputs:
  54. image_exists_arm64: ${{ steps.compute_image_exists.outputs.image_exists_arm64 }}
  55. image_exists_arm: ${{ steps.compute_image_exists.outputs.image_exists_arm }}
  56. strategy:
  57. matrix:
  58. platform: ['linux/arm64', 'linux/arm/v7']
  59. include:
  60. - platform: 'linux/arm64'
  61. platform_tag: 'arm64'
  62. file: 'joystream-node.Dockerfile'
  63. - platform: 'linux/arm/v7'
  64. platform_tag: 'arm'
  65. file: 'joystream-node-arm.Dockerfile'
  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_${{ matrix.platform_tag }}::${IMAGE_EXISTS}"
  94. echo "::set-output name=image_exists::${IMAGE_EXISTS}"
  95. - name: Configure AWS credentials
  96. uses: aws-actions/configure-aws-credentials@v1
  97. with:
  98. aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
  99. aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  100. aws-region: us-east-1
  101. if: ${{ steps.compute_image_exists.outputs.image_exists == 1 }}
  102. - name: Deploy to AWS CloudFormation
  103. uses: aws-actions/aws-cloudformation-github-deploy@v1
  104. id: deploy_stack
  105. with:
  106. name: ${{ env.STACK_NAME }}
  107. template: devops/infrastructure/single-instance-docker.yml
  108. no-fail-on-empty-changeset: '1'
  109. parameter-overrides: 'KeyName=${{ env.KEY_NAME }},EC2AMI=ami-00d1ab6b335f217cf,EC2InstanceType=t4g.xlarge'
  110. if: ${{ steps.compute_image_exists.outputs.image_exists == 1 }}
  111. - name: Run playbook
  112. uses: dawidd6/action-ansible-playbook@v2
  113. with:
  114. playbook: build-arm64-playbook.yml
  115. directory: devops/infrastructure
  116. requirements: requirements.yml
  117. key: ${{ secrets.SSH_PRIVATE_KEY }}
  118. inventory: |
  119. [all]
  120. ${{ steps.deploy_stack.outputs.PublicIp }}
  121. options: |
  122. --extra-vars "git_repo=https://github.com/${{ github.repository }} \
  123. branch_name=${{ steps.extract_branch.outputs.branch }} \
  124. docker_username=${{ secrets.DOCKERHUB_USERNAME }} \
  125. docker_password=${{ secrets.DOCKERHUB_PASSWORD }} \
  126. tag_name=${{ steps.compute_shasum.outputs.shasum }}-${{ matrix.platform_tag }} \
  127. repository=${{ env.REPOSITORY }} dockerfile=${{ matrix.file }} \
  128. stack_name=${{ env.STACK_NAME }} platform=${{ matrix.platform }}"
  129. if: ${{ steps.compute_image_exists.outputs.image_exists == 1 }}
  130. push-manifest:
  131. name: Create manifest using both the arch images
  132. needs: [push-amd64, push-arm]
  133. if: needs.push-amd64.outputs.image_exists == 1 || needs.push-arm.outputs.image_exists_arm == 1 || needs.push-arm.outputs.image_exists_arm64 == 1
  134. runs-on: ubuntu-latest
  135. env:
  136. TAG_SHASUM: ${{ needs.push-amd64.outputs.tag_shasum }}
  137. steps:
  138. - name: Login to DockerHub
  139. uses: docker/login-action@v1
  140. with:
  141. username: ${{ secrets.DOCKERHUB_USERNAME }}
  142. password: ${{ secrets.DOCKERHUB_PASSWORD }}
  143. - name: Create manifest for multi-arch images
  144. run: |
  145. # get artifacts from previous steps
  146. IMAGE=${{ env.REPOSITORY }}:${{ env.TAG_SHASUM }}
  147. echo $IMAGE
  148. docker pull $IMAGE-amd64
  149. docker pull $IMAGE-arm64
  150. docker pull $IMAGE-arm
  151. docker manifest create $IMAGE $IMAGE-amd64 $IMAGE-arm64 $IMAGE-arm
  152. docker manifest annotate $IMAGE $IMAGE-amd64 --arch amd64
  153. docker manifest annotate $IMAGE $IMAGE-arm64 --arch arm64
  154. docker manifest annotate $IMAGE $IMAGE-arm --arch arm
  155. docker manifest push $IMAGE