-
Notifications
You must be signed in to change notification settings - Fork 15
147 lines (132 loc) · 6.24 KB
/
Copy pathsst_workflow.yml
File metadata and controls
147 lines (132 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
---
name: 🚀 Shared Workflow - SST Deploy
on:
workflow_call:
inputs:
app-env:
description: 'Application environment'
required: true
type: string
preview:
description: 'Create or destroy preview env'
required: false
type: string
default: false
working-directory:
description: 'Working directory in repo'
required: false
type: string
default: ./
stack-name:
description: 'Stack name'
required: false
default: ""
type: string
yarn-cache:
description: 'Cache required or not for yarn install'
type: string
default: false
deploy:
description: 'Default deploy otherwise run diff command to detect changes in stacks'
type: string
default: true
self-hosted:
description: 'Deploy stack with github self hosted runner or not'
type: string
default: true
secrets:
token:
description: 'GitHub Token'
required: false
env-vars:
description: 'Environment-variables to store in .env file'
required: false
build-role:
description: 'Assume role arn'
required: true
jobs:
setup:
runs-on: ubuntu-latest
outputs:
runner: ${{ steps.step1.outputs.runner }}
steps:
- name: 🕵️ Check branch
id: step1
run: |
if [ ${{ inputs.self-hosted }} == 'true' ]; then
echo "runner=${{ inputs.app-env }}" >> "$GITHUB_OUTPUT"
else
echo "runner=ubuntu-latest" >> "$GITHUB_OUTPUT"
fi
sst-deploy:
needs: [setup]
runs-on: ${{ needs.setup.outputs.runner }}
environment:
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)) || '' }}
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 }}
defaults:
run:
working-directory: ${{ inputs.working-directory }}
name: 🚀 Run sst-deploy
steps:
- name: 📦 Checkout Repository
uses: actions/checkout@v7
- name: 📝 Update environment variable in .env file
run: |
if [ -n "${{ secrets.env-vars }}" ]; then
echo -e "${{ secrets.env-vars }}" > ./.env
fi
- name: 🔑 Configure AWS Creds via role
uses: aws-actions/configure-aws-credentials@v6
with:
aws-region: us-west-2
role-to-assume: ${{ secrets.build-role }}
role-duration-seconds: 900
role-skip-session-tagging: true
- name: 📦 Install yarn
run: sudo npm install -g yarn
- name: 📥 Install dependencies
if: ${{ inputs.yarn-cache != 'true' }}
run: yarn install --frozen-lockfile
- name: 📦 Install dependencies with yarn cache
if: ${{ inputs.yarn-cache == 'true' }}
uses: ./.github/actions/yarn-nm-install
- name: 🌿 Set branch name
run: |
BRANCH_NAME=$(echo "${{ github.head_ref }}" | cut -d'|' -f2)
echo "BRANCH_NAME=${BRANCH_NAME}"
SLUG_BRANCH_NAME=$(echo "${BRANCH_NAME}" | sed 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)
echo "SLUG_BRANCH_NAME=${SLUG_BRANCH_NAME}"
echo "GITHUB_HEAD_REF_SLUG=${SLUG_BRANCH_NAME}" >> $GITHUB_ENV
- name: 🔍 Check diffrence in deployed and local stacks
if: ${{ inputs.deploy != 'true' }}
run: yarn sst diff --stage ${{ inputs.app-env }}
- name: 🚀 Deploy and get API endpoint
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'))) }}
run: |
if [[ ${{ inputs.preview }} == true ]]; then
if [[ -n "${{ inputs.stack-name }}" ]]; then
yarn sst deploy --stage pr-${{ github.event.number }}-${{ env.GITHUB_HEAD_REF_SLUG }} ${{ inputs.stack-name }} | tee deploy-output.log
else
yarn sst deploy --stage pr-${{ github.event.number }}-${{ env.GITHUB_HEAD_REF_SLUG }} | tee deploy-output.log
fi
else
if [[ -n "${{ inputs.stack-name }}" ]]; then
yarn sst deploy --stage ${{ inputs.app-env }} ${{ inputs.stack-name }} | tee deploy-output.log
else
yarn sst deploy --stage ${{ inputs.app-env }} | tee deploy-output.log
fi
fi
api_endpoint=$(cat deploy-output.log | egrep "ApiEndpoint|SiteUrl" | awk '{print $2}')
echo "API endpoint: $api_endpoint"
echo "API_ENDPOINT_URL=$api_endpoint" >> $GITHUB_ENV
- name: 🗑️ Destroy preview env
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' ) }}
run: yarn sst remove --stage pr-${{ github.event.number }}-${{ env.GITHUB_HEAD_REF_SLUG }}
- name: 🧹 Cleanup preview env deployment
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' ) }}
uses: strumwolf/delete-deployment-environment@v4.0.0
with:
token: ${{ secrets.token }}
environment: ${{ github.head_ref }}
...