name: Build OpenCart Extension on: push: branches: - main tags: - "v*" workflow_dispatch: inputs: publish_release: description: "Publish or replace the release for this version" required: true type: boolean default: false jobs: build: name: Validate and package extension runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: Install dependencies run: | sudo apt-get update sudo apt-get install -y \ jq \ libxml2-utils \ zip \ unzip \ php-cli - name: Validate and build package id: package shell: bash run: | set -Eeuo pipefail CONFIG=".oc-extension.json" if [[ ! -f "$CONFIG" ]]; then echo "::error::Missing .oc-extension.json" exit 1 fi PLATFORM="$(jq -er '.platform' "$CONFIG")" CODE="$(jq -er '.code' "$CONFIG")" if [[ ! "$CODE" =~ ^[a-z0-9_]+$ ]]; then echo "::error::Invalid extension code: $CODE" exit 1 fi mkdir -p build/package case "$PLATFORM" in oc3) echo "Target platform: OpenCart 3" if [[ ! -f install.xml ]]; then echo "::error::OC3 requires install.xml" exit 1 fi if [[ -f install.json ]]; then echo "::error::OC3 repository must not contain install.json" exit 1 fi if [[ ! -d upload ]]; then echo "::error::OC3 requires an upload/ directory" exit 1 fi xmllint --noout install.xml XML_CODE="$(xmllint --xpath \ 'string(/modification/code)' install.xml)" VERSION="$(xmllint --xpath \ 'string(/modification/version)' install.xml)" if [[ "$XML_CODE" != "$CODE" ]]; then echo "::error::Configured code does not match install.xml" exit 1 fi if [[ -z "$VERSION" ]]; then echo "::error::Missing version in install.xml" exit 1 fi if [[ ! "$VERSION" =~ ^[0-9]+(\.[0-9]+){1,2}([.-][0-9A-Za-z.-]+)?$ ]]; then echo "::error::Invalid extension version: $VERSION" exit 1 fi EMPTY_PATHS="$(xmllint --xpath \ 'count(//file[not(normalize-space(@path))])' install.xml)" EMPTY_SEARCHES="$(xmllint --xpath \ 'count(//search[not(normalize-space(string(.)))])' install.xml)" EMPTY_ADDS="$(xmllint --xpath \ 'count(//add[not(normalize-space(string(.)))])' install.xml)" if [[ "$EMPTY_PATHS" != "0" ]]; then echo "::error::install.xml contains an empty file path" exit 1 fi if [[ "$EMPTY_SEARCHES" != "0" || "$EMPTY_ADDS" != "0" ]]; then echo "::error::install.xml contains an empty search or add operation" exit 1 fi cp install.xml build/package/ cp -a upload build/package/ for FILE in install.php install.sql; do if [[ -f "$FILE" ]]; then cp "$FILE" build/package/ fi done ;; oc4) echo "Target platform: OpenCart 4" if [[ ! -f install.json ]]; then echo "::error::OC4 requires install.json" exit 1 fi if [[ -f install.xml ]]; then echo "::error::OC4 repository must not contain install.xml" exit 1 fi jq -e ' (.name | type == "string" and length > 0) and (.version | type == "string" and length > 0) and (.author | type == "string" and length > 0) and (.link | type == "string" and length > 0) ' install.json >/dev/null VERSION="$(jq -er '.version' install.json)" if [[ ! "$VERSION" =~ ^[0-9]+(\.[0-9]+){1,2}([.-][0-9A-Za-z.-]+)?$ ]]; then echo "::error::Invalid extension version: $VERSION" exit 1 fi MIN_VERSION="$(jq -r '.minimum_version // "4.0.0"' "$CONFIG")" if [[ ! "$MIN_VERSION" =~ ^4\.[0-9]+\.[0-9]+$ ]]; then echo "::error::Invalid OC4 minimum_version: $MIN_VERSION" exit 1 fi if [[ "$MIN_VERSION" == "4.0.0" || "$MIN_VERSION" == "4.0.1" ]]; then if [[ -d ocmod ]]; then echo "::error::OC4.0.0/4.0.1 targets must not contain OCMOD XML" exit 1 fi fi FOUND_DIR=false for DIR in admin catalog system ocmod; do if [[ -d "$DIR" ]]; then cp -a "$DIR" build/package/ FOUND_DIR=true fi done if [[ "$FOUND_DIR" != "true" ]]; then echo "::error::No OC4 extension directories found" exit 1 fi cp install.json build/package/ if [[ -d ocmod ]]; then mapfile -t OCMOD_FILES < <( find ocmod -type f -name '*.ocmod.xml' -print ) if [[ ${#OCMOD_FILES[@]} -eq 0 ]]; then echo "::error::ocmod/ exists but contains no *.ocmod.xml files" exit 1 fi for XML_FILE in "${OCMOD_FILES[@]}"; do xmllint --noout "$XML_FILE" done fi ;; *) echo "::error::Unsupported platform: $PLATFORM" exit 1 ;; esac # PHP syntax validation while IFS= read -r -d '' PHP_FILE; do echo "Checking PHP: $PHP_FILE" php -l "$PHP_FILE" done < <(find build/package -type f -name '*.php' -print0) # Package files from the package root ZIP_NAME="${CODE}_v${VERSION}.ocmod.zip" mkdir -p dist ( cd build/package zip -qr "../../dist/$ZIP_NAME" . ) # ZIP integrity unzip -t "dist/$ZIP_NAME" # SHA-256 checksum ( cd dist sha256sum "$ZIP_NAME" > "$ZIP_NAME.sha256" ) echo "platform=$PLATFORM" >> "$GITHUB_OUTPUT" echo "code=$CODE" >> "$GITHUB_OUTPUT" echo "version=$VERSION" >> "$GITHUB_OUTPUT" echo "zip_name=$ZIP_NAME" >> "$GITHUB_OUTPUT" echo "Package created: dist/$ZIP_NAME" echo "Platform: $PLATFORM" echo "Version: $VERSION" - name: Upload build artifacts uses: actions/upload-artifact@v3 with: name: ${{ steps.package.outputs.code }}-${{ steps.package.outputs.platform }}-${{ steps.package.outputs.version }} path: | dist/*.ocmod.zip dist/*.sha256 if-no-files-found: error - name: Publish or replace Gitea release if: startsWith(github.ref, 'refs/tags/') || (github.event_name == 'workflow_dispatch' && inputs.publish_release) env: GITEA_SERVER: ${{ github.server_url }} GITEA_REPOSITORY: ${{ github.repository }} GITEA_TOKEN: ${{ secrets.OC3_EXTENSION_RELEASE_TOKEN }} VERSION: ${{ steps.package.outputs.version }} CODE: ${{ steps.package.outputs.code }} ZIP_NAME: ${{ steps.package.outputs.zip_name }} REF_NAME: ${{ github.ref_name }} EVENT_NAME: ${{ github.event_name }} COMMIT_SHA: ${{ github.sha }} shell: bash run: | set -Eeuo pipefail if [[ -z "$GITEA_TOKEN" ]]; then echo "::error::Missing OC3_EXTENSION_RELEASE_TOKEN secret" exit 1 fi TAG="v${VERSION}" # A tag-triggered release must match the extension metadata. if [[ "$EVENT_NAME" == "push" && "$REF_NAME" != "$TAG" ]]; then echo "::error::Git tag $REF_NAME does not match metadata version $TAG" exit 1 fi API="${GITEA_SERVER%/}/api/v1" OWNER="${GITEA_REPOSITORY%%/*}" REPO="${GITEA_REPOSITORY#*/}" AUTH_HEADER="Authorization: token ${GITEA_TOKEN}" RELEASE_URL="$API/repos/$OWNER/$REPO/releases/tags/$TAG" RESPONSE="$(mktemp)" PAYLOAD="$(mktemp)" # Check whether a release already exists. STATUS="$(curl -sS \ -H "$AUTH_HEADER" \ -o "$RESPONSE" \ -w '%{http_code}' \ "$RELEASE_URL")" RELEASE_NAME="${CODE} ${TAG}" if [[ "$STATUS" == "200" ]]; then RELEASE_ID="$(jq -er '.id' "$RESPONSE")" echo "Existing release found. Replacing its assets." # Update release metadata. jq -n \ --arg name "$RELEASE_NAME" \ --arg body "Automated OpenCart extension release." \ '{name: $name, body: $body}' > "$PAYLOAD" curl -fsS -X PATCH \ -H "$AUTH_HEADER" \ -H "Content-Type: application/json" \ --data-binary "@$PAYLOAD" \ "$API/repos/$OWNER/$REPO/releases/$RELEASE_ID" \ >/dev/null # Delete old assets before uploading new ones. jq -r '.assets[]?.id' "$RESPONSE" | while read -r ASSET_ID; do echo "Deleting old release asset: $ASSET_ID" curl -fsS -X DELETE \ -H "$AUTH_HEADER" \ "$API/repos/$OWNER/$REPO/releases/$RELEASE_ID/assets/$ASSET_ID" \ >/dev/null done elif [[ "$STATUS" == "404" ]]; then echo "Creating new release: $TAG" jq -n \ --arg tag "$TAG" \ --arg name "$RELEASE_NAME" \ --arg body "Automated OpenCart extension release." \ --arg commit "$COMMIT_SHA" \ '{ tag_name: $tag, target_commitish: $commit, name: $name, body: $body, draft: false, prerelease: false }' > "$PAYLOAD" curl -fsS -X POST \ -H "$AUTH_HEADER" \ -H "Content-Type: application/json" \ --data-binary "@$PAYLOAD" \ "$API/repos/$OWNER/$REPO/releases" \ > "$RESPONSE" RELEASE_ID="$(jq -er '.id' "$RESPONSE")" else echo "::error::Unable to retrieve release. HTTP $STATUS" cat "$RESPONSE" exit 1 fi # Upload the new package and checksum. for ASSET in "dist/$ZIP_NAME" "dist/$ZIP_NAME.sha256"; do ASSET_NAME="$(basename "$ASSET")" echo "Uploading: $ASSET_NAME" curl -fsS \ -X POST \ -H "$AUTH_HEADER" \ -H "Content-Type: application/octet-stream" \ --data-binary "@$ASSET" \ --get \ --data-urlencode "name=$ASSET_NAME" \ "$API/repos/$OWNER/$REPO/releases/$RELEASE_ID/assets" \ >/dev/null done # Verify the final release assets. curl -fsS \ -H "$AUTH_HEADER" \ "$API/repos/$OWNER/$REPO/releases/$RELEASE_ID" \ > "$RESPONSE" jq -e --arg zip "$ZIP_NAME" ' ([.assets[].name] | index($zip) != null) and ([.assets[].name] | index($zip + ".sha256") != null) ' "$RESPONSE" >/dev/null echo "Release $TAG successfully published." echo "Package and checksum verified." rm -f "$RESPONSE" "$PAYLOAD"