Merge pull request #48 from lsjmc0224/main #126
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
name: Web Application Server - CI/CD | |
on: | |
push: | |
branches: ["main"] | |
pull_request_target: | |
types: [labeled, closed] | |
jobs: | |
# safe tag에 대한 gradlew test && merged에 대한 docker image build and push | |
CI: | |
if: contains(github.event.pull_request.labels.*.name, 'safe') | |
runs-on: ubuntu-20.04 | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Create .env | |
shell: bash | |
run: | |
touch .env; | |
echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env; | |
echo "DEBUG_VALUE=${{ secrets.DEBUG_VALUE }}" >> .env; | |
echo "DJANGO_DEPLOY=${{ secrets.DJANGO_DEPLOY }}" >> .env; | |
echo "DATABASE_ENGINE=${{ secrets.DATABASE_ENGINE }}" >> .env; | |
echo "DATABASE_NAME=${{ secrets.DATABASE_NAME }}" >> .env; | |
echo "DATABASE_USER=${{ secrets.DATABASE_USER }}" >> .env; | |
echo "DATABASE_USER_PASSWORD=${{ secrets.DATABASE_USER_PASSWORD }}" >> .env; | |
echo "DATABASE_HOST=${{ secrets.DATABASE_HOST }}" >> .env; | |
echo "DATABASE_PORT=${{ secrets.DATABASE_PORT }}" >> .env; | |
cat .env | |
- name: Set up Python 3.11 | |
uses: actions/setup-python@v2 | |
with: | |
python-version: 3.11 | |
- name: Install dependencies | |
run: | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
- name: Run tests | |
run: | |
python3 manage.py test | |
### Docker Image Build and Push ### | |
- name: Login to Docker Hub | |
if: github.event.pull_request.merged == true | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Set up Docker Buildx | |
if: github.event.pull_request.merged == true | |
uses: docker/setup-buildx-action@v2 | |
- name: Build and push | |
if: github.event.pull_request.merged == true | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPONAME }} | |
# closed에 대한 server deploy | |
CD: | |
if: github.event.pull_request.merged == true | |
needs: [CI] | |
runs-on: ubuntu-20.04 | |
steps: | |
### SSH Connect and Docker Image Pull and Container Run | |
- name: Docker Image Pull and Container Run | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
password: ${{ secrets.SSH_PASSWORD }} | |
port: ${{ secrets.SSH_PORT }} | |
script: | | |
docker stop mua-dongguk-server | |
docker rm mua-dongguk-server | |
docker image rm ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPONAME }} | |
docker run -d -v /home/django/media:/app/media -v /home/django/static:/app/static -e TZ=Asia/Seoul -p 8000:8000 --name mua-dongguk-server ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPONAME }} | |
- name: Collect static files | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
password: ${{ secrets.SSH_PASSWORD }} | |
port: ${{ secrets.SSH_PORT }} | |
script: | | |
docker exec mua-dongguk-server python manage.py collectstatic --noinput |