Skip to content

Commit 0e5a1bb

Browse files
committed
update staging files
1 parent a0f03a1 commit 0e5a1bb

File tree

4 files changed

+76
-16
lines changed

4 files changed

+76
-16
lines changed

.github/workflows/cd.yml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
name: CD
22

3-
on: [workflow_dispatch, pull_request, push]
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- 'master'
48

59
jobs:
610
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+
)
719
runs-on: ubuntu-latest
820
steps:
921
- name: Checkout
1022
uses: actions/checkout@v3
11-
12-
- name: Decode private key file
23+
- name: Generate .env for staging vm from github secrets
1324
run: |
1425
echo "${{secrets.PRODUCTION_DOT_ENV_FILE}}" > .env
26+
- name: Decode private key file for OpenSSH access over Ansible
27+
run: |
1528
echo "${{secrets.SSH_PRIVATE_KEY}}" | base64 --decode > "private.pem"
1629
chmod 400 private.pem
17-
18-
- name: Run CD playbook
30+
- name: Run playbook for deployment
1931
uses: dawidd6/action-ansible-playbook@v2
2032
with:
2133
playbook: deploy.yml
@@ -24,6 +36,7 @@ jobs:
2436
hosts:
2537
staging:
2638
ansible_host: staging.pycon.tw
27-
ansible_user: changchaishi
39+
ansible_user: "${{secrets.GCE_USERNAME}}"
40+
# secret file generated from previous step
2841
ansible_ssh_private_key_file: private.pem
2942
ansible_python_interpreter: /home/dev/.pyenv/shims/python

deploy.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
---
2-
- name: Check services
2+
- name: Deploy project to staging machine
33
hosts: staging
4-
# need to use become since I'm connecting using personal private key
4+
# escalate privilege
55
become: true
6-
# switch user as dev
76
become_user: dev
87
vars:
98
project_dir: /home/dev/web-projects/pycontw-2023-ansible
109

1110
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
1312
community.general.python_requirements_info:
1413
dependencies:
1514
- docker
15+
- docker-compose
1616

17-
- name: Create a directory if it does not exist
17+
- name: Create project directory (if not exist)
1818
ansible.builtin.file:
1919
path: "{{ project_dir }}"
2020
state: directory
2121

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
2324
ansible.posix.synchronize:
2425
src: ./
2526
dest: "{{ project_dir }}"
@@ -33,5 +34,4 @@
3334
community.docker.docker_compose:
3435
project_src: "{{ project_dir }}"
3536
build: true
36-
# try to build first, without up the service
37-
state: absent
37+
state: present

docker-compose-staging.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ version: "3.5"
22
services:
33
web:
44
build: .
5-
container_name: pycontw-2023-ansible
6-
image: pycontw-2023_web-ansible
5+
container_name: pycontw-2023
6+
image: pycontw-2023_web
77
hostname: pycontw-2023
88
entrypoint: ""
99
command:

0 commit comments

Comments
 (0)