-
Notifications
You must be signed in to change notification settings - Fork 3
131 lines (123 loc) · 6.95 KB
/
prod-aws.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
131
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# Source Partly from: https://aws.amazon.com/getting-started/guides/deploy-webapp-lightsail/module-four/
name: Deploy Docker Image to Prod AWS Lightsail Container Service
on:
push:
branches:
- main
workflow_dispatch:
jobs:
deploy-to-aws:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: 🔎 Check if files besides docs have changed
id: changed-files
uses: tj-actions/changed-files@v35
with:
files_ignore: documentation
- name: Upgrade AWS CLI version and setup lightsailctl
if: steps.changed-files.outputs.any_changed == 'true'
run: |
aws --version
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install --bin-dir /usr/local/bin --install-dir /usr/local/aws-cli --update
which aws
aws --version
sudo curl "https://s3.us-west-2.amazonaws.com/lightsailctl/latest/linux-amd64/lightsailctl" -o "/usr/local/bin/lightsailctl"
sudo chmod +x /usr/local/bin/lightsailctl
- name: Configure AWS Credentials
if: steps.changed-files.outputs.any_changed == 'true'
uses: aws-actions/configure-aws-credentials@v1-node16
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Set up Docker Buildx
if: steps.changed-files.outputs.any_changed == 'true'
uses: docker/setup-buildx-action@v2
- name: Login to DockerHub
if: steps.changed-files.outputs.any_changed == 'true'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push 1-data-loading-service
if: steps.changed-files.outputs.any_changed == 'true'
id: docker_build_data_loading_service
uses: docker/build-push-action@v2
with:
push: true
tags: lacmta/metro-api-v2:data-loading-service
context: ./data-loading-service/
build-args: |
BUILDKIT_CONTEXT_KEEP_GIT_DIR=true
- name: Build and push 2-fastapi
if: steps.changed-files.outputs.any_changed == 'true'
id: docker_build_fastapi
uses: docker/build-push-action@v2
with:
push: true
tags: lacmta/metro-api-v2:fastapi
context: ./fastapi/
build-args: |
BUILDKIT_CONTEXT_KEEP_GIT_DIR=true
- name: Push Updated data loading service Docker Image on Lightsail Prod
if: steps.changed-files.outputs.any_changed == 'true'
run: >
aws lightsail create-container-service-deployment --service-name data-loading-service --containers '{"data-loading-service": {"image":"lacmta/metro-api-v2:data-loading-service", "environment": {"FTP_SERVER": "${{secrets.FTP_SERVER}}", "FTP_USERNAME": "${{secrets.FTP_USERNAME}}", "FTP_PASS": "${{secrets.FTP_PASS}}", "SWIFTLY_AUTH_KEY_BUS": "${{secrets.SWIFTLY_AUTH_KEY_BUS}}", "SWIFTLY_AUTH_KEY_RAIL": "${{secrets.SWIFTLY_AUTH_KEY_RAIL}}", "AWS_ACCESS_KEY_ID": "${{secrets.AWS_ACCESS_KEY_ID}}", "ACCESS_SECRET_KEY": "${{secrets.ACCESS_SECRET_KEY}}", "SWIFTLY_AUTH_KEY": "${{secrets.SWIFTLY_AUTH_KEY}}", "API_DB_URI": "${{secrets.API_DB_URI}}", "HASH_KEY": "${{secrets.HASH_KEY}}", "HASHING_ALGORITHM": "${{secrets.HASHING_ALGORITHM}}", "LOGZIO_TOKEN": "${{secrets.LOGZIO_TOKEN}}", "LOGZIO_URL": "${{secrets.LOGZIO_URL}}", "RUNNING_ENV": "prod"}}}'
- name: Push Updated Docker Image on Lightsail Prod
if: steps.changed-files.outputs.any_changed == 'true'
run: |
MAX_RETRIES=5
RETRY_DELAY=5
count=0
until aws lightsail create-container-service-deployment --service-name metro-api-v2 --containers '{"redis": {"image": "redis:latest", "environment": {}, "ports": {"6379": "TCP"}}, "fastapi": {"image":"lacmta/metro-api-v2:fastapi", "environment": {"FTP_SERVER": "${{secrets.FTP_SERVER}}", "FTP_USERNAME": "${{secrets.FTP_USERNAME}}", "FTP_PASS": "${{secrets.FTP_PASS}}", "SWIFTLY_AUTH_KEY_BUS": "${{secrets.SWIFTLY_AUTH_KEY_BUS}}", "SWIFTLY_AUTH_KEY_RAIL": "${{secrets.SWIFTLY_AUTH_KEY_RAIL}}", "AWS_ACCESS_KEY_ID": "${{secrets.AWS_ACCESS_KEY_ID}}", "ACCESS_SECRET_KEY": "${{secrets.ACCESS_SECRET_KEY}}", "SWIFTLY_AUTH_KEY": "${{secrets.SWIFTLY_AUTH_KEY}}", "API_DB_URI": "${{secrets.API_DB_URI}}", "HASH_KEY": "${{secrets.HASH_KEY}}", "HASHING_ALGORITHM": "${{secrets.HASHING_ALGORITHM}}", "LOGZIO_TOKEN": "${{secrets.LOGZIO_TOKEN}}", "LOGZIO_URL": "https://listener.logz.io:8071", "RUNNING_ENV": "prod", "REDIS_URL": "redis://metro-api-v2.service.local:6379"}, "ports": {"80": "HTTP"}}}' --public-endpoint '{"containerName": "fastapi","containerPort": 80,"healthCheck":{ "healthyThreshold": 2,"unhealthyThreshold": 2,"timeoutSeconds": 2,"intervalSeconds": 5,"path": "/","successCodes": "200-499"}}'
do
if [[ "$count" -lt "$MAX_RETRIES" ]]; then
count=$((count+1))
echo "Attempt $count of $MAX_RETRIES. Retrying after $RETRY_DELAY seconds..."
sleep $RETRY_DELAY
else
echo "All attempts failed. Exiting..."
exit 1
fi
done
deploy-documentation:
runs-on: ubuntu-latest
name: Deploy Documentation to GitHub Pages
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
cache: yarn
cache-dependency-path: "./documentation/package.json"
- name: 🔎 Check for change in docs folder
id: changed-documentation
uses: tj-actions/changed-files@v35
with:
files: documentation
- name: Install dependencies
if: steps.changed-documentation.outputs.any_changed == 'true'
working-directory: ./documentation
run: yarn install
- name: Build website
if: steps.changed-documentation.outputs.any_changed == 'true'
working-directory: ./documentation
run: yarn build
- name: Deploy Documentation to GitHub Pages
if: steps.changed-documentation.outputs.any_changed == 'true'
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.METRO_GITHUB_TOKEN }}
publish_dir: ./documentation/build
publish_branch: gh-pages
user_name: github-actions[bot]
user_email: 41898282+github-actions[bot]@users.noreply.github.com