Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Build context for Containerfile.artifact. Mirrors .npmignore: the build
# needs sources, the shrinkwrap, the scripts that assemble dist/, and the
# command docs that ship in the package (m365 --help reads docs/docs/cmd).
.git
.github
node_modules
dist
allCommands*.json
coverage
.nyc_output
.vscode
.circleci
test-projects
docs/**
!docs/docs/cmd/**
!docs/docs/_clisettings.mdx
39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: ci

# Fork CI. The workflows inherited from upstream (check_pr, release, ...) are
# all gated on `github.repository_owner == 'pnp'` and never run here, so
# without this file the fork ships a production artifact with zero test
# coverage in CI. Same steps as upstream's check_pr build+test legs, on the
# Node major the artifact image is built and consumed with.

on:
pull_request:
push:
branches: [main]
workflow_dispatch:

permissions:
contents: read

concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true

jobs:
build-and-test:
runs-on: ubuntu-latest
timeout-minutes: 30
env:
CLIMICROSOFT365_NOUPDATE: "1"
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4

- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
with:
node-version: 22
cache: npm

- run: npm ci --ignore-scripts
- run: npm run build
- run: npm run lint
- run: npm run test:test
44 changes: 44 additions & 0 deletions .github/workflows/cleanup-pr-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: cleanup-pr-image

# Delete the pr-<number> image tag once its pull request closes, so PR
# builds do not pile up in billed GHCR storage. The sha-* / semver / latest
# tags published from the default branch are never touched -- PR pushes only
# ever create pr-<number>.
#
# pull_request_target rather than pull_request because fork PRs get a
# read-only GITHUB_TOKEN on pull_request, which cannot delete package
# versions. pull_request_target is safe here because this workflow never
# checks out or executes PR code.

on:
pull_request_target:
types: [closed]

permissions:
packages: write

jobs:
delete:
runs-on: ubuntu-latest
env:
# Keep in step with the image matrix in the publish-image workflow.
IMAGES: m365-cli
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.number }}
steps:
- name: Delete pr-${{ github.event.number }} package versions
run: |
for image in $IMAGES; do
ids=$(gh api "orgs/${{ github.repository_owner }}/packages/container/${image}/versions" \
--paginate \
--jq ".[] | select(.metadata.container.tags[]? == \"pr-${PR_NUMBER}\") | .id")
if [ -z "$ids" ]; then
echo "No pr-${PR_NUMBER} version of ${image} to delete."
continue
fi
for id in $ids; do
echo "Deleting ${image} version ${id} (pr-${PR_NUMBER})"
gh api --method DELETE \
"orgs/${{ github.repository_owner }}/packages/container/${image}/versions/${id}"
done
done
87 changes: 87 additions & 0 deletions .github/workflows/prune-images.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: prune-images

# Weekly registry hygiene: delete package versions that are MAX_AGE_DAYS old
# or older and are either
# - tagged only pr-* : images whose close-time cleanup was missed, or for
# open PRs untouched for a month -- the next push to such a PR simply
# republishes its image, or
# - untagged : orphaned digests that escaped the inline superseded-
# digest deletion in publish-pr-image (e.g. a run that failed mid-push).
#
# sha-*, semver and latest tags are never deleted regardless of age, and any
# digest referenced as a child of a *tagged* index is skipped, so this stays
# safe if builds ever go multi-arch (GHCR lists index children as untagged
# versions).
#
# Note: GitHub suspends cron workflows after ~60 days without repository
# activity; a push re-arms them.

on:
schedule:
- cron: "17 3 * * 1" # Mondays 03:17 UTC
workflow_dispatch:
inputs:
dry_run:
description: Only report what would be deleted
type: boolean
default: true

jobs:
prune:
runs-on: ubuntu-latest
permissions:
packages: write
env:
# Keep in step with the image matrix in the publish-image workflow.
IMAGES: m365-cli
MAX_AGE_DAYS: 30
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Empty on scheduled runs, which therefore delete for real.
DRY_RUN: ${{ inputs.dry_run }}
steps:
- name: Prune old PR images and orphans
run: |
set -euo pipefail
owner="${{ github.repository_owner }}"
cutoff=$(date -u -d "${MAX_AGE_DAYS} days ago" +%s)
echo "$GH_TOKEN" | skopeo login ghcr.io --username "${{ github.actor }}" --password-stdin

for image in $IMAGES; do
versions=$(gh api "orgs/${owner}/packages/container/${image}/versions" --paginate)

# Digests referenced by any tagged index must survive, or the
# tagged image breaks.
protected=$(echo "$versions" \
| jq -r '.[] | select(.metadata.container.tags | length > 0) | .name' \
| while read -r digest; do
skopeo inspect --raw "docker://ghcr.io/${owner}/${image}@${digest}" \
| jq -r '.manifests[]?.digest // empty'
done | sort -u)

candidates=$(echo "$versions" | jq -r --argjson cutoff "$cutoff" '
.[]
| select((.updated_at | fromdateiso8601) <= $cutoff)
| select((.metadata.container.tags | length == 0)
or (.metadata.container.tags | all(startswith("pr-"))))
| "\(.id)\t\(.name)\t\(.metadata.container.tags | join(","))"')

if [ -z "$candidates" ]; then
echo "${image}: nothing ${MAX_AGE_DAYS}+ days old to prune."
continue
fi

while IFS=$'\t' read -r id name tags; do
[ -z "$id" ] && continue
if [ -n "$protected" ] && echo "$protected" | grep -qx "$name"; then
echo "${image}: keeping ${name} (child of a tagged index)"
continue
fi
if [ "$DRY_RUN" = "true" ]; then
echo "${image}: DRY RUN, would delete version ${id} (${tags:-untagged}) ${name}"
else
echo "${image}: deleting version ${id} (${tags:-untagged}) ${name}"
gh api --method DELETE \
"orgs/${owner}/packages/container/${image}/versions/${id}"
fi
done <<< "$candidates"
done
155 changes: 155 additions & 0 deletions .github/workflows/publish-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
name: publish-image

# Build this fork's artifact image (Containerfile.artifact: a FROM scratch
# image containing only the built m365 package tree at /opt/m365) and publish
# it to the organisation's private GitHub Container Registry as
# ghcr.io/rh-forge/m365-cli.
#
# Consumers pin it by tag *and* digest, e.g. in rh-forge/openclaw-saw-image:
# ARG M365_IMAGE=ghcr.io/rh-forge/m365-cli:sha-<commit>@sha256:<digest>
# so sha-<commit> tags must be immutable: the guard step below refuses to
# publish a commit whose sha- tag already exists. A re-run must never
# re-point sha-<commit> at a new digest -- the old digest would become
# untagged and prune-images would delete it, breaking every pinned consumer.
# There is deliberately no semver/tag trigger here for the same reason: this
# fork cuts no releases, and every published artifact maps 1:1 to a commit.
#
# Pull requests build the image and hand it off as an OCI-archive artifact
# instead of pushing. The publish-pr-image workflow (workflow_run) picks the
# artifact up and pushes it as pr-<number>. The split exists because
# GITHUB_TOKEN is downgraded to read-only on pull_request runs from forks --
# so this workflow's PR path only ever reads, and the privileged half never
# executes PR code. PR builds publish only pr-<number>, so unreviewed images
# stay clearly labelled and never enter the sha-* namespace.
#
# `latest` tracks the default branch. Nothing should pin it.

on:
push:
branches: [main]
pull_request:
workflow_dispatch:

# GITHUB_TOKEN can push to ghcr.io for repositories in this organisation, so
# no PAT and no organisation secret is needed.
permissions:
contents: read
packages: write

concurrency:
group: publish-image-${{ github.ref }}
cancel-in-progress: true

env:
IMAGE: m365-cli
REGISTRY_IMAGE: ghcr.io/${{ github.repository_owner }}/m365-cli

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4

- uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3

- name: Log in to ghcr.io
if: github.event_name != 'pull_request'
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Refuse to republish an existing sha- tag
if: github.event_name != 'pull_request'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
echo "$GH_TOKEN" | skopeo login ghcr.io --username "${{ github.actor }}" --password-stdin
ref="docker://${REGISTRY_IMAGE}:sha-${GITHUB_SHA}"
if digest=$(skopeo inspect --no-tags --format '{{.Digest}}' "$ref" 2>/dev/null); then
echo "::error::${REGISTRY_IMAGE}:sha-${GITHUB_SHA} is already published as ${digest}."
echo "::error::sha- tags are immutable (consumers pin them by digest). Push a new commit to publish a new artifact."
exit 1
fi
echo "No existing artifact for sha-${GITHUB_SHA}; proceeding."

- id: meta
name: Derive tags and OCI labels
uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5
with:
images: ${{ env.REGISTRY_IMAGE }}
tags: |
type=sha,format=long,prefix=sha-
type=raw,value=latest,enable={{is_default_branch}}
type=ref,event=pr
labels: |
org.opencontainers.image.title=${{ env.IMAGE }}

- name: Build and push
id: build
if: github.event_name != 'pull_request'
uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6
with:
context: .
file: Containerfile.artifact
# Stated, not inherited: a silent runner flip would ship binaries
# that die on the cluster with "Exec format error".
platforms: linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha,scope=${{ env.IMAGE }}
cache-to: type=gha,mode=max,scope=${{ env.IMAGE }},ignore-error=true
# Suppress the unknown/unknown attestation manifest. With a single
# platform this also makes the pushed object a plain manifest, so
# the digest below is exactly what FROM ...@sha256 needs.
provenance: false

- name: Record the pin for consumers
if: github.event_name != 'pull_request'
env:
DIGEST: ${{ steps.build.outputs.digest }}
run: |
set -euo pipefail
{
echo "## Published ${REGISTRY_IMAGE}"
echo
echo "Pin (tag and digest):"
echo
echo '```'
echo "${REGISTRY_IMAGE}:sha-${GITHUB_SHA}@${DIGEST}"
echo '```'
echo
echo "Bump the consumer with:"
echo
echo '```'
echo "scripts/bump-tool-pin.sh ${IMAGE} sha-${GITHUB_SHA}"
echo '```'
} >> "$GITHUB_STEP_SUMMARY"

# Deliberately untagged: each tag becomes a separate entry in the
# archive's index and skopeo then refuses to pick ("more than one
# image in oci"). The destination tag is applied by publish-pr-image.
- name: Build to OCI archive
if: github.event_name == 'pull_request'
uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6
with:
context: .
file: Containerfile.artifact
platforms: linux/amd64
outputs: type=oci,dest=/tmp/${{ env.IMAGE }}.tar
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha,scope=${{ env.IMAGE }}
cache-to: type=gha,mode=max,scope=${{ env.IMAGE }},ignore-error=true
provenance: false

- name: Upload image artifact
if: github.event_name == 'pull_request'
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
with:
name: pr-image-${{ env.IMAGE }}
path: /tmp/${{ env.IMAGE }}.tar
retention-days: 1
compression-level: 0
Loading
Loading