cd #14
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: cd | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| confirm: | |
| description: 'Type "yes" to confirm manual build & deploy on master' | |
| required: true | |
| default: 'no' | |
| env: | |
| APP_NAME: kartel | |
| WEBHOOK_PORT: 1995 | |
| API_PORT: 1996 | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| TARGET_HOST: 100.83.169.32 | |
| TARGET_USER: pfm | |
| # serialize deployment | |
| concurrency: | |
| group: kartel-prod-cd | |
| cancel-in-progress: false | |
| jobs: | |
| build: | |
| name: Build | |
| runs-on: ubuntu-latest | |
| if: github.event.inputs.confirm == 'yes' | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout codebase | |
| uses: actions/checkout@v6 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Get version from tag or use master-<short-sha> | |
| id: version | |
| run: | | |
| TAG=$(git describe --tags --exact-match 2>/dev/null || echo "") | |
| if [ -n "$TAG" ]; then | |
| VERSION=$TAG | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "has_tag=true" >> $GITHUB_OUTPUT | |
| else | |
| BRANCH=${GITHUB_REF#refs/heads/} | |
| SHORT_SHA=$(git rev-parse --short HEAD) | |
| VERSION="$BRANCH-$SHORT_SHA" | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "has_tag=false" >> $GITHUB_OUTPUT | |
| fi | |
| echo "Using version: $VERSION" | |
| - name: Extract metadata (tags, labels) | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| tags: | | |
| type=raw,value=${{ steps.version.outputs.version }} | |
| type=raw,value=latest,enable=true | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| build-args: | | |
| APP_NAME=${{ env.APP_NAME }} | |
| WEBHOOK_PORT=${{ env.WEBHOOK_PORT }} | |
| API_PORT=${{ env.API_PORT }} | |
| deploy: | |
| name: Deploy | |
| runs-on: ubuntu-latest | |
| needs: build | |
| if: github.event.inputs.confirm == 'yes' | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v6 | |
| - name: Spawning Tailscale Ephemeral Node | |
| uses: tailscale/github-action@v4 | |
| with: | |
| oauth-client-id: ${{ secrets.TS_OAUTH_CLIENT_ID }} | |
| oauth-secret: ${{ secrets.TS_OAUTH_SECRET }} | |
| tags: tag:kartel-prod-cd | |
| - name: Check connectivity to deployment target | |
| run: | | |
| tailscale ping ${{ env.TARGET_HOST }} | |
| - name: Configure SSH | |
| run: | | |
| mkdir -p ~/.ssh | |
| cat >> ~/.ssh/config << 'EOF' | |
| Host * | |
| StrictHostKeyChecking no | |
| UserKnownHostsFile=/dev/null | |
| LogLevel ERROR | |
| EOF | |
| chmod 600 ~/.ssh/config | |
| - name: Copy files into target host | |
| run: | | |
| scp docker-compose.yml ${{ env.TARGET_USER }}@${{ env.TARGET_HOST }}:/home/${{ env.TARGET_USER }}/${{ env.APP_NAME }} | |
| scp run.sh ${{ env.TARGET_USER }}@${{ env.TARGET_HOST }}:/home/${{ env.TARGET_USER }}/${{ env.APP_NAME }} | |
| - name: Run app | |
| run: | | |
| ssh -o StrictHostKeyChecking=no \ | |
| ${{ env.TARGET_USER }}@${{ env.TARGET_HOST }} \ | |
| "cd ~/${{ env.APP_NAME }} && chmod +x ~/${{ env.APP_NAME }}/run.sh && ~/${{ env.APP_NAME }}/run.sh" |