Github actions, des fois c'est bien

Ah y’est ! Après m’être bien arraché ce qui me restait de cheveux hier, j’ai enfin une tâche Github action automagique qui fonctionne \o/

Le code source si ça peut vous aider :

name: Update dcstore.xml file if necessary

on:
  release:
    types:
      - released
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "dcstore"
  dcstore:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Setup PHP
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.0'

      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      # Back to default branche
      - name: Back to default branch of repository
        shell: bash
        run: |
          echo "git fetch && git switch ${{github.event.repository.default_branch}}"
          git fetch
          git switch ${{github.event.repository.default_branch}}

      # Run PHP code
      - name: Run PHP code
        id: getversion
        shell: php {0}
        run: |
          <?php
          $version = '';
          $dcmin = '2.0';
          if (file_exists('dcstore.xml')) {
            $df = file_get_contents('./_define.php');
            if (preg_match('/registerModule\((.*?),(.*?)[\'"],(.*?)[\'"],(.*?)[\'"](.*?)[\'"](.*?)(,.*)\)/s',$df,$matches)) {
              if (isset($matches[5])) {
                $version = $matches[5];
                if (isset($matches[7])) {
                  $str = $matches[7];
                  if (preg_match('/\[(.*?)[\'"]core[\'"](.*?),(.*?)[\'"](.*?)[\'"](.*?)\]/s',$str,$submatches)) {
                    $dcmin = $submatches[4];
                  }
                }
              }
            }
            if ($version !== '') {
              $ds = file_get_contents('dcstore.xml');
              if ($ds) {
                $ds = preg_replace('/<version>(.*?)<\/version>/s',"<version>$version</version>",$ds);
                $ds = preg_replace('/download\/(.*?)\//s',"download/$version/",$ds);
                $ds = preg_replace('/(.*)-(.*?).zip/s',"$1-$version.zip",$ds);
                $ds = preg_replace('/<da:dcmin>(.*?)<\/da:dcmin>/s',"<da:dcmin>$dcmin</da:dcmin>",$ds);
                if ($ds) {
                  file_put_contents('dcstore.xml',$ds);
                }
              }
            }
          }
          echo "::set-output name=module_version::$version\n";
          echo "::set-output name=module_dcmin::$dcmin\n";

      # Cope with returned versions
      - name: Log output of the script
        shell: bash
        run: echo "${{steps.getversion.outputs.module_version}} (Dotclear ${{steps.getversion.outputs.module_dcmin}}+)"

      # Log default branch
      - name: Log output the default branch
        shell: bash
        run: echo "The default branch is ${{github.event.repository.default_branch}}"

      # Update dcstore.xml if necessary
      - name: Update dcstore
        id: update-dcstore
        shell: bash
        run: |
          cat dcstore.xml
          test=$(git diff --name-only -- dcstore.xml)
          if [[ "$test" != "" ]]; then
            echo "dcstore.xml modified, need to be commit"
            echo "set credentials"
            git config user.email "carnet.franck.paul@gmail.com"
            git config user.name "franck-paul"
            echo "git add dcstore.xml"
            git add dcstore.xml
            echo "git commit"
            git commit -m "Update dcstore.xml - version ${{steps.getversion.outputs.module_version}} - Dotclear ${{steps.getversion.outputs.module_dcmin}}+"
            echo "git push"
            git push
          else
            echo "nothing to do"
          fi

      # Notification via Telegram
      - name: Send Telegram Message Ok
        uses: appleboy/telegram-action@master
        env:
          GITHUB_CONTEXT: ${{toJSON(github)}}
        with:
          to: ${{ secrets.TELEGRAM_ID }}
          token: ${{ secrets.TELEGRAM_TOKEN }}
          format: markdown
          message: |
            → Update *dcstore.xml* file if necessary
            Version: *${{steps.getversion.outputs.module_version}}* (Dotclear ${{steps.getversion.outputs.module_dcmin}}+)
            Branch: *${{ github.ref }}*
            Repository: *${{github.repository}}*
            Owner: *${{github.repository_owner}}*
            🍺

Je vais enfin pouvoir passer à autre chose… Après avoir déployé ça partout, à moins que…

Ajouter un commentaire

Les commentaires peuvent être formatés en utilisant la syntaxe Markdown Extra.

Ajouter un rétrolien

URL de rétrolien : https://open-time.net/trackback/15155

Haut de page