diff --git a/.github/workflows/deploy-production.yml b/.github/workflows/deploy-production.yml index db2ce1cf2..7cdfed00a 100644 --- a/.github/workflows/deploy-production.yml +++ b/.github/workflows/deploy-production.yml @@ -1,279 +1,66 @@ name: Deploy GranCaffe Production - - on: - push: branches: - - develop - + - main workflow_dispatch: - - - permissions: - contents: read packages: write - - - env: - IMAGE_NAME: ghcr.io/${{ github.repository_owner }}/grancaffe - - - jobs: - - build-and-deploy: - - - name: Build Docker image and deploy - + name: Build and Deploy Production runs-on: ubuntu-latest - - - steps: - - - ######################################## - # Checkout - ######################################## - - - name: Checkout source code - + - name: Checkout repository uses: actions/checkout@v4 - - - - ######################################## - # Docker Buildx - ######################################## - - name: Setup Docker Buildx - uses: docker/setup-buildx-action@v3 - - - - ######################################## - # Login GHCR - ######################################## - - - name: Login GitHub Container Registry - + - name: Login to GHCR uses: docker/login-action@v3 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - - ######################################## - # Docker metadata - ######################################## - - - name: Docker metadata - - id: meta - - uses: docker/metadata-action@v5 - - with: - - images: ${{ env.IMAGE_NAME }} - - tags: | - - type=raw,value=latest - - type=sha - - - - ######################################## - # Build and push image - ######################################## - - - name: Build and push Docker image - + - name: Build and Push Docker Image uses: docker/build-push-action@v6 - - with: - context: . - file: ./Dockerfile - push: true - - platforms: linux/amd64 - - tags: | - - ${{ steps.meta.outputs.tags }} - - - labels: | - - ${{ steps.meta.outputs.labels }} - - - build-args: | - - GIT_COMMIT=${{ github.sha }} - - GIT_BRANCH=${{ github.ref_name }} - - + ${{ env.IMAGE_NAME }}:latest + ${{ env.IMAGE_NAME }}:${{ github.sha }} cache-from: | - type=gha - - cache-to: | - type=gha,mode=max - - - - - - ######################################## - # Deploy DigitalOcean - ######################################## - - - name: Deploy to DigitalOcean Droplet - - + - name: Deploy to DigitalOcean uses: appleboy/ssh-action@v1.2.0 - - with: - - host: ${{ secrets.DO_HOST }} - - username: ${{ secrets.DO_USER }} - - key: ${{ secrets.DO_SSH_KEY }} - - script: | - - set -e - - - - echo "Go to application directory" - - cd /opt/grancaffe - - - - - echo "Login GHCR" - - - echo "${{ secrets.GHCR_TOKEN }}" | docker login ghcr.io -u romamatran --password-stdin - - - - - echo "Pull latest image" - - + echo "${{ secrets.GHCR_TOKEN }}" | docker login ghcr.io \ + -u ${{ github.repository_owner }} \ + --password-stdin docker compose pull - - - - - echo "Restart containers" - - docker compose up -d --remove-orphans - - - - - echo "Cleanup old images" - - docker image prune -f - - - - - echo "Deployment completed" - - - - - - ######################################## - # Verify - ######################################## - - - name: Health check - - + - name: Verify deployment uses: appleboy/ssh-action@v1.2.0 - - with: - - host: ${{ secrets.DO_HOST }} - - username: ${{ secrets.DO_USER }} - - key: ${{ secrets.DO_SSH_KEY }} - - script: | - - - set -e - - - - echo "Containers status" - - - docker ps - - - - echo "Waiting application startup" - - sleep 20 - - - - - echo "Checking HTTP" - - + docker ps curl --fail http://localhost:5000 - - - - echo "Application is running" + echo "GranCaffe is running" diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml deleted file mode 100644 index 43bdb3740..000000000 --- a/.github/workflows/docker-image.yml +++ /dev/null @@ -1,167 +0,0 @@ -name: Docker Image CI - -on: - push: - branches: - - main - - develop - - workflow_dispatch: - - -permissions: - contents: read - packages: write - - -env: - IMAGE_NAME: ghcr.io/${{ github.repository_owner }}/grancaffe - - -jobs: - - build: - - name: Build and Push Docker Image - - runs-on: ubuntu-latest - - - steps: - - - ################################# - # Checkout source code - ################################# - - - name: Checkout repository - uses: actions/checkout@v4 - - - - ################################# - # Docker setup - ################################# - - - name: Set up QEMU - uses: docker/setup-qemu-action@v3 - - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - - ################################# - # Login GHCR - ################################# - - - name: Login to GitHub Container Registry - - uses: docker/login-action@v3 - - with: - - registry: ghcr.io - - username: ${{ github.actor }} - - password: ${{ secrets.GITHUB_TOKEN }} - - - - ################################# - # Define image tag - ################################# - - - name: Set image tag - - shell: bash - - run: | - - if [ "${{ github.ref_name }}" = "main" ]; then - - echo "IMAGE_TAG=latest" >> $GITHUB_ENV - - else - - echo "IMAGE_TAG=develop" >> $GITHUB_ENV - - fi - - - - ################################# - # Docker metadata - ################################# - - - name: Docker metadata - - id: meta - - uses: docker/metadata-action@v5 - - with: - - images: ${{ env.IMAGE_NAME }} - - tags: | - - type=raw,value=${{ env.IMAGE_TAG }} - - type=sha - - - - ################################# - # Build and push image - ################################# - - - name: Build and push Docker image - - uses: docker/build-push-action@v6 - - - with: - - context: . - - - file: ./Dockerfile - - - push: true - - - platforms: linux/amd64 - - - tags: | - - ${{ steps.meta.outputs.tags }} - - - labels: | - - ${{ steps.meta.outputs.labels }} - - - - build-args: | - - GIT_COMMIT=${{ github.sha }} - - GIT_BRANCH=${{ github.ref_name }} - - - - cache-from: | - - type=gha - - - - cache-to: | - - type=gha,mode=max diff --git a/.github/workflows/pull-request-build.yml b/.github/workflows/pull-request-build.yml new file mode 100644 index 000000000..c9efef1ed --- /dev/null +++ b/.github/workflows/pull-request-build.yml @@ -0,0 +1,24 @@ +name: Pull Request Build Test +on: + pull_request: + branches: + - main +permissions: + contents: read +jobs: + docker-build: + name: Docker Build Test + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Setup Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Build Docker image + uses: docker/build-push-action@v6 + with: + context: . + file: ./Dockerfile + push: false + tags: + grancaffe:test diff --git a/.github/workflows/test-ssh.yml b/.github/workflows/test-ssh.yml deleted file mode 100644 index 9f7653091..000000000 --- a/.github/workflows/test-ssh.yml +++ /dev/null @@ -1,26 +0,0 @@ -name: Test SSH Connection - -on: - workflow_dispatch: - -jobs: - test: - - runs-on: ubuntu-latest - - steps: - - - name: Test SSH connection - - uses: appleboy/ssh-action@v1.2.0 - - with: - host: ${{ secrets.DO_HOST }} - username: ${{ secrets.DO_USER }} - key: ${{ secrets.DO_SSH_KEY }} - - script: | - echo "Connected successfully" - hostname - whoami - docker --version diff --git a/Dockerfile b/Dockerfile index 656b2472f..183aa85da 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,9 @@ COPY ./src/ ./ # Restore RUN dotnet restore /app/Web/Grand.Web/Grand.Web.csproj +################################# +# Build application +################################# # Publish application RUN dotnet publish \