|
1 | | -name: Shared workflow- SST APP |
| 1 | +name: Shared Workflow - SST Deploy |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | workflow_call: |
5 | 5 | inputs: |
6 | 6 | app-env: |
7 | | - description: 'Application environment' |
| 7 | + description: 'Application environment' |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + preview: |
| 11 | + description: 'Create or destroy preview env' |
8 | 12 | required: false |
9 | 13 | type: string |
| 14 | + default: false |
10 | 15 | working-directory: |
11 | | - description: 'Working directory in the repository' |
12 | | - required: true |
| 16 | + description: 'Working directory in repo' |
| 17 | + required: false |
| 18 | + type: string |
| 19 | + default: ./ |
| 20 | + stack-name: |
| 21 | + description: 'Stack name' |
| 22 | + required: false |
| 23 | + default: "" |
13 | 24 | type: string |
| 25 | + yarn-cache: |
| 26 | + description: 'Cache required or not for yarn install' |
| 27 | + type: string |
| 28 | + default: false |
| 29 | + deploy: |
| 30 | + description: 'Default deploy otherwise run diff command to detect changes in stacks' |
| 31 | + type: string |
| 32 | + default: true |
| 33 | + self-hosted: |
| 34 | + description: 'Deploy stack with github self hosted runner or not' |
| 35 | + type: string |
| 36 | + default: true |
14 | 37 |
|
15 | 38 | secrets: |
16 | | - aws-access-key-id: |
17 | | - description: 'AWS Access Key ID' |
18 | | - required: true |
19 | | - aws-secret-access-key: |
20 | | - description: 'AWS Secret Access Key' |
| 39 | + token: |
| 40 | + description: 'GitHub Token' |
| 41 | + required: false |
| 42 | + env-vars: |
| 43 | + description: 'Environment-variables to store in .env file' |
| 44 | + required: false |
| 45 | + build-role: |
| 46 | + description: 'Assume role arn' |
21 | 47 | required: true |
22 | 48 |
|
| 49 | + |
23 | 50 | jobs: |
24 | | - deploy: |
25 | | - runs-on: ubuntu-20.04 |
26 | | - environment: |
27 | | - name: ${{ github.head_ref }} |
28 | | - url: ${{ env.API_ENDPOINT_URL }} |
| 51 | + setup: |
| 52 | + runs-on: ubuntu-latest |
| 53 | + outputs: |
| 54 | + runner: ${{ steps.step1.outputs.runner }} |
| 55 | + steps: |
| 56 | + - name: Check branch |
| 57 | + id: step1 |
| 58 | + run: | |
| 59 | + if [ ${{ inputs.self-hosted }} == 'true' ]; then |
| 60 | + echo "runner=${{ inputs.app-env }}" >> "$GITHUB_OUTPUT" |
| 61 | + else |
| 62 | + echo "runner=ubuntu-latest" >> "$GITHUB_OUTPUT" |
| 63 | + fi |
| 64 | + sst-deploy: |
| 65 | + needs: [setup] |
| 66 | + runs-on: ${{ needs.setup.outputs.runner }} |
| 67 | + environment: |
| 68 | + name: ${{ (((github.event.action == 'opened' || github.event.action == 'synchronize') && inputs.preview == 'true') || (github.event.pull_request.merged == true && inputs.preview == 'false' && inputs.app-env == 'staging') || (inputs.app-env == 'production' && startsWith(github.ref, 'refs/tags/v'))) && ((inputs.preview == 'true' && (inputs.stack-name != '' && github.head_ref-inputs.stack-name || github.head_ref) || inputs.app-env)) || '' }} |
| 69 | + url: ${{ ((github.event.action == 'opened' && inputs.preview == 'true') || (github.event.action == 'synchronize' && inputs.preview == 'true') || (github.event.pull_request.merged == true && inputs.preview == 'false' && inputs.app-env == 'staging') || (inputs.app-env == 'production' && startsWith(github.ref, 'refs/tags/v'))) && env.API_ENDPOINT_URL }} |
29 | 70 | defaults: |
30 | 71 | run: |
31 | 72 | working-directory: ${{ inputs.working-directory }} |
32 | 73 |
|
33 | | - name: Deploy SST APP |
| 74 | + name: Run sst-deploy |
34 | 75 | steps: |
35 | 76 | - name: Checkout git repo |
36 | 77 | uses: actions/checkout@v3 |
37 | 78 |
|
38 | | - - name: Configure AWS Credentials |
39 | | - uses: aws-actions/configure-aws-credentials@v2 |
| 79 | + - name: update environment variable in .env file |
| 80 | + run: | |
| 81 | + if [ -n "${{ secrets.env-vars }}" ]; then |
| 82 | + echo -e "${{ secrets.env-vars }}" > ./.env |
| 83 | + fi |
| 84 | +
|
| 85 | + - name: Configure AWS Creds via role |
| 86 | + uses: aws-actions/configure-aws-credentials@v1-node16 |
40 | 87 | with: |
41 | | - aws-access-key-id: ${{ secrets.aws-access-key-id }} |
42 | | - aws-secret-access-key: ${{ secrets.aws-secret-access-key }} |
43 | | - aws-region: us-east-2 |
| 88 | + aws-region: us-west-2 |
| 89 | + role-to-assume: ${{ secrets.build-role }} |
| 90 | + role-duration-seconds: 900 |
| 91 | + role-skip-session-tagging: true |
| 92 | + |
| 93 | + - name: Install yarn |
| 94 | + run: sudo npm install -g yarn |
44 | 95 |
|
45 | | - - name: Install dependencies (yarn install) |
46 | | - run: yarn install |
47 | | - |
48 | | - - name: Extract branch name |
| 96 | + - name: Install dependencies |
| 97 | + if: ${{ inputs.yarn-cache != 'true' }} |
| 98 | + run: yarn install --frozen-lockfile |
| 99 | + |
| 100 | + - name: Install dependencies with yarn cache |
| 101 | + if: ${{ inputs.yarn-cache == 'true' }} |
| 102 | + uses: ./.github/actions/yarn-nm-install |
| 103 | + |
| 104 | + - name: Set branch name |
49 | 105 | run: | |
50 | | - BRANCH_NAME=$(echo "${{ github.head_ref }}" | cut -d'/' -f3) |
| 106 | + BRANCH_NAME=$(echo "${{ github.head_ref }}" | cut -d'|' -f2) |
51 | 107 | echo "BRANCH_NAME=${BRANCH_NAME}" |
52 | 108 | SLUG_BRANCH_NAME=$(echo "${BRANCH_NAME}" | sed 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z) |
53 | 109 | echo "SLUG_BRANCH_NAME=${SLUG_BRANCH_NAME}" |
54 | 110 | echo "GITHUB_HEAD_REF_SLUG=${SLUG_BRANCH_NAME}" >> $GITHUB_ENV |
55 | 111 | |
| 112 | + - name: check diffrence in deployed and local stacks |
| 113 | + if: ${{ inputs.deploy != 'true' }} |
| 114 | + run: yarn sst diff --stage ${{ inputs.app-env }} |
| 115 | + |
56 | 116 | - name: Deploy and get API endpoint |
57 | | - if: ${{ (github.event.action == 'opened' || github.event.action == 'synchronize' && inputs.app-env == 'preview') || ( github.event.pull_request.merged == true && (inputs.app-env == 'prod' || inputs.app-env == 'stage')) }} |
| 117 | + if: ${{ inputs.deploy == 'true' && ((github.event.action == 'opened' && inputs.preview == 'true') || (github.event.action == 'synchronize' && inputs.preview == 'true') || (github.event.pull_request.merged == true && inputs.preview == 'false' && inputs.app-env == 'staging') || (inputs.app-env == 'production' && startsWith(github.ref, 'refs/tags/v'))) }} |
58 | 118 | run: | |
59 | | - api_endpoint=$(yarn sst deploy --stage pr-${{ github.event.number }}-${{ env.GITHUB_HEAD_REF_SLUG }} | egrep "ApiEndpoint|SiteUrl" | awk '{print $2}') |
| 119 | + if [[ ${{ inputs.preview }} == true ]]; then |
| 120 | + if [[ -n "${{ inputs.stack-name }}" ]]; then |
| 121 | + yarn sst deploy --stage pr-${{ github.event.number }}-${{ env.GITHUB_HEAD_REF_SLUG }} ${{ inputs.stack-name }} | tee deploy-output.log |
| 122 | + else |
| 123 | + yarn sst deploy --stage pr-${{ github.event.number }}-${{ env.GITHUB_HEAD_REF_SLUG }} | tee deploy-output.log |
| 124 | + fi |
| 125 | + else |
| 126 | + if [[ -n "${{ inputs.stack-name }}" ]]; then |
| 127 | + yarn sst deploy --stage ${{ inputs.app-env }} ${{ inputs.stack-name }} | tee deploy-output.log |
| 128 | + else |
| 129 | + yarn sst deploy --stage ${{ inputs.app-env }} | tee deploy-output.log |
| 130 | + fi |
| 131 | + fi |
| 132 | + api_endpoint=$(cat deploy-output.log | egrep "ApiEndpoint|SiteUrl" | awk '{print $2}') |
60 | 133 | echo "API endpoint: $api_endpoint" |
61 | 134 | echo "API_ENDPOINT_URL=$api_endpoint" >> $GITHUB_ENV |
62 | | - |
63 | | - - name: Destroy SST App for Preview app environment |
64 | | - if: ${{ ( github.event.action == 'labeled' && github.event.label.name == 'destroy' && inputs.app-env == 'preview' ) || (github.event.action == 'closed' && inputs.app-env == 'preview' || github.event.pull_request.merged == true && inputs.app-env == 'preview') }} |
| 135 | +
|
| 136 | + - name: Destroy preview env |
| 137 | + if: ${{ ( github.event.action == 'labeled' && github.event.label.name == 'destroy' && inputs.preview == 'true' ) || (github.event.action == 'closed' && inputs.preview == 'true' || github.event.pull_request.merged == true && inputs.preview == 'true' ) }} |
65 | 138 | run: yarn sst remove --stage pr-${{ github.event.number }}-${{ env.GITHUB_HEAD_REF_SLUG }} |
| 139 | + |
| 140 | + - name: Cleanup preview env deployment |
| 141 | + if: ${{ ( github.event.action == 'labeled' && github.event.label.name == 'destroy' && inputs.preview == 'true' ) || (github.event.action == 'closed' && inputs.preview == 'true' || github.event.pull_request.merged == true && inputs.preview == 'true' ) }} |
| 142 | + |
| 143 | + with: |
| 144 | + token: ${{ secrets.token }} |
| 145 | + environment: ${{ github.head_ref }} |
0 commit comments