-
Notifications
You must be signed in to change notification settings - Fork 0
78 lines (74 loc) · 3.63 KB
/
CICD.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
name: CICD
on:
push:
branches:
- prod
jobs:
run_tests:
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3
- name: setup the testing server with required packages
run: |
sudo apt-get -y upgrade && sudo apt-get -y update && sudo apt-get -y install python3.9
sudo apt -y install python3-wheel vim
export FLASK_APP=blogging.py
export FLASK_CONFIG=testing
python3 -m pip install -r requirements/common.txt
python3 -m flask test
build_container_and_push_to_dockerhub:
runs-on: ubuntu-latest
needs: run_tests
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3
- name: create the required env variable files from the secrets
# touch .env-mysql is needed since even though we are not building mysql, the docker-compose file has the mysql service there too
# and it still looks for it, so just make it to satisfy this unnecessity.
run: |
touch .env-mysql
touch .env-blogging
echo FLASK_APP=${{ secrets.FLASK_APP }} >> .env-blogging
echo FLASK_CONFIG=${{ secrets.FLASK_CONFIG }} >> .env-blogging
echo SECRET_KEY=${{ secrets.SECRET_KEY }} >> .env-blogging
echo DATABASE_URL=${{ secrets.DATABASE_URL }} >> .env-blogging
- name: build the docker image for the blogging service, keep the mysql service intact
run: docker-compose build --no-cache blogging
- name: Log in to docker hub
run: docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Tag image
run:
# Usage of tag command is: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
# With this setup, the image, which was built untagged, is now given the "latest" tag
docker tag aldosebastian/blogging aldosebastian/blogging:latest
- name: Push image to dockerhub
# We have push the using the tag instead of the image name; it's just the way things are
run: docker push aldosebastian/blogging:latest
# After pushing, we go to the server, download the image and run it there
deploy_service_on_server:
runs-on: ubuntu-latest
needs: build_container_and_push_to_dockerhub
steps:
- name: Deploy to server via SSH action
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSHHOST1 }}
username: root
key: ${{ secrets.SSHKEY1}}
script: |
# Move to directory where the docker-compose.yml is
cd /home/aldo/
# Login to dockerhub registry
docker login -u aldosebastian -p ${{ secrets.DOCKERHUB_PASSWORD }}
# Stop and remove any blogging service container
docker stop blogging_service && docker rm blogging_service
# Delete the image that ran the deleted container (i.e. the one with the tag latest), since now this image is outdated
# (this is to save space, if you want you can skip this. If skipped, after docker pull the old image will have tag None)
docker rmi aldosebastian/blogging:latest
# Pull the latest image
docker pull aldosebastian/blogging
# Run the blogging service container using the docker-compose.yml in the server.
# The below actually will start both the blogging and its db containers, but if a db
# container already is running, it will say it is still up to date
docker-compose up -d