diff --git a/.github/release-drafter.yml b/.github/release-drafter.yml new file mode 100644 index 0000000..c3360e5 --- /dev/null +++ b/.github/release-drafter.yml @@ -0,0 +1,17 @@ +name-template: 'Docker Image docker-v$RESOLVED_VERSION' +tag-template: 'docker-v$RESOLVED_VERSION' +tag-prefix: docker-v +filter-by-commitish: false +change-template: '- $TITLE (#$NUMBER) @$AUTHOR' +template: | + ## Changes + + $CHANGES + + ## Docker image + + ```sh + docker pull ghcr.io/michaelsp/infra-eye:docker-v$RESOLVED_VERSION + ``` + + Supported platforms: linux/amd64, linux/arm64. diff --git a/.github/workflows/README.md b/.github/workflows/README.md index 87ab5f7..f0516e3 100644 --- a/.github/workflows/README.md +++ b/.github/workflows/README.md @@ -1,176 +1,59 @@ -# GitHub Actions Workflows +# CI and releases -This directory contains automated CI/CD workflows for the infra-eye project. +## Main builds and draft releases -## Workflows +`docker-publish.yml` lints, tests, and builds on every PR and push to `main`, +including chart changes. PRs build without pushing. Successful main builds push +multi-platform images (amd64/arm64) to `ghcr.io/michaelsp/infra-eye`, tagged with +`main`, `latest`, and the next `docker-vX.Y.Z` version. -### 1. Docker Image Build and Publish (`docker-publish.yml`) +The existing semver action calculates the version from conventional commits. +[Release Drafter](https://github.com/release-drafter/release-drafter) then creates +or updates one draft release with merged PR notes and the exact built commit SHA. +Until publication, later main builds may update the draft version and image. -Builds and publishes the Docker image to GitHub Container Registry (ghcr.io). +## Weekly publication -**Triggers:** -- Push to `main` branch -- Git tags matching `v*.*.*` (e.g., v1.0.0) -- Pull requests to `main` (build only, no push) -- Manual workflow dispatch +`release-publish.yml` runs Mondays at **08:00 UTC** (09:00 CET / 10:00 CEST). +It can also be run manually on `main`. If no Docker draft exists, it does nothing. -**Features:** -- Multi-architecture builds (linux/amd64, linux/arm64) -- Automatic versioning using semantic versioning from git tags -- Layer caching for faster builds -- Tagged images: - - `latest` (main branch) - - Semantic versions from tags (e.g., `v1.0.0`, `v1.0`, `v1`) - - Branch names for feature branches - - Commit SHA for tracking +For an existing draft, it: -**Usage:** -```bash -# Pull the latest image -docker pull ghcr.io/michaelsp/infra-eye:latest +1. Checks out the exact commit recorded in the draft. +2. Packages the Helm chart with version `X.Y.Z` and appVersion `docker-vX.Y.Z`, + so the default deployment image matches the built Docker tag. +3. Uses the community Helm chart-releaser action to publish the chart archive + and update the `gh-pages` Helm index; also pushes the chart to + `oci://ghcr.io/michaelsp/infra-eye`. +4. Uses [softprops/action-gh-release](https://github.com/softprops/action-gh-release) + to publish the existing Docker draft and mark it latest. -# Pull a specific version -docker pull ghcr.io/michaelsp/infra-eye:v1.0.0 +Publication and main image builds share a concurrency lock. Chart publication +happens first so a failure leaves the draft available for retry. Existing chart +releases are skipped on retry. Multiple Docker drafts fail explicitly rather +than choosing one arbitrarily. -# Run the container -docker run -p 3000:3000 ghcr.io/michaelsp/infra-eye:latest -``` - -### 2. Helm Chart Build and Publish (`helm-publish.yml`) - -Packages and publishes the Helm chart to multiple destinations. +Chart versions are set in the packaging workspace. No chart bump PR, auto-merge, +merge polling, or follow-up workflow dispatch is needed. Source Chart.yaml stays +as the development default. -**Triggers:** -- Push to `main` branch (when chart files change) -- Git tags matching `v*.*.*` -- Pull requests to `main` (lint and test only) -- Manual workflow dispatch +`chart-0-test.yml` additionally lints and tests chart-related PRs. -**Features:** -- Helm chart linting and validation -- Automatic version management: - - Tags: Uses git tag version (e.g., `v1.0.0` → `1.0.0`) - - Main branch: Uses Chart.yaml version with commit SHA (e.g., `0.1.0-a1b2c3d`) -- Publishes to three locations: - 1. **OCI Registry (ghcr.io)** - Modern Helm chart distribution - 2. **GitHub Releases** - Downloadable chart archives (on tags) - 3. **GitHub Pages** - Traditional Helm repository +## Permissions and setup -**Usage:** +Main builds need `contents: write`, `packages: write`, and `pull-requests: read` +for drafts, images, and release notes. Weekly publishing needs `contents: write` +and `packages: write`. Both use the built-in `GITHUB_TOKEN`; no PAT is needed. -#### Using OCI Registry (Recommended): -```bash -# Install from OCI registry -helm install infra-eye oci://ghcr.io/michaelsp/charts/infra-eye --version 1.0.0 - -# Upgrade -helm upgrade infra-eye oci://ghcr.io/michaelsp/charts/infra-eye --version 1.0.1 -``` +Configure GitHub Pages to publish from the `gh-pages` branch. The Helm repository +is available at https://michaelsp.github.io/infra-eye. Make GHCR packages public +if anonymous installs are required. -#### Using GitHub Pages: -```bash -# Add the Helm repository +```sh helm repo add infra-eye https://michaelsp.github.io/infra-eye - -# Update repo index helm repo update +helm install infra-eye infra-eye/infra-eye --version X.Y.Z -# Install the chart -helm install my-infra-eye infra-eye/infra-eye - -# Search available versions -helm search repo infra-eye -l -``` - -#### Using GitHub Releases: -Download the chart archive from the [Releases page](https://github.com/MichaelSp/infra-eye/releases) and install: -```bash -helm install infra-eye ./infra-eye-1.0.0.tgz +# OCI alternative +helm install infra-eye oci://ghcr.io/michaelsp/infra-eye/infra-eye --version X.Y.Z ``` - -## Release Process - -To create a new release with both Docker image and Helm chart: - -1. **Update version in Chart.yaml** (if needed): - ```yaml - version: 1.0.0 - appVersion: "1.0.0" - ``` - -2. **Commit changes**: - ```bash - git add chart/infra-eye/Chart.yaml - git commit -m "chore: bump version to 1.0.0" - git push origin main - ``` - -3. **Create and push a git tag**: - ```bash - git tag -a v1.0.0 -m "Release version 1.0.0" - git push origin v1.0.0 - ``` - -4. **Workflows will automatically**: - - Build and push Docker image with tag `v1.0.0`, `v1.0`, `v1`, and `latest` - - Package and publish Helm chart version `1.0.0` - - Create a GitHub Release with chart archive attached - - Update GitHub Pages repository index - -## Permissions Required - -The workflows require the following GitHub permissions: -- `contents: write` - For creating releases and updating gh-pages -- `packages: write` - For publishing to GitHub Container Registry -- `pages: write` - For deploying to GitHub Pages -- `id-token: write` - For OIDC authentication - -These are automatically granted through `GITHUB_TOKEN` in GitHub Actions. - -## Configuration - -### GitHub Pages Setup - -To enable the GitHub Pages Helm repository: - -1. Go to repository **Settings** → **Pages** -2. Under "Build and deployment": - - Source: **Deploy from a branch** - - Branch: **gh-pages** / **(root)** -3. Save changes - -The Helm repository will be available at: `https://michaelsp.github.io/infra-eye` - -### Container Registry Visibility - -To make Docker images and Helm charts public: - -1. Go to the [package settings](https://github.com/users/MichaelSp/packages) -2. Find `infra-eye` (Docker) and `charts/infra-eye` (Helm) -3. Click on each package → **Package settings** -4. Scroll to "Danger Zone" → **Change visibility** → Make public - -## Troubleshooting - -### Workflow fails with "403 Forbidden" -- Ensure GitHub Actions has permission to write to packages -- Check repository settings → Actions → General → Workflow permissions → Enable "Read and write permissions" - -### Helm chart push fails -- Verify the chart version is not already published -- Ensure Chart.yaml has valid semantic versioning - -### GitHub Pages not updating -- Check if gh-pages branch exists and has content -- Verify GitHub Pages is enabled in repository settings -- Allow a few minutes for GitHub Pages to deploy after the workflow completes - -## Manual Workflow Execution - -You can manually trigger workflows from the GitHub Actions tab: - -1. Go to **Actions** tab -2. Select the workflow (Docker or Helm) -3. Click **Run workflow** -4. Choose the branch -5. Click **Run workflow** button diff --git a/.github/workflows/chart-0-test.yml b/.github/workflows/chart-0-test.yml index 802cb9b..816821d 100644 --- a/.github/workflows/chart-0-test.yml +++ b/.github/workflows/chart-0-test.yml @@ -1,8 +1,5 @@ name: Helm Chart - 0 Test and Lint -description: | - Lint and test Helm chart. - on: pull_request: branches: @@ -10,6 +7,7 @@ on: paths: - "chart/**" - ".github/workflows/chart-*.yml" + - ".github/workflows/release-publish.yml" workflow_dispatch: permissions: diff --git a/.github/workflows/chart-1-publish.yml b/.github/workflows/chart-1-publish.yml deleted file mode 100644 index b05b439..0000000 --- a/.github/workflows/chart-1-publish.yml +++ /dev/null @@ -1,83 +0,0 @@ -name: Helm Chart - 1 Publish - -on: - workflow_dispatch: - inputs: - version: - description: Chart version to publish (without the chart-v prefix) - required: true - type: string -permissions: {} - -env: - REGISTRY: ghcr.io - CHART_PATH: chart/infra-eye - -jobs: - release: - runs-on: ubuntu-latest - permissions: - contents: write - packages: write - id-token: write - - steps: - - name: Checkout repository - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 - with: - fetch-depth: 0 - - - name: Set up Helm - uses: azure/setup-helm@9bc31f4ebc9c6b171d7bfbaa5d006ae7abdb4310 # v5 - with: - version: "latest" - - - name: Configure Git - run: | - git config user.name "$GITHUB_ACTOR" - git config user.email "$GITHUB_ACTOR@users.noreply.github.com" - - - name: Verify and create chart tag - env: - CHART_VERSION: ${{ inputs.version }} - run: | - VERSION="$(yq eval '.version' "${{ env.CHART_PATH }}/Chart.yaml")" - if [ "$VERSION" != "$CHART_VERSION" ]; then - echo "::error::Chart.yaml version $VERSION does not match requested version $CHART_VERSION" - exit 1 - fi - - TAG="chart-v${CHART_VERSION}" - if git show-ref --tags --verify --quiet "refs/tags/${TAG}"; then - TAG_SHA="$(git rev-parse "${TAG}^{commit}")" - if [ "$TAG_SHA" != "$GITHUB_SHA" ]; then - echo "::error::${TAG} already points to ${TAG_SHA}, expected ${GITHUB_SHA}" - exit 1 - fi - else - git tag "$TAG" "$GITHUB_SHA" - git push origin "refs/tags/${TAG}" - fi - - - name: Run chart-releaser - uses: helm/chart-releaser-action@cae68fefc6b5f367a0275617c9f83181ba54714f # v1.7.0 - with: - charts_dir: chart - env: - CR_TOKEN: "${{ secrets.GITHUB_TOKEN }}" - - - name: Log in to Container Registry - uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4 - with: - registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Push Helm chart to OCI registry - run: | - OWNER_LC=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]') - for chart in .cr-release-packages/*.tgz; do - [ -f "$chart" ] || continue - helm push "$chart" oci://${{ env.REGISTRY }}/$OWNER_LC/infra-eye - echo "Pushed $chart" - done diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index 0359813..702e3f2 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -4,24 +4,15 @@ on: push: branches: - main - tags: - - "docker-v*.*.*" - paths-ignore: - - "chart/**" - - ".github/workflows/chart-*.yml" pull_request: - paths-ignore: - - "chart/**" - - ".github/workflows/chart-*.yml" branches: - main - workflow_dispatch: -permissions: {} +permissions: + contents: read env: REGISTRY: ghcr.io - CHART_PATH: chart/infra-eye jobs: lint: @@ -66,13 +57,14 @@ jobs: runs-on: ubuntu-latest needs: [test, lint] concurrency: - group: ${{ github.workflow }}-${{ github.ref }} + # Also locks weekly publishing so a draft always matches its built image. + group: release-${{ github.ref_name }} + cancel-in-progress: false permissions: contents: write packages: write id-token: write - pull-requests: write - actions: write + pull-requests: read steps: - name: Checkout repository @@ -132,99 +124,19 @@ jobs: ${{ github.event_name != 'pull_request' && format('type=registry,ref={0}/{1}:buildcache,mode=max', env.REGISTRY, steps.repo.outputs.image_name) || '' }} type=gha,mode=max - - name: Create GitHub Release - if: github.event_name == 'push' && github.ref == 'refs/heads/main' + - name: Strip Docker tag prefix + id: version env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - RELEASE_SHA: ${{ github.sha }} - run: | - VERSION="${{ steps.semver.outputs.next }}" - RELEASE_TAG="${VERSION}" - - # Create release notes - cat > release_notes.md << EOF - ## Docker Image Release - - \`\`\`bash - docker pull ${{ env.REGISTRY }}/${{ steps.repo.outputs.image_name }}:${VERSION} - \`\`\` - - ### Available Images - - \`${{ env.REGISTRY }}/${{ steps.repo.outputs.image_name }}:${VERSION}\` - - \`${{ env.REGISTRY }}/${{ steps.repo.outputs.image_name }}:latest\` - - ### Supported Platforms - - linux/amd64 - - linux/arm64 - - ### Build Information - - Commit SHA: \`${{ github.sha }}\` - - Committed by: @${{ github.actor }} - - Build Date: ${{ github.event.head_commit.timestamp }} - EOF - - if gh release view "${RELEASE_TAG}" > /dev/null 2>&1; then - TAG_SHA="$(git rev-list -n 1 "${RELEASE_TAG}")" - if [ "${TAG_SHA}" != "${RELEASE_SHA}" ]; then - echo "::error::Release ${RELEASE_TAG} already targets ${TAG_SHA}, expected ${RELEASE_SHA}" - exit 1 - fi - - gh release edit "${RELEASE_TAG}" \ - --title "Docker Image ${VERSION}" \ - --notes-file release_notes.md \ - --latest - else - gh release create "${RELEASE_TAG}" \ - --target "${RELEASE_SHA}" \ - --title "Docker Image ${VERSION}" \ - --notes-file release_notes.md \ - --latest - fi - - - name: Update Helm Chart version - if: github.event_name == 'push' && github.ref == 'refs/heads/main' - run: | - VERSION="${{ steps.semver.outputs.next }}" - PLAIN_VERSION="${VERSION#docker-v}" - - yq eval -i ".appVersion = \"$PLAIN_VERSION\"" ${{ env.CHART_PATH }}/Chart.yaml - yq eval -i ".version = \"$PLAIN_VERSION\"" ${{ env.CHART_PATH }}/Chart.yaml + RELEASE_TAG: ${{ steps.semver.outputs.next }} + run: echo "plain=${RELEASE_TAG#docker-v}" >> "$GITHUB_OUTPUT" - - name: Create Helm Chart version bump PR + - name: Create or update draft release if: github.event_name == 'push' && github.ref == 'refs/heads/main' - id: chart_bump_pr - uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8 + uses: release-drafter/release-drafter@d1a19e60e81a7e650d00a7de9277f87e439117ea # v7.11.0 with: - token: ${{ github.token }} - branch: chore/release-chart-${{ steps.semver.outputs.next }} - base: main - title: "chore(release): bump Helm chart version to ${{ steps.semver.outputs.next }}" - body: "Automated Helm chart version bump after Docker image release ${{ steps.semver.outputs.next }}." - commit-message: "chore(release): bump Helm chart version to ${{ steps.semver.outputs.next }}" - add-paths: | - ${{ env.CHART_PATH }}/Chart.yaml - labels: "release, automated-pr" - delete-branch: true - - - name: Enable auto-merge for Helm Chart version bump PR - if: steps.chart_bump_pr.outputs.pull-request-number != '' + version: ${{ steps.version.outputs.plain }} + tag: ${{ steps.semver.outputs.next }} + commitish: ${{ github.sha }} + publish: false env: - GH_TOKEN: ${{ github.token }} - run: | - PR_NUMBER="${{ steps.chart_bump_pr.outputs.pull-request-number }}" - gh pr merge --squash --auto "$PR_NUMBER" - - for attempt in $(seq 1 60); do - MERGED_AT="$(gh pr view "$PR_NUMBER" --json mergedAt --jq '.mergedAt // empty')" - if [ -n "$MERGED_AT" ]; then - VERSION="${{ steps.semver.outputs.next }}" - PLAIN_VERSION="${VERSION#docker-v}" - gh workflow run chart-1-publish.yml --ref main -f version="$PLAIN_VERSION" - exit 0 - fi - sleep 10 - done - - echo "::error::Timed out waiting for PR #$PR_NUMBER to merge" - exit 1 + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/release-publish.yml b/.github/workflows/release-publish.yml new file mode 100644 index 0000000..2bb508d --- /dev/null +++ b/.github/workflows/release-publish.yml @@ -0,0 +1,109 @@ +name: Release - Publish Weekly + +on: + schedule: + - cron: "0 8 * * 1" # Mondays, 08:00 UTC + workflow_dispatch: +permissions: {} + +env: + REGISTRY: ghcr.io + CHART_PATH: chart/infra-eye + +jobs: + release: + runs-on: ubuntu-latest + if: github.ref == 'refs/heads/main' + concurrency: + group: release-main + cancel-in-progress: false + permissions: + contents: write + packages: write + + steps: + - name: Find pending Docker release + id: draft + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + script: | + const releases = await github.paginate(github.rest.repos.listReleases, { + ...context.repo, per_page: 100 + }); + const drafts = releases.filter(r => r.draft && /^docker-v\d+\.\d+\.\d+$/.test(r.tag_name)); + if (drafts.length > 1) { + throw new Error('Multiple Docker drafts found; resolve duplicates before publishing.'); + } + const draft = drafts[0]; + if (!draft) { + core.info('No draft release; nothing to publish.'); + return; + } + if (!/^[0-9a-f]{40}$/.test(draft.target_commitish)) { + throw new Error('Draft must target an exact built commit SHA.'); + } + core.setOutput('tag', draft.tag_name); + core.setOutput('sha', draft.target_commitish); + core.setOutput('version', draft.tag_name.replace(/^docker-v/, '')); + + - name: Checkout release commit + if: steps.draft.outputs.tag != '' + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + ref: ${{ steps.draft.outputs.sha }} + fetch-depth: 0 + + - name: Set up Helm + if: steps.draft.outputs.tag != '' + uses: azure/setup-helm@9bc31f4ebc9c6b171d7bfbaa5d006ae7abdb4310 # v5 + with: + version: "latest" + + - name: Package matching Helm chart + if: steps.draft.outputs.tag != '' + env: + RELEASE_VERSION: ${{ steps.draft.outputs.version }} + RELEASE_TAG: ${{ steps.draft.outputs.tag }} + run: | + yq eval -i '.version = strenv(RELEASE_VERSION) | .appVersion = strenv(RELEASE_TAG)' "$CHART_PATH/Chart.yaml" + helm lint "$CHART_PATH" + mkdir -p .cr-release-packages + helm package "$CHART_PATH" --destination .cr-release-packages + git config user.name "$GITHUB_ACTOR" + git config user.email "$GITHUB_ACTOR@users.noreply.github.com" + + - name: Publish Helm chart and Pages index + if: steps.draft.outputs.tag != '' + uses: helm/chart-releaser-action@cae68fefc6b5f367a0275617c9f83181ba54714f # v1.7.0 + with: + skip_packaging: true + skip_existing: true + mark_as_latest: false + env: + CR_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Log in to Container Registry + if: steps.draft.outputs.tag != '' + uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Push Helm chart to OCI registry + if: steps.draft.outputs.tag != '' + env: + RELEASE_VERSION: ${{ steps.draft.outputs.version }} + run: | + OWNER_LC="${GITHUB_REPOSITORY_OWNER,,}" + helm push ".cr-release-packages/infra-eye-${RELEASE_VERSION}.tgz" "oci://${REGISTRY}/${OWNER_LC}/infra-eye" + + # Publish last: a failed chart publish leaves the draft available for retry. + - name: Publish draft release + if: steps.draft.outputs.tag != '' + uses: softprops/action-gh-release@8a0cd651e5b2365bd640854a3fa5dfc70e4875f3 # v3.0.4 + with: + tag_name: ${{ steps.draft.outputs.tag }} + target_commitish: ${{ steps.draft.outputs.sha }} + draft: false + make_latest: true