Skip to content

Commit 7ced358

Browse files
committed
WIP CI/CD
1 parent b3605c0 commit 7ced358

File tree

5 files changed

+185
-2
lines changed

5 files changed

+185
-2
lines changed

.github/workflows/prod.yml

+51-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on:
1111
jobs:
1212
build-and-deploy-front:
1313
name: Build and Deploy frontend
14-
runs-on: self-hosted
14+
runs-on: ubuntu-latest
1515
steps:
1616
- name: Checkout code
1717
uses: actions/checkout@v2
@@ -24,3 +24,53 @@ jobs:
2424
repository: larryeitel/fastapi-vue3-docker-workflow
2525
dockerfile: frontend/Dockerfile
2626
tags: latest
27+
28+
#Push to Digital Ocean private Registry
29+
pushToDORegistry:
30+
# Ensure test job passes before pushing image.
31+
# needs: test
32+
runs-on: ubuntu-latest
33+
if: github.event_name == 'push'
34+
steps:
35+
- uses: actions/checkout@v2
36+
- name: Build image
37+
run: docker build . --file Dockerfile --tag $IMAGE_NAME
38+
39+
- name: Install doctl # install the doctl on the runner
40+
uses: digitalocean/action-doctl@v2
41+
with:
42+
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
43+
44+
- name: push image to digitalocean
45+
run: |
46+
doctl registry login
47+
docker tag $IMAGE_NAME registry.digitalocean.com/orgtec/$IMAGE_NAME
48+
docker push registry.digitalocean.com/orgtec/$IMAGE_NAME
49+
50+
deploy:
51+
runs-on: ubuntu-latest
52+
if: github.event_name == 'push'
53+
needs: build_and_push
54+
55+
steps:
56+
- name: Deploy to Digital Ocean droplet via SSH action
57+
uses: appleboy/[email protected]
58+
with:
59+
HOST: ${{ secrets.HOST }}
60+
USERNAME: ${{ secrets.USERNAME }}
61+
KEY: ${{ secrets.SSHKEY }}
62+
envs: IMAGE_NAME,REGISTRY,GITHUB_SHA,COMMAND
63+
script: |
64+
# Stop running container
65+
docker stop $(echo $IMAGE_NAME)
66+
67+
# Remove old container
68+
docker rm $(echo $IMAGE_NAME)
69+
70+
# Run a new container from a new image
71+
docker run -d \
72+
--restart always \
73+
--env-file .env \
74+
-p 8000:8000 \
75+
--name $(echo $IMAGE_NAME) \
76+
$(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) $(echo $COMMAND)

.github/workflows/sample._yml

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
env:
2+
REGISTRY: "registry.digitalocean.com/your_registry"
3+
IMAGE_NAME: "image_name"
4+
5+
COMMAND: "python main.py"
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout master
13+
uses: actions/checkout@v2
14+
15+
- name: Set up Python 3.8
16+
uses: actions/setup-python@v2
17+
with:
18+
python-version: 3.8
19+
20+
- name: Install dependencies with pipenv
21+
run: |
22+
python -m pip install --upgrade pip
23+
pip install pipenv
24+
if [ -f Pipfile ]; then pipenv install --deploy --dev; fi
25+
26+
- name: Lint with flake8
27+
run: |
28+
# stop the build if there are Python syntax errors or undefined names
29+
pipenv run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
30+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
31+
pipenv run flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
32+
33+
- name: Test with pytest
34+
run: |
35+
pipenv run pytest .
36+
build_and_push:
37+
runs-on: ubuntu-latest
38+
if: github.event_name == 'push'
39+
needs: test
40+
41+
steps:
42+
- name: Checkout master
43+
uses: actions/checkout@v2
44+
45+
- name: Build container image
46+
run: docker build -t $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) .
47+
48+
- name: Install doctl
49+
uses: digitalocean/action-doctl@v2
50+
with:
51+
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
52+
53+
- name: Log in to DigitalOcean Container Registry with short-lived credentials
54+
run: doctl registry login --expiry-seconds 600
55+
56+
- name: Push image to DigitalOcean Container Registry
57+
run: docker push $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)
58+
59+
deploy:
60+
runs-on: ubuntu-latest
61+
if: github.event_name == 'push'
62+
needs: build_and_push
63+
64+
steps:
65+
- name: Deploy to Digital Ocean droplet via SSH action
66+
uses: appleboy/[email protected]
67+
with:
68+
HOST: ${{ secrets.HOST }}
69+
USERNAME: ${{ secrets.USERNAME }}
70+
KEY: ${{ secrets.SSHKEY }}
71+
envs: IMAGE_NAME,REGISTRY,GITHUB_SHA,COMMAND
72+
script: |
73+
# Stop running container
74+
docker stop $(echo $IMAGE_NAME)
75+
76+
# Remove old container
77+
docker rm $(echo $IMAGE_NAME)
78+
79+
# Run a new container from a new image
80+
docker run -d \
81+
--restart always \
82+
--env-file .env \
83+
-p 8000:8000 \
84+
--name $(echo $IMAGE_NAME) \
85+
$(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) $(echo $COMMAND)

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Stubbing out and documenting FastAPI, VueJS 3 and Docker workflow.
33

