fix: run apt-get update before installing openssh-client #8
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 & Deploy Ciyex App Stage | |
| on: | |
| push: | |
| branches: [ 'stage' ] | |
| pull_request: | |
| branches: [ 'stage' ] | |
| env: | |
| IMAGE_NAME: ciyex-app-stage | |
| REGISTRY: ${{ secrets.REGISTRY_URL }} | |
| VERSION: v1.0.${{ github.run_number }} | |
| jobs: | |
| pr-build: | |
| name: PR Build - Validate Docker Image | |
| if: github.event_name == 'pull_request' | |
| runs-on: [self-hosted, linux, x64] | |
| steps: | |
| - name: Checkout source | |
| uses: actions/checkout@v3 | |
| - name: Build Docker image (no push) | |
| run: | | |
| docker build --build-arg ENVIRONMENT=stage -t $REGISTRY/$IMAGE_NAME:$VERSION . | |
| echo "✅ PR image build successful: $REGISTRY/$IMAGE_NAME:$VERSION" | |
| build-and-deploy: | |
| name: Deploy to Staging | |
| if: github.ref == 'refs/heads/stage' | |
| runs-on: [self-hosted, linux, x64] | |
| steps: | |
| - name: Checkout Repo | |
| uses: actions/checkout@v3 | |
| - name: Setup Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| with: | |
| driver: docker | |
| - name: Setup kubectl | |
| uses: azure/setup-kubectl@v3 | |
| - name: Login to Registry | |
| run: | | |
| echo "${{ secrets.PROD_REGISTRY_PASSWORD }}" | docker login $REGISTRY -u ${{ secrets.PROD_REGISTRY_USERNAME }} --password-stdin | |
| - name: Build and Push Docker Image | |
| run: | | |
| docker build --build-arg ENVIRONMENT=stage -t $REGISTRY/$IMAGE_NAME:$VERSION . | |
| docker push $REGISTRY/$IMAGE_NAME:$VERSION | |
| echo "✅ Image pushed: $REGISTRY/$IMAGE_NAME:$VERSION" | |
| - name: Update image in kustomization | |
| run: | | |
| sed -i "s|newName: IMAGE_URL|newName: $REGISTRY/$IMAGE_NAME|g" k8s/overlays/stage/kustomization.yaml | |
| sed -i "s|newTag: IMAGE_TAG|newTag: $VERSION|g" k8s/overlays/stage/kustomization.yaml | |
| - name: Install SSH client | |
| run: which ssh || (sudo apt-get update -qq && sudo apt-get install -y openssh-client) | |
| - name: Set up SSH key | |
| run: | | |
| echo "${{ secrets.SSH_KEY_STAGE }}" > /tmp/stage_key | |
| chmod 600 /tmp/stage_key | |
| - name: Create registry pull secret | |
| run: | | |
| kubectl create secret docker-registry regcred \ | |
| --docker-server=$REGISTRY \ | |
| --docker-username=${{ secrets.PROD_REGISTRY_USERNAME }} \ | |
| --docker-password=${{ secrets.PROD_REGISTRY_PASSWORD }} \ | |
| --dry-run=client -o yaml | \ | |
| ssh -i /tmp/stage_key -o StrictHostKeyChecking=no debian@ns1005474.ip-147-135-65.us kubectl apply -f - | |
| - name: Deploy to Kubernetes | |
| run: | | |
| kubectl kustomize k8s/overlays/stage | \ | |
| ssh -i /tmp/stage_key -o StrictHostKeyChecking=no debian@ns1005474.ip-147-135-65.us kubectl apply -f - | |
| - name: Verify deployment | |
| run: | | |
| ssh -i /tmp/stage_key -o StrictHostKeyChecking=no debian@ns1005474.ip-147-135-65.us \ | |
| "kubectl rollout status deployment/stage-ciyex-app --timeout=3m && kubectl get pods -l app=ciyex-app -o wide" | |
| notify: | |
| name: Notify Team | |
| if: always() | |
| runs-on: [self-hosted, linux, x64] | |
| needs: [build-and-deploy] | |
| steps: | |
| - name: Determine deployment status | |
| id: status | |
| run: | | |
| if [ "${{ needs.build-and-deploy.result }}" == "success" ]; then | |
| echo "status=success" >> $GITHUB_OUTPUT | |
| echo "emoji=✅" >> $GITHUB_OUTPUT | |
| else | |
| echo "status=failure" >> $GITHUB_OUTPUT | |
| echo "emoji=❌" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Send Microsoft Teams notification | |
| run: | | |
| TEAMS_WEBHOOK="${{ secrets.TEAMS_WEBHOOK_URL }}" | |
| if [ -z "$TEAMS_WEBHOOK" ]; then | |
| echo "⚠️ TEAMS_WEBHOOK_URL secret not configured, skipping notification" | |
| exit 0 | |
| fi | |
| STATUS_COLOR="28a745" | |
| if [ "${{ steps.status.outputs.status }}" == "failure" ]; then | |
| STATUS_COLOR="dc3545" | |
| fi | |
| curl -H "Content-Type: application/json" -d '{ | |
| "@type": "MessageCard", | |
| "@context": "https://schema.org/extensions", | |
| "themeColor": "'"$STATUS_COLOR"'", | |
| "summary": "Ciyex Backend Stage Deployment ${{ steps.status.outputs.status }}", | |
| "sections": [{ | |
| "activityTitle": "${{ steps.status.outputs.emoji }} Ciyex Backend Stage Deployment ${{ steps.status.outputs.status }}", | |
| "activitySubtitle": "Version ${{ env.VERSION }}", | |
| "facts": [ | |
| {"name": "Environment:", "value": "Stage"}, | |
| {"name": "Version:", "value": "${{ env.VERSION }}"}, | |
| {"name": "Deployed by:", "value": "${{ github.actor }}"}, | |
| {"name": "Commit:", "value": "${{ github.sha }}"}, | |
| {"name": "URL:", "value": "https://stg.ciyex.com/api"} | |
| ], | |
| "markdown": true | |
| }], | |
| "potentialAction": [{ | |
| "@type": "OpenUri", | |
| "name": "View Workflow", | |
| "targets": [{"os": "default", "uri": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"}] | |
| }] | |
| }' "$TEAMS_WEBHOOK" |