Skip to content

Commit afa54ef

Browse files
authored
Merge pull request corndeladmin#1 from corndeladmin/deploy-with-docker
Deploy with docker
2 parents bf1a27b + 8fff2ca commit afa54ef

File tree

6 files changed

+94
-10
lines changed

6 files changed

+94
-10
lines changed

.dockerignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
__pycache__/
33
.env
44
.azure
5-
.venv
5+
.venv
6+
7+
Dockerfile
8+
.github
9+
.gitlab-ci.yml

.github/workflows/deploy.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Deploy
2+
on:
3+
push
4+
5+
jobs:
6+
my-job:
7+
runs-on: ubuntu-latest
8+
env:
9+
# REPLACE THIS
10+
DOCKERHUB_USERNAME: corndeldevopscourse
11+
# SET THESE IN GITHUB
12+
DOCKERHUB_TOKEN: '${{ secrets.DOCKERHUB_TOKEN }}'
13+
AZURE_WEBHOOK: '${{ secrets.AZURE_WEBHOOK }}'
14+
# DECIDE WHETHER YOU WANT A BRANCH CHECK - if so remove the other "if"
15+
# if: github.ref == 'refs/heads/master'
16+
if: github.repository != 'CorndelWithSoftwire/DevOps-Course-Workshop-Module-13-Learners'
17+
steps:
18+
- name: test
19+
run: echo ${{ github.repository }}
20+
- name: Checkout repo
21+
uses: actions/checkout@v3
22+
- name: Build image
23+
run: docker build --tag ${DOCKERHUB_USERNAME}/m13-order-processing-app --target production .
24+
- name: Docker login
25+
run: echo $DOCKERHUB_TOKEN | docker login -u $DOCKERHUB_USERNAME --password-stdin
26+
- name: Push image
27+
run: docker push ${DOCKERHUB_USERNAME}/m13-order-processing-app
28+
- name: Trigger deployment
29+
run: curl --fail-with-body -X POST "$AZURE_WEBHOOK"

.gitlab-ci.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
image: docker:latest
2+
stages:
3+
- deploy
4+
services:
5+
- docker:dind
6+
7+
variables:
8+
# REPLACE THIS, and set DOCKERHUB_TOKEN & AZURE_WEBHOOK in pipeline variables
9+
# Make sure that "Expand Variable" is **off** for the webhook, and that both secrets are masked
10+
DOCKERHUB_USERNAME: corndeldevopscourse
11+
12+
deploy:
13+
stage: deploy
14+
# UNCOMMENT IF YOU WANT A BRANCH CHECK
15+
# only:
16+
# refs:
17+
# - master
18+
19+
script:
20+
- docker build --tag ${DOCKERHUB_USERNAME}/m13-order-processing-app --target production .
21+
- echo $DOCKERHUB_TOKEN | docker login -u $DOCKERHUB_USERNAME --password-stdin
22+
- docker push ${DOCKERHUB_USERNAME}/m13-order-processing-app
23+
- apk add --update-cache curl
24+
- curl -dHf -X POST "$AZURE_WEBHOOK"

Dockerfile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ COPY . /app
1919

2020
WORKDIR /app
2121

22-
# Use a launch script to support both env variable expansion
23-
# And command line arguments
24-
ENTRYPOINT flask run --host 0.0.0.0 --port "${PORT}" "$@"
2522

2623
####################################
2724
# Production Image
@@ -30,9 +27,15 @@ FROM base as production
3027
ENV FLASK_ENV=production
3128
EXPOSE 80
3229

30+
ENTRYPOINT gunicorn "app:app" -b 0.0.0.0:$PORT "$@"
31+
3332
####################################
3433
# Local Development Image
3534
FROM base as development
3635

3736
ENV FLASK_ENV=development
38-
EXPOSE 80
37+
EXPOSE 80
38+
39+
# Use a launch script to support both env variable expansion
40+
# And command line arguments
41+
ENTRYPOINT flask run --host 0.0.0.0 --port "${PORT}" "$@"

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,36 @@ logging.basicConfig(level=logging.INFO)
5050

5151
## Deploy your changes
5252

53-
For the purpose of this workshop it is fine to directly push changes to the app.
54-
In a real setting you are likely to need to push changes to Git and get your CI/CD server to do a release.
53+
For the purpose of this workshop there is a default CD pipeline configured in `.github/workflows/deploy.yml` or `.gitlab-ci.yml` depending on your platform. In order to make use of this you will need to:
54+
* Update that pipeline file to contain your DockerHub username
55+
* In Azure, under your App Service's "Deployment Center", update the image name/tag to `<your_dockerhub_username>/m13-order-processing-app`
56+
* Grab the "Webhook URL" from the Deployment Center and add it to your CI pipeline secrets/variables as `AZURE_WEBHOOK`
57+
* If using GitLab, make sure that the "Expand variable" option is off, and the secret is masked and available to your branch
58+
* [Generate a DockerHub token](https://docs.docker.com/docker-hub/access-tokens/#create-an-access-token) and set it in the repository pipeline secrets/variables under `DOCKERHUB_TOKEN`
59+
60+
When you're ready, push those changes to your fork which will update your DockerHub image and notify your server to pull the latest and restart.
61+
62+
If you find this step slow, or just prefer to release manually, then you will still need to update the App Service but can always build & push the latest image directly
63+
```bash
64+
docker build --tag ${DOCKERHUB_USERNAME}/m13-order-processing-app --target production .
65+
docker push ${DOCKERHUB_USERNAME}/m13-order-processing-app
66+
```
67+
68+
You'll also need to trigger the webhook - you can do this directly, or you can register that with DockerHub to do that for you.
69+
70+
<details><summary>Register a webhook in DockerHub</summary>
5571

56-
In the root of the project run `az login` then `az webapp up -n {App Service name} && az webapp restart` (or `(az webapp up -n {App Service name}) -and (az webapp restart)` if using a version of PowerShell before PowerShell7). {App Service name} is name of the App Service resource in the Azure Portal, which is also the first part of the web address (e.g. `XXX-order-processing-app`)
57-
This will push your current code to the server and restart it.
72+
DockerHub allows you to register webhooks on individual image repositories, you can find that through:
73+
1) Signing in
74+
1) Navigating to "My Profile" from the dropdown by your username
75+
1) Selecting the relevant image repository
76+
1) Selecting "Manage Repository"
77+
1) Select the "Webhooks" tab and entering appropriate details
5878

59-
Subsequent deploys can be done with just `az webapp up && az webapp restart` (or `(az webapp up) -and (az webapp restart)` for PowerShell), as your login and the App Service name name will be remembered.
79+
Or alternatively by jumping to this URL with the appropriate values completed:
80+
81+
https://hub.docker.com/repository/docker/\<YOUR_USERNAME\>/\<IMAGE_NAME\>/webhooks
82+
</details>
6083

6184
## Investigate and fix
6285

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ flask-sqlalchemy>=2.5.1
55
pyodbc>=4.0.32
66
pytz>=2022.1
77
apscheduler==3.9.1
8+
gunicorn>=21.2.0

0 commit comments

Comments
 (0)