diff --git a/.github/workflows/ansible-deploy-dev.yml b/.github/workflows/ansible-deploy-dev.yml deleted file mode 100644 index 9bb8899..0000000 --- a/.github/workflows/ansible-deploy-dev.yml +++ /dev/null @@ -1,50 +0,0 @@ -name: Deploy to dev server with Ansible playbook - -on: - workflow_run: - workflows: ["Django tests"] - types: - - completed - branches: - - dev - -jobs: - deploy: - runs-on: ubuntu-latest - environment: deployment - if: ${{ github.event.workflow_run.conclusion == 'success' }} - concurrency: - group: docker-build-group - cancel-in-progress: false - - steps: - - name: Checkout Repository - uses: actions/checkout@v2 - with: - ref: dev - - - name: Set up Python - uses: actions/setup-python@v2 - with: - python-version: '3.11' - - - name: Install Ansible - run: | - python -m pip install --upgrade pip - pip install ansible - - - name: Install ssh keys - # For reference: https://stackoverflow.com/a/70447517 - run: | - install -m 600 -D /dev/null ~/.ssh/galaxy - echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/galaxy - echo "${{ secrets.SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts - - - name: Run Ansible Playbook - run: | - cd ansible - ansible-playbook -i hosts dev.yml --tags update - env: - ANSIBLE_HOST_KEY_CHECKING: 'False' - ANSIBLE_REMOTE_USER: ${{ secrets.SSH_USER }} - ANSIBLE_PRIVATE_KEY_FILE: ~/.ssh/galaxy diff --git a/.github/workflows/ansible-deploy.yml b/.github/workflows/ansible-deploy.yml index 9e4eb53..73a8407 100644 --- a/.github/workflows/ansible-deploy.yml +++ b/.github/workflows/ansible-deploy.yml @@ -2,26 +2,26 @@ name: Deploy to prod server with Ansible playbook on: workflow_run: - workflows: ["Django tests"] + workflows: ["Build and Push Docker Image"] types: - completed branches: + - dev - main jobs: deploy: runs-on: ubuntu-latest environment: deployment - if: ${{ github.event.workflow_run.conclusion == 'success' }} concurrency: - group: docker-build-group - cancel-in-progress: false + group: deploy-group + cancel-in-progress: true steps: - name: Checkout Repository uses: actions/checkout@v2 with: - ref: main + ref: ${{ github.ref }} - name: Set up Python uses: actions/setup-python@v2 @@ -42,8 +42,13 @@ jobs: - name: Run Ansible Playbook run: | + if [ $GITHUB_REF == "refs/heads/dev" ]; then + PLAYBOOK=dev.yml + else + PLAYBOOK=prod.yml + fi cd ansible - ansible-playbook -i hosts prod.yml --tags update + ansible-playbook -i hosts $PLAYBOOK --tags update env: ANSIBLE_HOST_KEY_CHECKING: 'False' ANSIBLE_REMOTE_USER: ${{ secrets.SSH_USER }} diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml index 6b71c63..bf7430b 100644 --- a/.github/workflows/docker-build.yml +++ b/.github/workflows/docker-build.yml @@ -1,37 +1,53 @@ name: Build and Push Docker Image on: - push: + workflow_run: + workflows: ["Django tests"] + types: + - completed branches: - - main - dev - paths: - - Dockerfile - - requirements.txt + - main jobs: build: runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'success' }} environment: build concurrency: - group: docker-build-group - cancel-in-progress: false + group: deploy-group + cancel-in-progress: true steps: - name: Checkout code uses: actions/checkout@v3 + - name: Check if Dockerfile or requirements.txt was changed + id: check_dependencies + run: | + if git diff --name-only HEAD^ HEAD | grep -q -e 'Dockerfile' -e 'requirements.txt'; then + echo "Dockerfile or requirements.txt changed" + echo "::set-output name=updated::true" + else + echo "Dockerfile or requirements.txt not changed" + echo "::set-output name=updated::false" + fi + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 + if: steps.check_dependencies.outputs.updated == 'true' - name: Log in to DockerHub uses: docker/login-action@v2 + if: steps.check_dependencies.outputs.updated == 'true' with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Build Docker image + if: steps.check_dependencies.outputs.updated == 'true' run: docker build -t ${{ secrets.DOCKER_IMAGE }}:latest . - name: Push Docker image + if: steps.check_dependencies.outputs.updated == 'true' run: docker push ${{ secrets.DOCKER_IMAGE }}:latest