From ed51ad90f9d625830f1a998ca74e79efdb3ba382 Mon Sep 17 00:00:00 2001 From: Bobby Date: Mon, 16 Jun 2025 10:36:49 +0530 Subject: Combine build and deploy into single workflow with job dependencies --- .github/workflows/build.yml | 53 +++++++++++++++++++++++++++++++++-- .github/workflows/deploy.yml | 67 -------------------------------------------- 2 files changed, 50 insertions(+), 70 deletions(-) delete mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 84de603..44e1093 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,4 +1,4 @@ -name: Build +name: Build and Deploy on: push: @@ -7,7 +7,6 @@ on: branches: [ "main" ] jobs: - build: runs-on: ubuntu-latest steps: @@ -19,4 +18,52 @@ jobs: go-version: '1.24' - name: Build - run: go build -v ./... \ No newline at end of file + run: go build -v ./... + + deploy: + runs-on: ubuntu-latest + needs: build + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + env: + IMAGE_URL: "" + APP_SERVER: ${{ secrets.APP_SERVER }} + APP_NAME: ${{ secrets.APP_NAME }} + APP_TOKEN: ${{ secrets.APP_TOKEN }} + + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Preset Image Name + id: set_image_url + run: | + IMAGE_URL=$(echo ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:$(echo ${{ github.sha }} | cut -c1-7) | tr '[:upper:]' '[:lower:]') + echo "IMAGE_URL=$IMAGE_URL" >> $GITHUB_ENV + echo "image_url=$IMAGE_URL" >> $GITHUB_OUTPUT + + - name: Build and push Docker Image + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile + push: true + platforms: linux/arm64/v8 + tags: ${{ steps.set_image_url.outputs.image_url }} + + - name: Deploy Image to Server + uses: caprover/deploy-from-github@v1.1.2 + with: + server: ${{ env.APP_SERVER }} + app: ${{ env.APP_NAME }} + token: ${{ env.APP_TOKEN }} + image: ${{ steps.set_image_url.outputs.image_url }} \ No newline at end of file diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml deleted file mode 100644 index eaf5099..0000000 --- a/.github/workflows/deploy.yml +++ /dev/null @@ -1,67 +0,0 @@ -name: Deploy - -on: - workflow_run: - workflows: - - "Build" - - "build.yml" - types: - - completed - branches: - - main - workflow_dispatch: - -jobs: - build_and_deploy: - runs-on: ubuntu-latest - if: ${{ github.event.workflow_run.conclusion == 'success' }} - env: - IMAGE_URL: "" - APP_SERVER: ${{ secrets.APP_SERVER }} - APP_NAME: ${{ secrets.APP_NAME }} - APP_TOKEN: ${{ secrets.APP_TOKEN }} - - steps: - - name: Debug workflow run info - run: | - echo "Workflow run conclusion: ${{ github.event.workflow_run.conclusion }}" - echo "Workflow run status: ${{ github.event.workflow_run.status }}" - echo "Workflow run name: ${{ github.event.workflow_run.name }}" - echo "Branch: ${{ github.event.workflow_run.head_branch }}" - - - name: Check out repository - uses: actions/checkout@v4 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Login to Container Registry - uses: docker/login-action@v3 - with: - registry: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Preset Image Name - id: set_image_url - run: | - IMAGE_URL=$(echo ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:$(echo ${{ github.sha }} | cut -c1-7) | tr '[:upper:]' '[:lower:]') - echo "IMAGE_URL=$IMAGE_URL" >> $GITHUB_ENV - echo "image_url=$IMAGE_URL" >> $GITHUB_OUTPUT - - - name: Build and push Docker Image - uses: docker/build-push-action@v5 - with: - context: . - file: ./Dockerfile - push: true - platforms: linux/arm64/v8 - tags: ${{ steps.set_image_url.outputs.image_url }} - - - name: Deploy Image to Server - uses: caprover/deploy-from-github@v1.1.2 - with: - server: ${{ env.APP_SERVER }} - app: ${{ env.APP_NAME }} - token: ${{ env.APP_TOKEN }} - image: ${{ steps.set_image_url.outputs.image_url }} \ No newline at end of file -- cgit v1.2.3