diff --git a/.env.example b/.env.example index 471b9a1..473a8d9 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,7 @@ RABBITMQ_URL=amqp://guest:guest@127.0.0.1:5672/ -REDIS_ADDRESS=127.0.0.1:6379 +REDIS_ADDRESS=redis:6379 REDIS_PASSWORD= REDIS_DB=0 @@ -20,4 +20,4 @@ MAIL_PORT=587 PMB_DOMAIN="" PMB_USER="" -PMB_PASSWORD="" \ No newline at end of file +PMB_PASSWORD="" diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml new file mode 100644 index 0000000..b07ede0 --- /dev/null +++ b/.github/workflows/deploy.yaml @@ -0,0 +1,98 @@ + +name: Deploy to Production + +on: + push: + branches: + - main + +env: + PROJECT_NAME: notification + +permissions: + contents: write + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Copy env + run: | + cp .env.example .env + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build Docker image + uses: docker/build-push-action@v5 + with: + context: . + file: ./docker/Dockerfile.web + push: false + load: true + tags: ${{ env.PROJECT_NAME }}:test + no-cache: true + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Tag and push to Docker Hub + run: | + docker tag ${{ env.PROJECT_NAME }}:test ${{ secrets.DOCKER_USERNAME }}/${{ env.PROJECT_NAME }}:latest + docker tag ${{ env.PROJECT_NAME }}:test ${{ secrets.DOCKER_USERNAME }}/${{ env.PROJECT_NAME }}:${{ github.run_number }} + docker push ${{ secrets.DOCKER_USERNAME }}/${{ env.PROJECT_NAME }}:latest + docker push ${{ secrets.DOCKER_USERNAME }}/${{ env.PROJECT_NAME }}:${{ github.run_number }} + echo "SUCCESS TAGS: latest, ${{ github.run_number }}" + + - name: Update stack.yaml and version + run: | + sed -i 's|image: ${{ secrets.DOCKER_USERNAME }}/${{ env.PROJECT_NAME }}:.*|image: ${{ secrets.DOCKER_USERNAME }}/${{ env.PROJECT_NAME }}:${{ github.run_number }}|' stack.yaml + + - name: Commit and push updated version + run: | + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git add . + git commit -m "🔄 Update image to ${{ github.run_number }} [CI SKIP]" || echo "No changes" + git pull origin main --rebase + git push origin main + + - name: Deploy to server via SSH + uses: appleboy/ssh-action@v1.2.2 + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + # key: ${{ secrets.KEY }} + password: ${{ secrets.PASSWORD }} + port: ${{ secrets.PORT }} + script: | + PROJECTS=/opt/projects/ + DIR=/opt/projects/${{ env.PROJECT_NAME }}/ + + if [ -d "$PROJECTS" ]; then + echo "projects papkasi mavjud" + else + mkdir -p $PROJECTS + echo "projects papkasi yaratildi" + fi + + if [ -d "$DIR" ]; then + echo "loyiha mavjud" + else + cd $PROJECTS + git clone git@gitea.felixits.uz:${{ github.repository }}.git ${{ env.PROJECT_NAME }} + echo "Clone qilindi"; + fi + + cd $DIR + git fetch origin main + git reset --hard origin/main + docker stack deploy -c stack.yaml ${{ env.PROJECT_NAME }} + diff --git a/Dockerfile b/Dockerfile index 8f77c01..f73b81a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,6 +15,5 @@ FROM alpine WORKDIR /app COPY --from=build /app/notification . -COPY ./.env /app/ CMD ["./notification"] diff --git a/cmd/main.go b/cmd/main.go index 0a1748b..0ffeded 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "log" "github.com/JscorpTech/notification/internal/consumer" "github.com/JscorpTech/notification/internal/redis" @@ -12,7 +13,7 @@ var ctx = context.Background() func main() { if err := godotenv.Load(); err != nil { - panic(err) + log.Println(".env not load") } redis.InitRedis() notification := consumer.NewNotificationConsumer(ctx) diff --git a/stack.yaml b/stack.yaml index 4129deb..648b222 100644 --- a/stack.yaml +++ b/stack.yaml @@ -3,13 +3,10 @@ version: "3.8" services: notification: image: jscorptech/taxi-notification:latest + env_file: + - /opt/env/.notification networks: - taxi - ports: - - mode: ingress - target: 8000 - published: ${PORT:?PORT not found} - protocol: tcp deploy: mode: replicated restart_policy: @@ -17,10 +14,10 @@ services: update_config: parallelism: 1 order: start-first - failure-action: rollback + failure_action: rollback monitor: 10s delay: 10s - max_failure_ration: 0.2 + max_failure_ratio: 0.2 resources: limits: cpus: "2"