create-ami.yml 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. name: Create AWS AMI
  2. on:
  3. workflow_dispatch:
  4. jobs:
  5. build:
  6. name: Build the code and run setup
  7. runs-on: ubuntu-latest
  8. env:
  9. STACK_NAME: joystream-github-action-${{ github.run_number }}
  10. KEY_NAME: joystream-github-action-key
  11. steps:
  12. - name: Extract branch name
  13. shell: bash
  14. run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
  15. id: extract_branch
  16. - name: Set AMI Name environment variable
  17. shell: bash
  18. run: echo "ami_name=joystream-${{ steps.extract_branch.outputs.branch }}-${{ github.run_number }}" >> $GITHUB_ENV
  19. id: ami_name
  20. - name: Checkout
  21. uses: actions/checkout@v2
  22. - name: Configure AWS credentials
  23. uses: aws-actions/configure-aws-credentials@v1
  24. with:
  25. aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
  26. aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  27. aws-region: us-east-1
  28. - name: Deploy to AWS CloudFormation
  29. uses: aws-actions/aws-cloudformation-github-deploy@v1
  30. id: deploy_stack
  31. with:
  32. name: ${{ env.STACK_NAME }}
  33. template: devops/infrastructure/cloudformation/single-instance.yml
  34. no-fail-on-empty-changeset: '1'
  35. parameter-overrides: 'KeyName=${{ env.KEY_NAME }}'
  36. - name: Install Ansible dependencies
  37. run: pipx inject ansible-core boto3 botocore
  38. - name: Run playbook
  39. uses: dawidd6/action-ansible-playbook@v2
  40. with:
  41. playbook: github-action-playbook.yml
  42. directory: devops/infrastructure
  43. requirements: requirements.yml
  44. key: ${{ secrets.SSH_PRIVATE_KEY }}
  45. inventory: |
  46. [all]
  47. ${{ steps.deploy_stack.outputs.PublicIp }}
  48. options: |
  49. --extra-vars "git_repo=https://github.com/${{ github.repository }} \
  50. branch_name=${{ steps.extract_branch.outputs.branch }} instance_id=${{ steps.deploy_stack.outputs.InstanceId }}
  51. ami_name=${{ env.ami_name }}"
  52. - name: Delete CloudFormation Stack
  53. if: always()
  54. continue-on-error: true
  55. run: |
  56. echo "Deleting ${{ env.STACK_NAME }} stack"
  57. aws cloudformation delete-stack --stack-name ${{ env.STACK_NAME }}
  58. echo "Waiting for ${{ env.STACK_NAME }} to be deleted..."
  59. aws cloudformation wait stack-delete-complete --stack-name ${{ env.STACK_NAME }}