|
| 1 | +name: Deploy LoopBack 4 to K3s via Tailscale |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - dev |
| 7 | + - main |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +env: |
| 11 | + IMAGE_REGISTRY: selecro |
| 12 | + |
| 13 | +jobs: |
| 14 | + ci: |
| 15 | + permissions: |
| 16 | + contents: read |
| 17 | + pull-requests: write |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Checkout code |
| 21 | + uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + |
| 25 | + - name: Set up Node.js |
| 26 | + uses: actions/setup-node@v4 |
| 27 | + with: |
| 28 | + node-version: 'lts/*' |
| 29 | + |
| 30 | + - name: Install dependencies |
| 31 | + run: npm ci |
| 32 | + |
| 33 | + - name: Lint |
| 34 | + run: npm run lint |
| 35 | + |
| 36 | + - name: Test |
| 37 | + run: npm run test |
| 38 | + |
| 39 | + build-and-push: |
| 40 | + permissions: |
| 41 | + contents: read |
| 42 | + pull-requests: write |
| 43 | + needs: ci |
| 44 | + runs-on: ubuntu-latest |
| 45 | + steps: |
| 46 | + - name: Checkout code |
| 47 | + - name: Checkout code |
| 48 | + uses: actions/checkout@v4 |
| 49 | + with: |
| 50 | + fetch-depth: 0 |
| 51 | + |
| 52 | + - name: Set deployment variables |
| 53 | + id: vars |
| 54 | + run: | |
| 55 | + BRANCH="${GITHUB_REF#refs/heads/}" |
| 56 | + case "$BRANCH" in |
| 57 | + dev) |
| 58 | + BACKEND_NAME="backend-dev" |
| 59 | + ;; |
| 60 | + main) |
| 61 | + BACKEND_NAME="backend-main" |
| 62 | + ;; |
| 63 | + *) |
| 64 | + echo "Invalid branch for deployment" && exit 1 |
| 65 | + ;; |
| 66 | + esac |
| 67 | + IMAGE_TAG="${{ github.ref_name }}-${{ github.sha }}" |
| 68 | + IMAGE_NAME="${{ env.IMAGE_REGISTRY }}/${BACKEND_NAME}:${IMAGE_TAG}" |
| 69 | + echo "BACKEND_NAME=$BACKEND_NAME" >> "$GITHUB_OUTPUT" |
| 70 | + echo "IMAGE_NAME=$IMAGE_NAME" >> "$GITHUB_OUTPUT" |
| 71 | + echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT" |
| 72 | + BRANCH="${GITHUB_REF#refs/heads/}" |
| 73 | + case "$BRANCH" in |
| 74 | + dev) |
| 75 | + BACKEND_NAME="backend-dev" |
| 76 | + ;; |
| 77 | + main) |
| 78 | + BACKEND_NAME="backend-main" |
| 79 | + ;; |
| 80 | + *) |
| 81 | + echo "Invalid branch for deployment" && exit 1 |
| 82 | + ;; |
| 83 | + esac |
| 84 | + IMAGE_TAG="${{ github.ref_name }}-${{ github.sha }}" |
| 85 | + IMAGE_NAME="${{ env.IMAGE_REGISTRY }}/${BACKEND_NAME}:${IMAGE_TAG}" |
| 86 | + echo "BACKEND_NAME=$BACKEND_NAME" >> "$GITHUB_OUTPUT" |
| 87 | + echo "IMAGE_NAME=$IMAGE_NAME" >> "$GITHUB_OUTPUT" |
| 88 | + echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT" |
| 89 | + |
| 90 | + - name: Log in to Docker Hub |
| 91 | + uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 |
| 92 | + with: |
| 93 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 94 | + password: ${{ secrets.DOCKER_PASSWORD }} |
| 95 | + |
| 96 | + - name: Build and push Docker image |
| 97 | + run: | |
| 98 | + docker build -t "${{ steps.vars.outputs.IMAGE_NAME }}" . |
| 99 | + docker push "${{ steps.vars.outputs.IMAGE_NAME }}" |
| 100 | + |
| 101 | + deploy: |
| 102 | + permissions: |
| 103 | + contents: read |
| 104 | + pull-requests: write |
| 105 | + needs: build-and-push |
| 106 | + runs-on: ubuntu-latest |
| 107 | + steps: |
| 108 | + - name: Checkout code |
| 109 | + uses: actions/checkout@v4 |
| 110 | + with: |
| 111 | + fetch-depth: 0 |
| 112 | + |
| 113 | + - name: Install and authenticate Tailscale |
| 114 | + run: | |
| 115 | + curl -fsSL https://tailscale.com/install.sh | sh |
| 116 | + sudo tailscale up --auth-key=${{ secrets.TAILSCALE_AUTH_KEY }} |
| 117 | + sudo tailscale status |
| 118 | + |
| 119 | + - name: Install kubectl |
| 120 | + uses: azure/setup-kubectl@0c5e050edfed71b2b50731ab044d42489d51c129 |
| 121 | + with: |
| 122 | + version: 'latest' |
| 123 | + |
| 124 | + - name: Configure kubectl |
| 125 | + run: | |
| 126 | + echo "${{ secrets.K3S_KUBECONFIG }}" | base64 -d > kubeconfig.yaml |
| 127 | + export KUBECONFIG=$(pwd)/kubeconfig.yaml |
| 128 | + kubectl config current-context # Verify the context |
| 129 | + |
| 130 | + - name: Check Cluster Connection |
| 131 | + run: kubectl cluster-info |
| 132 | + |
| 133 | + - name: Apply Kubernetes Manifests |
| 134 | + echo "${{ secrets.K3S_KUBECONFIG }}" | base64 -d > kubeconfig.yaml |
| 135 | + export KUBECONFIG=$(pwd)/kubeconfig.yaml |
| 136 | + kubectl config current-context # Verify the context |
| 137 | + |
| 138 | + - name: Check Cluster Connection |
| 139 | + run: kubectl cluster-info |
| 140 | + |
| 141 | + - name: Apply Kubernetes Manifests |
| 142 | + run: | |
| 143 | + envsubst < k8s/deployment.yaml | kubectl apply -f - |
| 144 | + envsubst < k8s/service.yaml | kubectl apply -f - |
| 145 | + env: |
| 146 | + BACKEND_NAME: ${{ needs.build-and-push.outputs.BACKEND_NAME }} |
| 147 | + IMAGE_NAME: ${{ needs.build-and-push.outputs.IMAGE_NAME }} |
| 148 | + SHORT_SHA: ${{ needs.build-and-push.outputs.SHORT_SHA }} |
| 149 | + |
| 150 | + - name: Wait for Deployment to be Ready |
| 151 | + run: kubectl rollout status deployment/${{ needs.build-and-push.outputs.BACKEND_NAME }} --timeout=120s |
| 152 | + |
| 153 | + - name: Clean up kubeconfig |
| 154 | + if: always() |
| 155 | + run: rm -f kubeconfig.yaml |
| 156 | + envsubst < k8s/deployment.yaml | kubectl apply -f - |
| 157 | + envsubst < k8s/service.yaml | kubectl apply -f - |
| 158 | + env: |
| 159 | + BACKEND_NAME: ${{ needs.build-and-push.outputs.BACKEND_NAME }} |
| 160 | + IMAGE_NAME: ${{ needs.build-and-push.outputs.IMAGE_NAME }} |
| 161 | + SHORT_SHA: ${{ needs.build-and-push.outputs.SHORT_SHA }} |
| 162 | + |
| 163 | + - name: Wait for Deployment to be Ready |
| 164 | + run: kubectl rollout status deployment/${{ needs.build-and-push.outputs.BACKEND_NAME }} --timeout=120s |
| 165 | + |
| 166 | + - name: Clean up kubeconfig |
| 167 | + if: always() |
| 168 | + run: rm -f kubeconfig.yaml |
0 commit comments