Build and Push Docker Images #12
This file contains hidden or 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 Images | |
| on: | |
| push: | |
| branches: | |
| - master | |
| paths: | |
| - "server/**" | |
| workflow_dispatch: | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME_API: ${{ github.repository }}/fastapi | |
| IMAGE_NAME_CELERY: ${{ github.repository }}/celery | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Log in to Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata for FastAPI | |
| id: meta-api | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_API }} | |
| tags: | | |
| type=ref,event=branch | |
| type=ref,event=pr | |
| type=semver,pattern={{version}} | |
| type=semver,pattern={{major}}.{{minor}} | |
| type=sha,prefix={{branch}}- | |
| - name: Extract metadata for Celery | |
| id: meta-celery | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_CELERY }} | |
| tags: | | |
| type=ref,event=branch | |
| type=ref,event=pr | |
| type=semver,pattern={{version}} | |
| type=semver,pattern={{major}}.{{minor}} | |
| type=sha,prefix={{branch}}- | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Build and push FastAPI Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./server | |
| file: ./server/Dockerfile | |
| push: true | |
| tags: ${{ steps.meta-api.outputs.tags }} | |
| labels: ${{ steps.meta-api.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Build and push Celery Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./server | |
| file: ./server/Dockerfile.celery | |
| push: true | |
| tags: ${{ steps.meta-celery.outputs.tags }} | |
| labels: ${{ steps.meta-celery.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Generate summary | |
| run: | | |
| echo "## Docker Images Built and Pushed 🚀" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "### FastAPI Image" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Registry:** ${{ env.REGISTRY }}" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Repository:** ${{ env.IMAGE_NAME_API }}" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Tags:** ${{ steps.meta-api.outputs.tags }}" >> $GITHUB_STEP_SUMMARY | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| echo "### Celery Image" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Registry:** ${{ env.REGISTRY }}" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Repository:** ${{ env.IMAGE_NAME_CELERY }}" >> $GITHUB_STEP_SUMMARY | |
| echo "- **Tags:** ${{ steps.meta-celery.outputs.tags }}" >> $GITHUB_STEP_SUMMARY | |
| deploy-to-azure: | |
| needs: build-and-push | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Install Ansible | |
| run: | | |
| sudo apt update | |
| sudo apt install software-properties-common | |
| sudo add-apt-repository --yes --update ppa:ansible/ansible | |
| sudo apt install ansible -y | |
| ansible-galaxy collection install community.docker | |
| ansible --version | |
| - name: Set up SSH key | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/azure_vm_key | |
| chmod 600 ~/.ssh/azure_vm_key | |
| ssh-keyscan -H ${{ secrets.AZURE_VM_HOST }} >> ~/.ssh/known_hosts | |
| - name: Deploy with Ansible | |
| run: | | |
| ansible-playbook -i ./infra/backend/ansible/inventory.ini ./infra/backend/ansible/redeploy.yml \ | |
| --extra-vars "@infra/backend/ansible/group_vars/server-redeploy.yml" \ | |
| -e "fastapi_image=${{ env.REGISTRY }}/$(echo ${{ env.IMAGE_NAME_API }}:${{ github.ref_name }}-${GITHUB_SHA::7} | tr '[:upper:]' '[:lower:]')" \ | |
| -e "celery_image=${{ env.REGISTRY }}/$(echo ${{ env.IMAGE_NAME_CELERY }}:${{ github.ref_name }}-${GITHUB_SHA::7} | tr '[:upper:]' '[:lower:]')" |