Browse Source

CI - publish web samples to gh-pages.

Yao Wei Tjong 姚伟忠 5 years ago
parent
commit
f9ba9ada78
3 changed files with 57 additions and 2 deletions
  1. 4 1
      .github/workflows/main.yml
  2. 48 1
      rakefile
  3. 5 0
      script/.env-file

+ 4 - 1
.github/workflows/main.yml

@@ -144,8 +144,11 @@ jobs:
       - name: Publish
         env:
           GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+          PUBLISHER_NAME: ${{ secrets.PUBLISHER_NAME }}
+          PUBLISHER_EMAIL: ${{ secrets.PUBLISHER_EMAIL }}
+          PUBLISHER_TOKEN: ${{ secrets.PUBLISHER_TOKEN }}
         run: script/dockerized.sh ${PLATFORM/-*} rake publish
-        if: github.event_name == 'push' && matrix.platform == 'android'
+        if: github.event_name == 'push' && (matrix.platform == 'android' || (matrix.platform == 'web' && matrix.build-type == 'rel'))
   macOS:
     name: 🍏
     runs-on: macos-latest

+ 48 - 1
rakefile

@@ -116,7 +116,9 @@ desc 'Publish build artifact'
 task :publish => [:init] do
   if ENV['PLATFORM'] == 'android'
     Rake::Task[:gradle].invoke('publish')
+    next
   end
+  Rake::Task[$publish_task.to_sym].invoke if $publish_task
 end
 
 desc 'Create a new project'
@@ -183,17 +185,62 @@ task :ci do
   else
     # Do nothing
   end
+  case ENV['PLATFORM']
+  when 'web'
+    ENV['EMSCRIPTEN_SHARE_DATA'] = '1'
+    $publish_task = 'ci_publish_web'
+  else
+    # Do nothing
+  end
   ENV['URHO3D_LIB_TYPE'] = ENV['LIB_TYPE'].upcase if /static|shared/ =~ ENV['LIB_TYPE']
   ENV['URHO3D_TESTING'] = '1' if /linux|macOS|win/ =~ ENV['PLATFORM']
   ENV['URHO3D_LINT'] = '1' if ENV['MODIFIER'] == 'clang-tidy'
   ENV['URHO3D_STYLE'] = '1' if ENV['MODIFIER'] == 'clang-format'
   # Enable all the bells and whistles
   %w[URHO3D_DATABASE_SQLITE URHO3D_EXTRAS].each { |it| ENV[it] = '1' }
-  if ENV['USE_CCACHE'] && /\[cache clear\]/ =~ `git log --format=%B -n 1`
+  if ENV['USE_CCACHE'] && /\[cache clear\]/ =~ `git log --format=%B -n1`
     system 'bash', '-c', 'rm -rf ~/.{ccache,gradle}' or abort 'Failed to clear the build cache'
   end
 end
 
+task :ci_publish_web do
+  require 'json'
+  system 'git clone --depth 1 -q https://github.com/urho3d/urho3d.github.io.git build/urho3d.github.io' or abort 'Failed to clone urho3d/urho3d.github.io'
+  system "rsync -a --delete --exclude tool --exclude *.pak --exclude index.md build/ci/bin/ build/urho3d.github.io/samples" or abort 'Failed to rsync Web samples'
+  Dir.chdir('build/urho3d.github.io/samples') {
+    next unless system 'git diff --quiet Urho3D.js.data'
+    uuid = `git diff --color=never --word-diff-regex='\\w+' --word-diff=porcelain Urho3D.js`.split.grep(/^[+-]\w+-/).map { |it| it[0] = ''; it }
+    system %Q(ruby -i.bak -pe "gsub '#{uuid.last}', '#{uuid.first}'" Urho3D.js) or abort 'Failed to substitute UUID'
+    if system 'git diff --quiet Urho3D.js'
+      File.unlink 'Urho3D.js.bak'
+      Dir['*.js'].each { |file| system %Q(ruby -i -pe "gsub '#{uuid.last}', '#{uuid.first}'" #{file}) }
+    else
+      File.rename 'Urho3D.js.bak', 'Urho3D.js'
+    end
+  }
+  web = {'samples' => {}}
+  Dir.chdir('build/urho3d.github.io/samples') { web['samples']['Native'] = Dir['*.html'].sort }
+  web['player'] = web['samples']['Native'].pop # Assume the last sample after sorting is the Urho3DPlayer.html
+  {'AngelScript' => 'Scripts', 'Lua' => 'LuaScripts'}.each { |lang, subdir|
+    Dir.chdir("bin/Data/#{subdir}") {
+      script_samples = Dir['[0-9]*'].sort
+      deleted_samples = [] # Delete samples that do not have their native counterpart
+      script_samples.each { |sample| deleted_samples.push sample unless web['samples']['Native'].include? "#{sample.split('.').first}.html" }
+      web['samples'][lang] = (script_samples - deleted_samples).map { |sample| "#{subdir}/#{sample}" }
+    }
+  }
+  File.open('build/urho3d.github.io/_data/web.json', 'w') { |file| file.puts web.to_json }
+  system %Q{
+    cd build/urho3d.github.io && \\
+    git config user.name #{ENV['PUBLISHER_NAME']} && \\
+    git config user.email #{ENV['PUBLISHER_EMAIL']} && \\
+    git remote set-url --push origin https://#{ENV['PUBLISHER_TOKEN']}@github.com/urho3d/urho3d.github.io.git && \\
+    git add -A . && \\
+    ( git commit -qm "GH Actions: Web samples update at #{Time.now.utc}.\n\nCommit: https://github.com/#{ENV['GITHUB_REPOSITORY']}/commit/#{ENV['GITHUB_SHA']}\n\nMessage: #{`git log --format=%B -n 1`}" || true) && \\
+    git push -q >/dev/null 2>&1
+  } or abort 'Failed to update Web samples'
+end
+
 task :source_checksum do
   require 'digest'
   sha256_final = Digest::SHA256.new

+ 5 - 0
script/.env-file

@@ -27,6 +27,8 @@ EMSCRIPTEN_SYSROOT
 EMSCRIPTEN_TOTAL_MEMORY
 GENERATOR
 GITHUB_ACTOR
+GITHUB_REPOSITORY
+GITHUB_SHA
 GITHUB_TOKEN
 GRAPHICS_API
 HOST
@@ -40,6 +42,9 @@ MODIFIER
 OPENSSL_ROOT_DIR
 OS
 PLATFORM
+PUBLISHER_EMAIL
+PUBLISHER_NAME
+PUBLISHER_TOKEN
 RPI
 RPI_ABI
 RPI_PREFIX