-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Ansible CD using github action to deploy project on staging vm #1123
Open
changchaishi
wants to merge
11
commits into
pycontw:master
Choose a base branch
from
changchaishi:staging
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+212
−0
Open
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0c83bd1
add cd
changchaishi 263043d
update playbook to build docker-image
changchaishi a0f03a1
use synchronize instead of copy
changchaishi f273953
update staging files
changchaishi e42879f
add documentation for CD pipeline
changchaishi e166efc
rename compose file
changchaishi f874de8
make cd server flexible
changchaishi fd20849
add google storage json to cd
changchaishi 2a30e2c
docs(cd): improve description for continuous deployment
changchaishi b152d8f
Merge branch 'master' into staging
josix 03e30ef
Merge branch 'master' into staging
josix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: CD | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: | ||
- 'master' | ||
|
||
jobs: | ||
cd: | ||
if: | | ||
github.event_name == 'push' || ( | ||
github.event_name == 'workflow_dispatch' && | ||
contains(fromJSON(vars.PROJECT_ADMINS), github.actor) | ||
) | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
- name: Generate .env for staging vm from github secrets | ||
run: | | ||
echo "${{secrets.PRODUCTION_DOT_ENV_FILE}}" > .env | ||
- name: Decode private key file for OpenSSH access over Ansible | ||
run: | | ||
echo "${{secrets.SSH_PRIVATE_KEY}}" | base64 --decode > "private.pem" | ||
chmod 400 private.pem | ||
- name: Run playbook for deployment | ||
uses: dawidd6/action-ansible-playbook@v2 | ||
with: | ||
playbook: deploy.yml | ||
inventory: | | ||
pycontw: | ||
hosts: | ||
staging: | ||
ansible_host: "${{secrets.VM_DOMAIN_IP}}" | ||
ansible_user: "${{secrets.VM_USERNAME}}" | ||
# secret file generated from previous step | ||
ansible_ssh_private_key_file: private.pem | ||
ansible_python_interpreter: "${{secrets.VM_PYTHON_INTERPRETER}}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
- name: Deploy project to staging machine | ||
hosts: staging | ||
# escalate privilege | ||
become: true | ||
become_user: dev | ||
vars: | ||
project_dir: /home/dev/web-projects/pycontw-2023-ansible | ||
|
||
tasks: | ||
- name: Dependencies check dor docker and docker-compose in remote server | ||
community.general.python_requirements_info: | ||
dependencies: | ||
- docker | ||
- docker-compose | ||
|
||
- name: Create project directory (if not exist) | ||
ansible.builtin.file: | ||
path: "{{ project_dir }}" | ||
state: directory | ||
|
||
# Copy project files to remote server (.env is included) | ||
- name: Copy project files to remote server | ||
ansible.posix.synchronize: | ||
src: ./ | ||
dest: "{{ project_dir }}" | ||
delete: true | ||
|
||
- name: Ensure docker network network-2023 exists | ||
community.docker.docker_network: | ||
name: network-2023 | ||
|
||
- name: Build and start service | ||
community.docker.docker_compose: | ||
project_src: "{{ project_dir }}" | ||
files: | ||
# use ansible-specific compose file | ||
- docker-compose-ansible.yml | ||
build: true | ||
state: present |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
version: "3.5" | ||
services: | ||
web: | ||
build: . | ||
container_name: pycontw-2023-ansible | ||
image: pycontw-2023_web-ansible | ||
hostname: pycontw-2023 | ||
entrypoint: "" | ||
command: | ||
# Hacky script for quick demonstration purpose | ||
- bash | ||
- -c | ||
- | | ||
set -o errexit -o nounset -o pipefail | ||
python3 manage.py compilemessages | ||
python3 manage.py migrate | ||
python3 manage.py collectstatic --no-input | ||
|
||
exec uwsgi --http-socket :8000 \ | ||
--master \ | ||
--hook-master-start "unix_signal:15 gracefully_kill_them_all" \ | ||
--static-map /static=assets \ | ||
--static-map /media=media \ | ||
--mount /prs=pycontw2016/wsgi.py \ | ||
--manage-script-name \ | ||
--offload-threads 2 | ||
restart: always | ||
environment: | ||
# Save us from having to type `--setting=pycontw2016.settings.production` | ||
DJANGO_SETTINGS_MODULE: pycontw2016.settings.production.pycontw2023 | ||
SCRIPT_NAME: /prs | ||
SECRET_KEY: ${SECRET_KEY} | ||
DATABASE_URL: ${DATABASE_URL} | ||
EMAIL_URL: ${EMAIL_URL} | ||
DSN_URL: ${DSN_URL} | ||
GTM_TRACK_ID: ${GTM_TRACK_ID} | ||
SLACK_WEBHOOK_URL: ${SLACK_WEBHOOK_URL} | ||
|
||
volumes: | ||
- ${MEDIA_ROOT}:/usr/local/app/src/media | ||
networks: | ||
- network | ||
|
||
networks: | ||
network: | ||
external: true | ||
name: network-2023 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Continuous Deployment on Staging Server | ||
|
||
The following describes how to setup continuous deployment for staging server. This setup presumes the site administrators have site deployment practices based on the docker production deployment [document/deploy_docker_prod.md](/document/deploy_docker_prod.md). | ||
|
||
## Requirements for Staging Server | ||
The staging server should have the following installed: | ||
- Docker 17.09+ (since we use `--chown` flag in the COPY directive) | ||
- Docker Compose | ||
- python3.6+ | ||
- [docker](https://pypi.org/project/docker/) SDK for python | ||
- [docker-compose](https://pypi.org/project/docker-compose/) SDK for python | ||
|
||
|
||
## Prerequisite for Site Administrators | ||
- Gather Container Environment Variables as mention in [document/deploy_docker_prod.md](/document/deploy_docker_prod.md). | ||
- Have a ssh user and secret file for accessing GCE instance (staging machine) | ||
- Secret file will be further encoded by base64 | ||
- Administrators github Ids | ||
- For CD workflow authorization | ||
|
||
## Settings for Github Actions Workflow | ||
After aboves steps, we have to add collected information to Github actions setting. | ||
|
||
Under the hood, we github action and [Ansible](https://www.ansible.com/overview/how-ansible-works) for continuous deployment. Github action will hold necessary variables and secrets that allows Ansible to access the staging VM on your behalf. | ||
|
||
So kindly configure project's action setting as the following: | ||
|
||
| Level | Type | Name | Value (example) | Remarks | | ||
|-----------|------------|---------------|----------|------------| | ||
| Repository | secrets | PRODUCTION_DOT_ENV_FILE | `DATABASE_URL=...` | multiline support | | ||
| Repository | secrets | VM_USERNAME | cd_user | user name for ssh {user_name}@{vm_domain} | | ||
| Repository | secrets | VM_DOMAIN_IP | staging.pycon.tw | IP address or Domain that points to the staging server | | ||
| Repository | secrets | VM_PYTHON_INTERPRETER | `/home/dev/.pyenv/shims/python` | path to your python environment that has docker/docker-compose packages installed | | ||
| Repository | secrets | SSH_PRIVATE_KEY | `21xa312....` | base64 encoded of key-pair (`.pem` file) | | ||
| Repository | variables | PROJECT_ADMINS | `["github_user_1", "github_user_2"]` | For example `["josix"]` | | ||
|
||
Reference | ||
- [Create a secret for a repository](https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository) | ||
- [Create a variable for a repository](https://docs.github.com/en/actions/learn-github-actions/variables#creating-configuration-variables-for-a-repository) | ||
- Create base64 encoded string for `key.pem` | ||
- `base64 -i key.pem` (mac) | ||
- `cat key.pem | base64` (linux) | ||
|
||
## Review | ||
### Events that triggers the pipeline | ||
1. When the PR merges to `master` | ||
- no authorization needed, as PRs normally reviewed before merge | ||
2. Manually [trigger](https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow#running-a-workflow) the CD workflow (By admins) | ||
- only for Administrator specify in repository's variable called *PROJECTS_ADMINS* | ||
|
||
Why? CD workflow will directly access to the GCE instance, should prevent unwanted deployments from PRs or push. (As a deployment guardian) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Overall the document looks great to me. Thanks @iknowright!
Here are some suggestions. If you think they would be beneficial, please feel free to adopt them.