Fichiers

463 lignes
14 KiB
YAML

name: Firefox extension release
on:
workflow_dispatch:
permissions:
contents: write
# Prevent two releases from running simultaneously.
concurrency:
group: firefox-extension-release
cancel-in-progress: false
jobs:
build-and-publish:
name: Build, package and publish SKYNET SSO
runs-on: ubuntu-22.04
env:
EXTENSION_DIR: src/
REPOSITORY: ${{ gitea.repository }}
GITEA_SERVER_URL: ${{ gitea.server_url }}
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
AMO_JWT_ISSUER: ${{ secrets.AMO_JWT_ISSUER }}
AMO_JWT_SECRET: ${{ secrets.AMO_JWT_SECRET }}
# Première publication publique sur AMO.
# Passer à false après la première soumission réussie.
AMO_FIRST_LISTED_SUBMISSION: ${{ vars.AMO_FIRST_LISTED_SUBMISSION }}
steps:
# --------------------------------------------------
# 1. Checkout
# --------------------------------------------------
- name: Checkout main branch
uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0
token: ${{ secrets.GITEA_TOKEN }}
# --------------------------------------------------
# 2. Calculate next version
# --------------------------------------------------
- name: Determine next version
id: version
shell: bash
run: |
set -euo pipefail
if [[ -z "${GITEA_TOKEN:-}" ]]; then
echo "::error::GITEA_TOKEN is missing."
exit 1
fi
git fetch --force --tags origin
CURRENT_VERSION="$(node -p \
"require('./${EXTENSION_DIR}/manifest.json').version")"
if [[ ! "${CURRENT_VERSION}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "::error::Invalid manifest version: ${CURRENT_VERSION}"
exit 1
fi
VERSION="${CURRENT_VERSION}"
echo "Manifest version: ${CURRENT_VERSION}"
# Increment patch version until an unused tag is found.
while git show-ref --verify --quiet "refs/tags/v${VERSION}"; do
echo "Tag v${VERSION} already exists."
IFS='.' read -r MAJOR MINOR PATCH <<< "${VERSION}"
PATCH=$((PATCH + 1))
VERSION="${MAJOR}.${MINOR}.${PATCH}"
done
RELEASE_TAG="v${VERSION}"
echo "Next version: ${VERSION}"
echo "Release tag: ${RELEASE_TAG}"
# Update manifest in the runner workspace only.
node - "${EXTENSION_DIR}/manifest.json" "${VERSION}" <<'NODE'
const fs = require("node:fs");
const file = process.argv[2];
const version = process.argv[3];
const manifest = JSON.parse(
fs.readFileSync(file, "utf8")
);
manifest.version = version;
fs.writeFileSync(
file,
JSON.stringify(manifest, null, 2) + "\n"
);
console.log(`Manifest updated to ${version}`);
NODE
# Export values to subsequent steps.
echo "VERSION=${VERSION}" >> "$GITHUB_ENV"
echo "RELEASE_TAG=${RELEASE_TAG}" >> "$GITHUB_ENV"
# --------------------------------------------------
# 3. Install build tools
# --------------------------------------------------
- name: Install build tools
run: |
npm install --no-save --no-audit --no-fund \
terser@5.44.1 \
clean-css-cli@5.6.3 \
html-minifier-terser@7.2.0 \
web-ext@8.9.0
# --------------------------------------------------
# 4. Prepare and minify extension
# --------------------------------------------------
- name: Prepare and safely minify extension
shell: bash
run: |
set -euo pipefail
rm -rf dist artifacts
mkdir -p dist artifacts
cp -a "${EXTENSION_DIR}/." dist/
rm -f dist/README.md
# Minify JavaScript.
while IFS= read -r -d '' file; do
tmp="${file}.min"
npx --no-install terser "${file}" \
--compress --mangle \
--output "${tmp}"
mv "${tmp}" "${file}"
done < <(find dist -type f -name '*.js' -print0)
# Minify CSS.
while IFS= read -r -d '' file; do
tmp="${file}.min"
npx --no-install cleancss \
--compatibility ie11 -O2 \
--output "${tmp}" "${file}"
mv "${tmp}" "${file}"
done < <(find dist -type f -name '*.css' -print0)
# Minify HTML.
while IFS= read -r -d '' file; do
tmp="${file}.min"
npx --no-install html-minifier-terser \
--collapse-whitespace \
--remove-comments \
--minify-css \
--keep-closing-slash \
"${file}" --output "${tmp}"
mv "${tmp}" "${file}"
done < <(find dist -type f -name '*.html' -print0)
# Validate manifest and referenced resources.
node <<'NODE'
const fs = require("node:fs");
const path = require("node:path");
const root = path.resolve("dist");
const manifest = JSON.parse(
fs.readFileSync(
path.join(root, "manifest.json"),
"utf8"
)
);
const required = [
...(manifest.background?.scripts || []),
...(manifest.content_scripts || []).flatMap(s => [
...(s.js || []),
...(s.css || [])
]),
...(manifest.options_ui?.page
? [manifest.options_ui.page]
: []),
...(manifest.action?.default_popup
? [manifest.action.default_popup]
: []),
...Object.values(manifest.icons || {}),
...(manifest.web_accessible_resources || []).flatMap(
r => r.resources || []
)
];
for (const entry of required) {
const target = path.resolve(root, entry);
if (
!target.startsWith(root + path.sep) ||
!fs.existsSync(target)
) {
throw new Error(
`Manifest resource missing or invalid: ${entry}`
);
}
}
console.log(
`Manifest OK; ${required.length} resources verified.`
);
NODE
# Firefox extension validation.
npx --no-install web-ext lint --source-dir dist
# Generate ZIP.
ZIP_NAME="SKYNET-SSO-${RELEASE_TAG}.zip"
(
cd dist
zip -qr "../artifacts/${ZIP_NAME}" .
)
test -s "artifacts/${ZIP_NAME}"
echo "ZIP_NAME=${ZIP_NAME}" >> "$GITHUB_ENV"
echo "Build completed successfully."
# --------------------------------------------------
# 5. Submit extension to Mozilla AMO (public listing)
# --------------------------------------------------
- name: Submit extension to Mozilla AMO
id: amo
shell: bash
run: |
set -euo pipefail
if [[ -z "${AMO_JWT_ISSUER:-}" || -z "${AMO_JWT_SECRET:-}" ]]; then
echo "::error::AMO credentials are missing."
exit 1
fi
mkdir -p artifacts/amo
AMO_METADATA_ARGS=()
if [[ "${AMO_FIRST_LISTED_SUBMISSION}" != "true" &&
"${AMO_FIRST_LISTED_SUBMISSION}" != "false" ]]; then
echo "::error::AMO_FIRST_LISTED_SUBMISSION must be true or false."
exit 1
fi
if [[ "${AMO_FIRST_LISTED_SUBMISSION}" == "true" ]]; then
if [[ ! -s "amo-metadata.json" ]]; then
echo "::error::amo-metadata.json is missing."
exit 1
fi
AMO_METADATA_ARGS=(--amo-metadata "amo-metadata.json")
echo "First public AMO submission."
else
echo "Submitting an update to the existing AMO listing."
fi
npx --no-install web-ext sign \
--source-dir dist \
--artifacts-dir artifacts/amo \
--channel listed \
--approval-timeout 0 \
"${AMO_METADATA_ARGS[@]}" \
--api-key "${AMO_JWT_ISSUER}" \
--api-secret "${AMO_JWT_SECRET}" \
--no-input
echo "Public AMO submission completed."
# --------------------------------------------------
# 6. Verify build artifacts before publication
# --------------------------------------------------
- name: Verify release files
shell: bash
run: |
set -euo pipefail
ZIP="artifacts/SKYNET-SSO-${RELEASE_TAG}.zip"
if [[ ! -s "${ZIP}" ]]; then
echo "::error::ZIP archive is missing."
exit 1
fi
echo "ZIP archive is ready."
echo "AMO submission completed successfully."
# --------------------------------------------------
# 7. Commit manifest and push Git tag
# --------------------------------------------------
- name: Commit manifest and create Git tag
id: git_publish
shell: bash
run: |
set -euo pipefail
git config user.name "Gitea Actions"
git config user.email "actions@celjim.fr"
# Check that the remote tag is still available.
git fetch --force --tags origin
if git show-ref --verify --quiet "refs/tags/${RELEASE_TAG}"; then
echo "::error::Tag ${RELEASE_TAG} already exists."
exit 1
fi
# Commit manifest version.
if ! git diff --quiet -- "${EXTENSION_DIR}/manifest.json"; then
git add "${EXTENSION_DIR}/manifest.json"
git commit -m "chore(release): ${RELEASE_TAG}"
fi
# Push updated manifest to main.
AUTH="$(printf 'x-access-token:%s' "${GITEA_TOKEN}" | base64 -w 0)"
git -c http.extraheader="AUTHORIZATION: basic ${AUTH}" \
push origin HEAD:refs/heads/main
# Create annotated tag.
git tag -a "${RELEASE_TAG}" HEAD \
-m "Release ${RELEASE_TAG}"
# Push tag.
git -c http.extraheader="AUTHORIZATION: basic ${AUTH}" \
push origin "refs/tags/${RELEASE_TAG}"
echo "Git commit and tag published successfully."
# --------------------------------------------------
# 8. Create Gitea release
# --------------------------------------------------
- name: Create Gitea release
id: release
shell: bash
run: |
set -euo pipefail
API_BASE="${GITEA_SERVER_URL%/}/api/v1"
OWNER="${REPOSITORY%%/*}"
REPO="${REPOSITORY#*/}"
jq -n \
--arg tag "${RELEASE_TAG}" \
--arg version "${VERSION}" \
'{
tag_name: $tag,
target_commitish: "main",
name: ("SKYNET SSO " + $tag),
body: (
"Release automatique de SKYNET SSO version "
+ $version
+ "."
),
draft: false,
prerelease: false
}' > release-payload.json
curl --fail --silent --show-error \
--request POST \
--header "Authorization: token ${GITEA_TOKEN}" \
--header "Content-Type: application/json" \
--data @release-payload.json \
"${API_BASE}/repos/${OWNER}/${REPO}/releases" \
--output release-response.json
RELEASE_ID="$(jq -r '.id // empty' release-response.json)"
if [[ -z "${RELEASE_ID}" ]]; then
echo "::error::Unable to retrieve Gitea release ID."
cat release-response.json
exit 1
fi
echo "RELEASE_ID=${RELEASE_ID}" >> "$GITHUB_ENV"
echo "Gitea release created: ${RELEASE_TAG}"
# --------------------------------------------------
# 9. Upload release files to Gitea
# --------------------------------------------------
- name: Upload release files to Gitea
shell: bash
run: |
set -euo pipefail
API_BASE="${GITEA_SERVER_URL%/}/api/v1"
OWNER="${REPOSITORY%%/*}"
REPO="${REPOSITORY#*/}"
upload_asset() {
local file="$1"
local name
name="$(basename "${file}")"
echo "Uploading ${name}..."
curl --fail --silent --show-error \
--request POST \
--header "Authorization: token ${GITEA_TOKEN}" \
--form "attachment=@${file}" \
"${API_BASE}/repos/${OWNER}/${REPO}/releases/${RELEASE_ID}/assets?name=${name}"
}
# Always upload ZIP.
upload_asset "artifacts/SKYNET-SSO-${RELEASE_TAG}.zip"
# Upload XPI only if AMO signing succeeded.
if [[ -f "artifacts/SKYNET-SSO-${RELEASE_TAG}.xpi" ]]; then
upload_asset "artifacts/SKYNET-SSO-${RELEASE_TAG}.xpi"
else
echo "AMO signing unavailable or unsuccessful."
echo "Publishing ZIP only."
fi
echo "Release assets uploaded successfully."
# --------------------------------------------------
# 10. Preserve workflow artifacts
# --------------------------------------------------
- name: Preserve build files in workflow artifacts
uses: actions/upload-artifact@v3
with:
name: SKYNET-SSO-${{ gitea.run_number }}
path: artifacts/*
if-no-files-found: error