File tree 4 files changed +76
-16
lines changed 4 files changed +76
-16
lines changed Original file line number Diff line number Diff line change 1
1
name : CD
2
2
3
- on : [workflow_dispatch, pull_request, push]
3
+ on :
4
+ workflow_dispatch :
5
+ push :
6
+ branches :
7
+ - ' master'
4
8
5
9
jobs :
6
10
cd :
11
+ # the secret for pipeline only
12
+ env :
13
+ project_admins : ${{ secrets.PROJECT_ADMINS }}
14
+ if : |
15
+ github.event_name == 'push' || (
16
+ github.event_name == 'workflow_dispatch' &&
17
+ contains(fromJSON(vars.PROJECT_ADMINS), github.actor)
18
+ )
7
19
runs-on : ubuntu-latest
8
20
steps :
9
21
- name : Checkout
10
22
uses : actions/checkout@v3
11
-
12
- - name : Decode private key file
23
+ - name : Generate .env for staging vm from github secrets
13
24
run : |
14
25
echo "${{secrets.PRODUCTION_DOT_ENV_FILE}}" > .env
26
+ - name : Decode private key file for OpenSSH access over Ansible
27
+ run : |
15
28
echo "${{secrets.SSH_PRIVATE_KEY}}" | base64 --decode > "private.pem"
16
29
chmod 400 private.pem
17
-
18
- - name : Run CD playbook
30
+ - name : Run playbook for deployment
19
31
uses : dawidd6/action-ansible-playbook@v2
20
32
with :
21
33
playbook : deploy.yml
24
36
hosts:
25
37
staging:
26
38
ansible_host: staging.pycon.tw
27
- ansible_user: changchaishi
39
+ ansible_user: "${{secrets.GCE_USERNAME}}"
40
+ # secret file generated from previous step
28
41
ansible_ssh_private_key_file: private.pem
29
42
ansible_python_interpreter: /home/dev/.pyenv/shims/python
Original file line number Diff line number Diff line change 1
1
---
2
- - name : Check services
2
+ - name : Deploy project to staging machine
3
3
hosts : staging
4
- # need to use become since I'm connecting using personal private key
4
+ # escalate privilege
5
5
become : true
6
- # switch user as dev
7
6
become_user : dev
8
7
vars :
9
8
project_dir : /home/dev/web-projects/pycontw-2023-ansible
10
9
11
10
tasks :
12
- - name : Ensure that Docker for python is present ( docker in pip)
11
+ - name : Dependencies check dor docker and docker-compose in remote server
13
12
community.general.python_requirements_info :
14
13
dependencies :
15
14
- docker
15
+ - docker-compose
16
16
17
- - name : Create a directory if it does not exist
17
+ - name : Create project directory ( if not exist)
18
18
ansible.builtin.file :
19
19
path : " {{ project_dir }}"
20
20
state : directory
21
21
22
- - name : Copy entire project files to remote server
22
+ # Copy project files to remote server (.env is included)
23
+ - name : Copy project files to remote server
23
24
ansible.posix.synchronize :
24
25
src : ./
25
26
dest : " {{ project_dir }}"
33
34
community.docker.docker_compose :
34
35
project_src : " {{ project_dir }}"
35
36
build : true
36
- # try to build first, without up the service
37
- state : absent
37
+ state : present
Original file line number Diff line number Diff line change
1
+ version : " 3.5"
2
+ services :
3
+ web :
4
+ build : .
5
+ container_name : pycontw-2023-ansible
6
+ image : pycontw-2023_web-ansible
7
+ hostname : pycontw-2023
8
+ entrypoint : " "
9
+ command :
10
+ # Hacky script for quick demonstration purpose
11
+ - bash
12
+ - -c
13
+ - |
14
+ set -o errexit -o nounset -o pipefail
15
+ python3 manage.py compilemessages
16
+ python3 manage.py migrate
17
+ python3 manage.py collectstatic --no-input
18
+
19
+ exec uwsgi --http-socket :8000 \
20
+ --master \
21
+ --hook-master-start "unix_signal:15 gracefully_kill_them_all" \
22
+ --static-map /static=assets \
23
+ --static-map /media=media \
24
+ --mount /prs=pycontw2016/wsgi.py \
25
+ --manage-script-name \
26
+ --offload-threads 2
27
+ restart : always
28
+ environment :
29
+ # Save us from having to type `--setting=pycontw2016.settings.production`
30
+ DJANGO_SETTINGS_MODULE : pycontw2016.settings.production.pycontw2023
31
+ SCRIPT_NAME : /prs
32
+ SECRET_KEY : ${SECRET_KEY}
33
+ DATABASE_URL : ${DATABASE_URL}
34
+ EMAIL_URL : ${EMAIL_URL}
35
+ DSN_URL : ${DSN_URL}
36
+ GTM_TRACK_ID : ${GTM_TRACK_ID}
37
+ SLACK_WEBHOOK_URL : ${SLACK_WEBHOOK_URL}
38
+
39
+ volumes :
40
+ - ${MEDIA_ROOT}:/usr/local/app/src/media
41
+ networks :
42
+ - network
43
+
44
+ networks :
45
+ network :
46
+ external : true
47
+ name : network-2023
Original file line number Diff line number Diff line change @@ -2,8 +2,8 @@ version: "3.5"
2
2
services :
3
3
web :
4
4
build : .
5
- container_name : pycontw-2023-ansible
6
- image : pycontw-2023_web-ansible
5
+ container_name : pycontw-2023
6
+ image : pycontw-2023_web
7
7
hostname : pycontw-2023
8
8
entrypoint : " "
9
9
command :
You can’t perform that action at this time.
0 commit comments