نحن نطبق إجراءات github لـ CI والنشر التلقائي على npm

الإجراءات جيثب


مرحبا يا هبر. تتناول هذه المقالة كيفية أتمتة مشروعك بإجراءات github.
سيتم تقسيم الأتمتة إلى:


  • CI - سيتم إجراء الاختبارات على كل التزام
  • نشر - عند الإصدار ، ننشر الإصدار في npm

CI


في مستودعنا (كيف وصلنا إلى هذا يمكن قراءة هنا ) لأن هناك النصوص التالية:


  • npm run build - بناء
  • npm test - الاختبارات
  • npm run codestyle - التحقق من نمط الكود
    سنقوم بتشغيل الإنشاءات والاختبارات على 3 إصدارات من العقدة (8 ، 10 ، 12) وفحص نمط الكود في نفس الوقت
    سيكون ملف .github/workflows/CI.yml مسؤولاً عن سير العمل هذا:

 name: Node CI on: push jobs: buildAndTest: runs-on: ubuntu-latest strategy: matrix: node-version: [8.x, 10.x, 12.x] steps: - uses: actions/checkout@v1 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - name: Install, build, test run: | npm install npm run build npm test env: CI: true checkCodestyle: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: '8.x' - name: Install and check codestyle run: | npm install npm run codestyle env: CI: true 

دعنا نحلل محتوياته
name: Node CI string التي تحتوي على اسم سير العمل
on: push سنقوم بتشغيل سير العمل على الضغط
jobs: هذه هي المهمة التي يتعين إكمالها


بناء الوظيفة و buildAndTest :


 buildAndTest: #  runs-on: ubuntu-latest #     strategy: #    matrix: #       node-version: [8.x, 10.x, 12.x] #   steps: #   - uses: actions/checkout@v1 - name: Use Node.js ${{ matrix.node-version }} #   uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - name: Install, build, test #    run: | npm install npm run build npm test env: #    CI   CI: true 

يمكنك أن تلاحظ matrix.node-version مثيرًا للإهتمام ${{ something }} أسميه استدعاء السياق هذا ، حيث something هناك تعبير ما ، في حالتنا matrix.node-version ، أي نحصل على قيمة في كائن matrix المسمى node-version . يقوم matrix بتخزين قيمة المتغيرات المحددة في المدى الحالي ، في حالتنا ، مع ثلاث عمليات بدء ، 12.x القيم 8.x و 12.x و 12.x هناك


CheckCodestyle وظيفة


 checkCodestyle: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: '8.x' - name: Install and check codestyle run: | npm install npm run codestyle env: CI: true 

حسنا ، هنا كل شيء يشبه الأول


نشر


 name: Publish #  workflow on: release #   ( ) jobs: #  test: # ,    runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions/setup-node@v1 with: node-version: 12 - run: npm ci - run: npm run build - run: npm test - run: npm run codestyle publish: #  needs: test #       runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions/setup-node@v1 with: node-version: 12 #   12  registry-url: https://registry.npmjs.org/ - run: npm run build #   - name: Publish beta #   if: github.event.prerelease == true #        run: npm publish --tag beta env: NODE_AUTH_TOKEN: ${{secrets.npm}} - name: Publish stable #    if: github.event.prerelease == false #     run: npm publish --tag beta env: NODE_AUTH_TOKEN: ${{secrets.npm}} - name: Build and Deploy ds #   uses: JamesIves/github-pages-deploy-action@master if: github.event.prerelease == false env: ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} BRANCH: gh-pages FOLDER: docs BUILD_SCRIPT: npm install && npm run typedoc ##     

للاهتمام هنا ، ${{ secrets.SOMETHING }} هو دعوة لعنصر SOMETHING في كائن SOMETHING
وأشير هنا إلى معنيين سريين:


  • npm - رمز NPM
  • ACCESS_TOKEN - الوصول إلى صفحات gh (رمز github)

أسرار يمكن أن تنشأ في إعدادات المشروع


وليس أقل خطوط مثيرة للاهتمام هي


 if: github.event.prerelease == true if: github.event.prerelease == false 

إذا كان يسمح لك بإخبار جيثب الخطوات التي يجب اتخاذها والتي لا.
للقيام بذلك ، نأخذ القيمة github.event.prerelease ، github.event يحتوي على كائن json الذي تم استلامه في إصدار webhook ، وإذا كان هذا true فسننشر النسخة التجريبية ،
إذا كان false فإننا ننشر مستقر ونرسي


هذه هي الطريقة البسيطة لأتمتة كل شيء باستخدام إجراءات github


مراجع


مستودع النهائي
مكتبة Npm
مثال التوثيق


وثائق الإجراءات جيثب
العمل مع السياق
العمل مع القيم السرية
وصف الافراج عن webhook

Source: https://habr.com/ru/post/ar468729/


All Articles