-
Notifications
You must be signed in to change notification settings - Fork 0
130 lines (108 loc) · 4.91 KB
/
ci.yml
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
name: CI
on:
pull_request:
branches: [ master ]
workflow_dispatch:
inputs:
stackName:
description: 'Pulumi stack name'
required: true
default: 'aws'
pulumiCommand:
description: 'Possible values are create, update or destroy'
default: 'update'
required: true
jobs:
turnstyle:
runs-on: ubuntu-latest
steps:
# Turnstyle is used to prevent multiple push jobs from
# running at the same time.
- name: Turnstyle
uses: softprops/turnstyle@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
infrastructure:
# Wait for turnstyle to allow this job to run.
needs: turnstyle
env:
ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}
ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
PULUMI_ACCESS_TOKEN: ${{ secrets.PULUMI_ACCESS_TOKEN }}
PULUMI_STACK_NAME: ${{ github.event.inputs.stackName }}
runs-on: ubuntu-latest
steps:
- name: Get current time
uses: srfrnk/current-time@master
id: current-time
with:
# Get the day of week as Sat, Sun, Mon etc.
# This action uses MomentJS style syntax.
format: ddd
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: "12"
- name: Install Pulumi CLI
uses: pulumi/[email protected]
- name: Restore npm dependencies
run: |
npm ci
echo "Restoring Azure Functions app dependencies..."
npm ci --prefix ./azure/functionapp
- name: Install Azure Functions Core Tools
run: |
# Install the Azure Functions Core Tools so that we can install
# the extension bundle for Azure Event Grid.
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-$(lsb_release -cs)-prod $(lsb_release -cs) main" > /etc/apt/sources.list.d/dotnetdev.list'
sudo apt-get update
sudo apt-get install azure-functions-core-tools
- name: Get stack outputs
id: set-stack-output
run: |
echo "::set-output name=stackOutput::$(pulumi stack output -s $PULUMI_STACK_NAME --json)"
# Always run a preview for pull_request events.
- name: Preview changes for all stacks
run: |
echo "Previewing AWS stack..."
npm run preview -- -s aws
echo "Previewing Azure stack..."
npm run buildAzFuncApp
npm run preview -- -s azure
if: github.event_name == 'pull_request' || steps.set-stack-output.outputs.stackOutput == '{}'
# Always run an update for a push build, IFF the stack already has resources.
- name: Update infrastructure
run: npm run ${PULUMI_STACK_NAME} -- --yes
if: github.event_name == 'workflow_dispatch' && steps.set-stack-output.outputs.stackOutput != '{}' && github.event.inputs.pulumiCommand == 'update'
# If this is a scheduled execution, and it is a Saturday, run the update to create the resources.
- name: Create infrastructure
run: npm run ${PULUMI_STACK_NAME} -- --yes
if: github.event_name == 'workflow_dispatch' && github.event.inputs.pulumiCommand == 'create'
# If this is a scheduled execution, and it is a Monday, destroy the resources.
- name: Destroy AWS infrastructure
if: env.PULUMI_STACK_NAME == 'aws' && github.event_name == 'workflow_dispatch' && github.event.inputs.pulumiCommand == 'destroy'
run: |
# First terminate the EC2 instance that fulfilled the Spot Instance Request (SIR).
# To do that, we should first query for the instance ID from the SIR.
SIR_ID=$(pulumi stack output spotRequestId -s "${PULUMI_STACK_NAME}")
echo "Spot Instance Request ID is ${SIR_ID}"
REGION=$(pulumi config get aws:region -s ${PULUMI_STACK_NAME})
aws configure set region ${REGION}
DESCRIBE_SIR=$(aws ec2 describe-spot-instance-requests --spot-instance-request-ids "${SIR_ID}")
INSTANCE_ID=$(echo ${DESCRIBE_SIR} | jq '.SpotInstanceRequests[0].InstanceId' -c -r)
# Finally terminate the instance.
echo "Terminating the instance ${INSTANCE_ID}"
aws ec2 terminate-instances --instance-ids "${INSTANCE_ID}"
echo "Sleeping for 30s..."
sleep 30
npm run destroy -- --yes -s ${PULUMI_STACK_NAME}
- name: Destroy Azure infrastructure
if: env.PULUMI_STACK_NAME == 'azure' && github.event_name == 'workflow_dispatch' && github.event.inputs.pulumiCommand == 'destroy'
run: npm run destroy -- --yes -s ${PULUMI_STACK_NAME}