44
![Build Containers for Prod & Push to Dockerhub](https://github.com/LarryEitel/fastapi-vue3-docker-workflow/workflows/Build%20Containers%20for%20Prod%20&%20Push%20to%20Dockerhub/badge.svg)
55

6+
## Must Reads
7+
- [x] [Deploying Self-Hosted Github Actions Runners with Docker](https://testdriven.io/blog/github-actions-docker/)
68

79
## Punchlist
810
- [x] Create sample vue frontend

digitalocean.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Install Docker Machine
2+
```
3+
base=https://github.com/docker/machine/releases/download/v0.16.0 &&
4+
mkdir -p "$HOME/bin" &&
5+
curl -L $base/docker-machine-Windows-x86_64.exe > "$HOME/bin/docker-machine.exe" &&
6+
chmod +x "$HOME/bin/docker-machine.exe"
7+
```
8+
9+
10+
```
11+
docker-machine create \
12+
--digitalocean-region "nyc1" \
13+
--driver digitalocean \
14+
--digitalocean-size "2gb" \
15+
--digitalocean-access-token $DIGITAL_OCEAN_ACCESS_TOKEN \
16+
runner-node;
17+
```
18+
19+
Install [doctl](https://www.digitalocean.com/docs/apis-clis/doctl/how-to/install/)
20+
```
21+
Invoke-WebRequest https://github.com/digitalocean/doctl/releases/download/v1.54.0/doctl-1.54.0-windows-amd64.zip -OutFile ~\doctl-1.54.0-windows-amd64.zip
22+
```
23+
Next, extract the binary by running:
24+
```
25+
Expand-Archive -Path ~\doctl-1.54.0-windows-amd64.zip
26+
```
27+
```
28+
Move-Item -Path doctl-1.54.0-windows-amd64/doctl.exe -Destination C:\\Users\\Larry\\Dropbox\\utils
29+
```
30+
Use the API token to grant doctl access to your DigitalOcean account. Pass in the token string when prompted by doctl auth init, and give this authentication context a name.
31+
```
32+
doctl auth init --context docker
33+
```
34+
35+
Authentication contexts let you switch between multiple authenticated accounts. You can repeat steps 2 and 3 to add other DigitalOcean accounts, then list and switch between authentication contexts:
36+
37+
```bigquery
38+
doctl auth list
39+
doctl auth switch --context <NAME>
40+
```
41+
42+
```
43+
docker tag frontend registry.digitalocean.com/orgtec/frontend
44+
doctl registry login
45+
docker push registry.digitalocean.com/orgtec/frontend
46+
```

frontend/src/components/HelloWorld.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
>vue-cli documentation</a
99
>.
1010
</p>
11-
<h3>Installed CLI Plugins!</h3>
11+
<h3>Installed CLI Plugins!!!!</h3>
1212
<ul>
1313
<li>
1414
<a

0 commit comments

Comments
 (0)