Skip to content

Commit

Permalink
autodeploy
Browse files Browse the repository at this point in the history
  • Loading branch information
TinyKitten committed Mar 30, 2021
1 parent 569235d commit f540bcb
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
39 changes: 39 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: deploy
on:
push:
branches:
- "**"
# on:
# push:
# branches:
# - master
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Utilities
run: sudo apt-get install -y jq
- name: Install AWS Client and LightsailControl Plugin
run: |
aws --version
curl "https://s3.us-west-2.amazonaws.com/lightsailctl/latest/linux-amd64/lightsailctl" -o "lightsailctl"
sudo mv "lightsailctl" "/usr/local/bin/lightsailctl"
sudo chmod +x /usr/local/bin/lightsailctl
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-1
- name: Build Docker Image
run: |
docker build -t stationapi:latest .
- name: Push and Deploy
env:
SERVICE_NAME: ${{ secrets.SERVICE_NAME }}
MYSQL_DATABASE: ${{ secrets.MYSQL_DATABASE }}
MYSQL_USER: ${{ secrets.MYSQL_USER }}
MYSQL_PASSWORD: ${{ secrets.MYSQL_PASSWORD }}
MYSQL_HOST: ${{ secrets.MYSQL_HOST }}
run: sh ./scripts/generate-aws-container-json.sh
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ lerna-debug.log*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/extensions.json

# Auto deployment
aws-container.json
28 changes: 28 additions & 0 deletions scripts/aws-container-template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"serviceName": $SERVICE_NAME,
"containers": {
"api": {
"image": $IMAGE,
"command": [["sh", "./scripts/start.prod.sh"]],
"environment": {
"MYSQL_DATABASE": $MYSQL_DATABASE,
"MYSQL_PASSWORD": $MYSQL_PASSWORD,
"MYSQL_USER": $MYSQL_USER,
"MYSQL_HOST": $MYSQL_HOST
},
"ports": { "3000": "HTTP" }
}
},
"publicEndpoint": {
"containerName": "api",
"containerPort": 3000,
"healthCheck": {
"healthyThreshold": 2,
"unhealthyThreshold": 2,
"timeoutSeconds": 3,
"intervalSeconds": 3,
"path": "/graphql",
"successCodes": "200-499"
}
}
}
22 changes: 22 additions & 0 deletions scripts/generate-aws-container-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

JSON_TEMPLATE=$(cat ./scripts/aws-container-template.json)

SERVICE_NAME=stationapi
aws lightsail push-container-image \
--region ap-northeast-1 \
--service-name ${SERVICE_NAME} \
--label api \
--image stationapi:latest
IMAGE=$(aws lightsail get-container-images --service-name ${SERVICE_NAME} | jq --raw-output ".containerImages[0].image")
JSON_STRING=$(jq -n \
--arg IMAGE "$IMAGE" \
--arg SERVICE_NAME "$SERVICE_NAME" \
--arg MYSQL_DATABASE "$MYSQL_DATABASE" \
--arg MYSQL_USER "$MYSQL_USER" \
--arg MYSQL_PASSWORD "$MYSQL_PASSWORD" \
--arg MYSQL_HOST "$MYSQL_HOST" \
"$JSON_TEMPLATE"
)
echo $JSON_STRING > aws-container.json
aws lightsail create-container-service-deployment --service-name ${SERVICE_NAME} --cli-input-json file://$(pwd)/aws-container.json

0 comments on commit f540bcb

Please sign in to comment.