Discourse Theme with pnpm and yarn #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Discourse Theme with pnpm and yarn | |
on: | |
workflow_dispatch: | |
workflow_call: | |
inputs: | |
repository: | |
type: string | |
required: false | |
core_ref: | |
type: string | |
required: false | |
secrets: | |
ssh_private_key: | |
description: "Optional SSH private key to be used when cloning additional plugin repos" | |
required: false | |
concurrency: | |
group: discourse-theme-${{ format('{0}-{1}-{2}', github.head_ref || github.run_number, github.job, inputs.core_ref) }} | |
cancel-in-progress: true | |
jobs: | |
linting: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
repository: ${{ inputs.repository }} | |
- name: Determine JS package manager | |
id: js-pkg-manager | |
run: | | |
if [ -f yarn.lock ]; then | |
echo "Using Yarn" | |
echo "manager=yarn" >> $GITHUB_OUTPUT | |
else | |
echo "Using pnpm" | |
echo "manager=pnpm" >> $GITHUB_OUTPUT | |
fi | |
- name: Install package manager | |
run: npm install -g ${{ steps.js-pkg-manager.outputs.manager }} | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 20 | |
cache: ${{ steps.js-pkg-manager.outputs.manager }} | |
- name: Install dependencies | |
run: ${{ steps.js-pkg-manager.outputs.manager }} install --frozen-lockfile | |
- name: ESLint | |
if: ${{ !cancelled() }} | |
run: | | |
if test -f .prettierrc.cjs; then | |
${{ steps.js-pkg-manager.outputs.manager }} eslint --ext .js,.gjs,.js.es6 --no-error-on-unmatched-pattern {test,javascripts} | |
else | |
${{ steps.js-pkg-manager.outputs.manager }} eslint --ext .js,.js.es6 --no-error-on-unmatched-pattern {test,javascripts} | |
fi | |
- name: Prettier | |
if: ${{ !cancelled() }} | |
shell: bash | |
run: | | |
${{ steps.js-pkg-manager.outputs.manager }} prettier --version | |
files=$(find javascripts desktop mobile common scss -type f \( -name "*.scss" -or -name "*.js" -or -name "*.gjs" -or -name "*.es6" \) 2> /dev/null) || true | |
if [ -n "$files" ]; then | |
${{ steps.js-pkg-manager.outputs.manager }} prettier --check $files | |
fi | |
if [ 0 -lt "$(find test -type f \( -name '*.js' -or -name '*.gjs' -or -name '*.es6' \) 2> /dev/null | wc -l)" ]; then | |
${{ steps.js-pkg-manager.outputs.manager }} prettier --check "test/**/*.{js,gjs,es6}" | |
fi | |
- name: Ember template lint | |
if: ${{ !cancelled() }} | |
run: ${{ steps.js-pkg-manager.outputs.manager }} ember-template-lint --no-error-on-unmatched-pattern javascripts | |
check_for_tests: | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.check_tests.outputs.matrix }} | |
has_tests: ${{ steps.check_tests.outputs.has_tests }} | |
has_compatibility_file: ${{ steps.check_tests.outputs.has_compatibility_file }} | |
has_specs: ${{ steps.check_tests.outputs.has_specs }} | |
has_system_specs: ${{ steps.check_tests.outputs.has_system_specs }} | |
steps: | |
- name: Install component | |
uses: actions/checkout@v4 | |
with: | |
repository: ${{ inputs.repository }} | |
path: tmp/component | |
fetch-depth: 1 | |
- name: Check For Test Types | |
id: check_tests | |
shell: ruby {0} | |
working-directory: tmp/component | |
run: | | |
require 'json' | |
has_system_specs = Dir.glob("spec/system/**/*.rb").any? | |
has_specs = Dir.glob("spec/**/*.rb").any? | |
has_compatibility_file = File.exist?(".discourse-compatibility") | |
has_locale_files = File.exist?("locales/en.yml") | |
matrix = [] | |
matrix << 'frontend' if Dir.glob("test/**/*.{js,es6,gjs}").any? | |
matrix << 'rspec' if has_specs || has_compatibility_file || has_locale_files | |
puts "Running jobs: #{matrix.inspect}" | |
File.write(ENV["GITHUB_OUTPUT"], "matrix=#{matrix.to_json}\n", mode: 'a+') | |
File.write(ENV["GITHUB_OUTPUT"], "has_specs=true\n", mode: 'a+') if has_specs | |
File.write(ENV["GITHUB_OUTPUT"], "has_system_specs=true\n", mode: 'a+') if has_system_specs | |
File.write(ENV["GITHUB_OUTPUT"], "has_compatibility_file=true\n", mode: 'a+') if has_compatibility_file | |
if matrix.any? | |
File.write(ENV["GITHUB_OUTPUT"], "has_tests=true\n", mode: 'a+') | |
end | |
test: | |
name: ${{ matrix.build_type || '' }}_tests | |
needs: check_for_tests | |
if: ${{ needs.check_for_tests.outputs.has_tests }} | |
runs-on: ubuntu-latest | |
container: discourse/discourse_test:slim-browsers | |
timeout-minutes: 15 | |
strategy: | |
fail-fast: false | |
matrix: | |
build_type: ${{ fromJSON(needs.check_for_tests.outputs.matrix) }} | |
env: | |
DISCOURSE_HOSTNAME: www.example.com | |
RUBY_GLOBAL_METHOD_CACHE_SIZE: 131072 | |
PGUSER: discourse | |
PGPASSWORD: discourse | |
RAILS_ENV: test | |
DISCOURSE_DEV_DB: discourse_test | |
CHEAP_SOURCE_MAPS: "1" | |
steps: | |
- name: Set working directory owner | |
run: chown root:root . | |
- uses: actions/checkout@v4 | |
with: | |
repository: discourse/discourse | |
fetch-depth: 1 | |
ref: ${{ inputs.core_ref }} | |
- name: Install component | |
uses: actions/checkout@v4 | |
with: | |
repository: ${{ inputs.repository }} | |
path: tmp/component | |
fetch-depth: 1 | |
- name: Setup Git | |
run: | | |
git config --global user.email "[email protected]" | |
git config --global user.name "Discourse CI" | |
- name: Clone additional plugins | |
uses: discourse/.github/actions/clone-additional-plugins@v1 | |
with: | |
ssh_private_key: ${{ secrets.ssh_private_key }} | |
about_json_path: tmp/component/about.json | |
- name: Start redis | |
run: | | |
redis-server /etc/redis/redis.conf & | |
- name: Start Postgres | |
run: | | |
chown -R postgres /var/run/postgresql | |
sudo -E -u postgres script/start_test_db.rb | |
sudo -u postgres psql -c "CREATE ROLE $PGUSER LOGIN SUPERUSER PASSWORD '$PGPASSWORD';" | |
- name: Container envs | |
id: container-envs | |
run: | | |
echo "ruby_version=$RUBY_VERSION" >> $GITHUB_OUTPUT | |
echo "debian_release=$DEBIAN_RELEASE" >> $GITHUB_OUTPUT | |
shell: bash | |
- name: Bundler cache | |
uses: actions/cache@v4 | |
with: | |
path: vendor/bundle | |
key: ${{ runner.os }}-${{ steps.container-envs.outputs.ruby_version }}-${{ steps.container-envs.outputs.debian_release }}-gem-${{ hashFiles('**/Gemfile.lock') }} | |
restore-keys: | | |
${{ runner.os }}-${{ steps.container-envs.outputs.ruby_version }}-${{ steps.container-envs.outputs.debian_release }}-gem- | |
- name: Setup gems | |
run: | | |
gem install bundler --conservative -v $(awk '/BUNDLED WITH/ { getline; gsub(/ /,""); print $0 }' Gemfile.lock) | |
bundle config --local path vendor/bundle | |
bundle config --local deployment true | |
bundle config --local without development | |
bundle install --jobs 4 | |
bundle clean | |
- name: Lint English locale | |
run: bundle exec ruby script/i18n_lint.rb "tmp/component/locales/en.yml" | |
- name: Get yarn cache directory | |
id: yarn-cache-dir | |
run: if [ -f yarn.lock ]; then echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT; fi | |
- name: Yarn cache | |
uses: actions/cache@v4 | |
id: yarn-cache | |
if: ${{ steps.yarn-cache-dir.outputs.dir }} | |
with: | |
path: ${{ steps.yarn-cache-dir.outputs.dir }} | |
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} | |
restore-keys: | | |
${{ runner.os }}-yarn- | |
- name: Install JS dependencies | |
run: if [ -f yarn.lock ]; then yarn install --frozen-lockfile; else pnpm install --frozen-lockfile; fi | |
- name: Fetch app state cache | |
uses: actions/cache@v4 | |
id: app-cache | |
with: | |
path: tmp/app-cache | |
key: >- | |
${{ hashFiles('.github/workflows/tests.yml') }}- | |
${{ hashFiles('db/**/*', 'plugins/**/db/**/*') }}- | |
${{ env.RAILS_ENV }} | |
- name: Restore database from cache | |
if: steps.app-cache.outputs.cache-hit == 'true' | |
run: | | |
if test -f script/silence_successful_output; then | |
script/silence_successful_output psql -f tmp/app-cache/cache.sql postgres | |
else | |
psql -f tmp/app-cache/cache.sql postgres | |
fi | |
- name: Restore uploads from cache | |
if: steps.app-cache.outputs.cache-hit == 'true' | |
run: rm -rf public/uploads && cp -r tmp/app-cache/uploads public/uploads | |
- name: Create and migrate database | |
if: steps.app-cache.outputs.cache-hit != 'true' | |
run: | | |
bin/rake db:create | |
if test -f script/silence_successful_output; then | |
script/silence_successful_output bin/rake db:migrate | |
else | |
bin/rake db:migrate | |
fi | |
- name: Dump database for cache | |
if: steps.app-cache.outputs.cache-hit != 'true' | |
run: mkdir -p tmp/app-cache && pg_dumpall > tmp/app-cache/cache.sql | |
- name: Dump uploads for cache | |
if: steps.app-cache.outputs.cache-hit != 'true' | |
run: rm -rf tmp/app-cache/uploads && cp -r public/uploads tmp/app-cache/uploads | |
- name: Validate discourse-compatibility | |
if: matrix.build_type == 'rspec' && needs.check_for_tests.outputs.has_compatibility_file && !inputs.core_ref | |
run: bin/rake "compatibility:validate[tmp/component/.discourse-compatibility]" | |
- name: Ember Build for System Tests | |
if: matrix.build_type == 'rspec' && needs.check_for_tests.outputs.has_system_specs | |
run: bin/ember-cli --build | |
- name: Theme RSpec Tests | |
if: matrix.build_type == 'rspec' && needs.check_for_tests.outputs.has_specs | |
env: | |
CAPYBARA_DEFAULT_MAX_WAIT_TIME: 10 | |
LOAD_PLUGINS: 1 | |
run: bin/rspec --format documentation tmp/component/spec | |
timeout-minutes: 10 | |
- name: Upload failed system test screenshots | |
uses: actions/upload-artifact@v3 | |
if: matrix.build_type == 'rspec' && needs.check_for_tests.outputs.has_system_specs && failure() | |
with: | |
name: failed-system-test-screenshots | |
path: tmp/capybara/*.png | |
- name: Create theme archive | |
if: matrix.build_type == 'frontend' | |
run: | | |
cd tmp/component | |
git archive --format=tar.gz HEAD > ../../theme.tar.gz | |
- name: Component QUnit | |
if: matrix.build_type == 'frontend' | |
env: | |
# Cloning components via git in the test env will fail because FinalDestination will return a fake IP | |
# If we need this functionality in future, we'll need some refactoring in core. | |
UPDATE_COMPONENTS: 0 | |
run: | | |
THEME_NAME=$(ruby -e 'require "json"; puts JSON.parse(File.read("tmp/component/about.json"))["name"]') | |
THEME_ARCHIVE=theme.tar.gz bundle exec rake themes:install:archive | |
UNICORN_TIMEOUT=120 bundle exec rake "themes:qunit[name,$THEME_NAME]" | |
timeout-minutes: 10 | |