Build and deploy #49
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: Build and Push Docker Image | |
on: | |
workflow_run: | |
workflows: ["Django tests"] | |
types: | |
- completed | |
branches: | |
- dev | |
- main | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.event == 'push' && github.event.workflow_run.conclusion == 'success' }} | |
environment: build | |
concurrency: | |
group: deploy-group-${{ github.ref }} | |
cancel-in-progress: true | |
outputs: | |
branch: "${{ github.event.workflow_run.outputs.branch }}" | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
with: | |
ref: "${{ github.event.workflow_run.outputs.branch }}" | |
fetch-depth: 0 # Fetch the entire history to ensure 'HEAD^' is resolvable | |
- name: Debug github refs | |
run: | | |
echo "github.event.workflow_run.outputs.branch = ${{ github.event.workflow_run.outputs.branch }}" | |
- name: Check if Dockerfile or requirements.txt was changed | |
id: check_dependencies | |
run: | | |
if git diff --name-only "${{ github.event.workflow_run.outputs.branch }}" | 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 |