Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Update action to rely on Shopify CLI 3.x + Theme Access app #52

Closed
wants to merge 12 commits into from
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
name: CI

on: [push]
on:
push:
branches:
- main

jobs:
check_dawn:
Expand Down
32 changes: 31 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
FROM cpclermont/lighthouse-ci-action:1.0.0
FROM ruby:3

# Install dependencies
RUN apt-get update \
&& apt-get -y install sudo jq chromium

# Use latest bundler version so that Shopify CLI does not complain
RUN gem install bundler -N

# Install Shopify CLI 2.x
RUN gem install shopify-cli -N

# Install Node.js
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
RUN apt-get install -y nodejs

# See https://github.com/nodejs/docker-node/blob/main/docs/BestPractices.md#global-npm-dependencies
# for reasons why we need to set `npm_config_prefix`.
ENV npm_config_prefix="${GITHUB_WORKSPACE:-/home}/.node"
ENV PATH="$npm_config_prefix:${PATH}"

# Install Shopify CLI 3.x
RUN npm install -g @shopify/cli @shopify/theme puppeteer @lhci/cli

# Chrome in Docker fix
# Install latest fonts to support major charsets (Chinese, Japanese, Arabic, Hebrew, Thai and a few others)
# Note: this installs the necessary libs to make the bundled version of Chromium that Puppeteer installs, work.
RUN apt-get install -y fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*

COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
51 changes: 26 additions & 25 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
# delete the code below. Everything else is platform independent.
#
# Here, we're translating the GitHub action input arguments into environment variables
# for this scrip to use.
# for this script to use.
[[ -n "$INPUT_STORE" ]] && export SHOP_STORE="$INPUT_STORE"
[[ -n "$INPUT_PASSWORD" ]] && export SHOP_PASSWORD="$INPUT_PASSWORD"
[[ -n "$INPUT_PRODUCT_HANDLE" ]] && export SHOP_PRODUCT_HANDLE="$INPUT_PRODUCT_HANDLE"
[[ -n "$INPUT_COLLECTION_HANDLE" ]] && export SHOP_COLLECTION_HANDLE="$INPUT_COLLECTION_HANDLE"
[[ -n "$INPUT_THEME_ROOT" ]] && export THEME_ROOT="$INPUT_THEME_ROOT"

# Authentication creds
export SHOP_ACCESS_TOKEN="$INPUT_ACCESS_TOKEN"
export SHOPIFY_CLI_THEME_TOKEN="$INPUT_SHOPIFY_CLI_THEME_TOKEN"

# Authentication creds (deprecated)
export SHOP_ACCESS_TOKEN="$INPUT_ACCESS_TOKEN"
[[ -n "$INPUT_APP_ID" ]] && export SHOP_APP_ID="$INPUT_APP_ID"
[[ -n "$INPUT_APP_PASSWORD" ]] && export SHOP_APP_PASSWORD="$INPUT_APP_PASSWORD"

Expand All @@ -34,7 +35,13 @@ export SHOP_ACCESS_TOKEN="$INPUT_ACCESS_TOKEN"
[[ -n "$INPUT_LHCI_MIN_SCORE_ACCESSIBILITY" ]] && export LHCI_MIN_SCORE_ACCESSIBILITY="$INPUT_LHCI_MIN_SCORE_ACCESSIBILITY"

# Add global node bin to PATH (from the Dockerfile)
export PATH="$PATH:$npm_config_prefix/bin"
if [[ -n "$SHOPIFY_CLI_THEME_TOKEN" ]]; then
# Use Shopify CLI 3.x
export PATH="$npm_config_prefix/bin:$PATH"
else
# Use Shopify CLI 2.x
export PATH="$PATH:$npm_config_prefix/bin"
fi

# END of GitHub Action Specific Code
####################################################################
Expand All @@ -53,12 +60,6 @@ step() {
EOF
}

is_installed() {
# This works with scripts and programs. For more info, check
# http://goo.gl/B9683D
type $1 &> /dev/null 2>&1
}

api_request() {
local url="$1"
local err="$(mktemp)"
Expand Down Expand Up @@ -118,38 +119,27 @@ cleanup() {

trap 'cleanup $?' EXIT

if ! is_installed lhci; then
Copy link
Contributor Author

@Poitrin Poitrin Nov 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't imagine any case when lhci and shopify are not already installed, as this entrypoint.sh script is based on a Dockerfile, in which both tools are installed.

Moreover, if you take a look at the Dockerfile.base, @lhci/cli@0.8.x is installed, so the command here is even outdated.
As I was initially confused and changing the command in this entrypoint.sh file, I decided to remove these lines.

step "Installing Lighthouse CI"
log npm install -g @lhci/cli@0.7.x puppeteer
npm install -g @lhci/cli@0.7.x puppeteer
fi

if ! is_installed shopify; then
step "Installing Shopify CLI"
log "gem install shopify"
gem install shopify
fi

step "Configuring shopify CLI"

# Disable analytics
# Disable analytics for CLI 2.x
mkdir -p ~/.config/shopify && cat <<-YAML > ~/.config/shopify/config
[analytics]
enabled = false
YAML
# Disable analytics for CLI 3.x, see https://shopify.dev/themes/tools/cli#usage-reporting
export SHOPIFY_CLI_NO_ANALYTICS=1

# Secret environment variable that turns shopify CLI into CI mode that accepts environment credentials
export CI=1
export SHOPIFY_SHOP="${SHOP_STORE#*(https://|http://)}"
export SHOPIFY_FLAG_STORE=$SHOPIFY_SHOP

if [[ -n "$SHOP_ACCESS_TOKEN" ]]; then
export SHOPIFY_PASSWORD="$SHOP_ACCESS_TOKEN"
else
export SHOPIFY_PASSWORD="$SHOP_APP_PASSWORD"
fi

shopify login

host="https://${SHOP_STORE#*(https://|http://)}"
theme_root="${THEME_ROOT:-.}"

Expand All @@ -160,7 +150,16 @@ log "Will run Lighthouse CI on $host"

step "Creating development theme"
theme_push_log="$(mktemp)"
shopify theme push --development --json $theme_root > "$theme_push_log" && cat "$theme_push_log"
if [[ -n "$SHOPIFY_CLI_THEME_TOKEN" ]]; then
# Use Shopify CLI 3.x
# In 3.x, you don't need to log in explicitly. If you aren't logged in,
# then you're prompted to log in when you run a command that requires authentication.
shopify theme push --development --json --path "$theme_root" | tee "$theme_push_log"
else
# Use Shopify CLI 2.x
shopify login
shopify theme push --development --json "$theme_root" | tee "$theme_push_log"
fi
preview_url="$(cat "$theme_push_log" | tail -n 1 | jq -r '.theme.preview_url')"
preview_id="$(cat "$theme_push_log" | tail -n 1 | jq -r '.theme.id')"

Expand Down Expand Up @@ -189,9 +188,11 @@ query_string="?preview_theme_id=${preview_id}&_fd=0&pb=0"
min_score_performance="${LHCI_MIN_SCORE_PERFORMANCE:-0.6}"
min_score_accessibility="${LHCI_MIN_SCORE_ACCESSIBILITY:-0.9}"


cat <<- EOF > lighthouserc.yml
ci:
collect:
chromePath: "$(which chromium)"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When puppeteerScript is given but no chromePath,
Lighthouse CI was shouting "Chrome not found".
Therefore, I set the path to chromium manually.

url:
- "$host/$query_string"
- "$host/products/$product_handle$query_string"
Expand Down