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

Replace individual inputs with JSON string array of inputs #1014

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 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
19 changes: 12 additions & 7 deletions .github/workflows/deploy-pr-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ on:

jobs:
deploy-pr-preview:
uses: grafana/writers-toolkit/.github/workflows/deploy-preview.yml@main
if: ${{ ! github.event.pull_request.head.repo.fork }}
if: "!github.event.pull_request.head.repo.fork"
uses: grafana/writers-toolkit/.github/workflows/deploy-preview.yml@jdb/deploy-preview
with:
sha: ${{ github.event.pull_request.head.sha }}
branch: ${{ github.head_ref }}
event_number: ${{ github.event.number }}
title: ${{ github.event.pull_request.title }}
repo: writers-toolkit
website_directory: content/docs/writers-toolkit
relative_prefix: /docs/writers-toolkit/
index_file: false
sha: ${{ github.event.pull_request.head.sha }}
sources: |
[{
"index_file": null,
"relative_prefix": "/docs/writers-toolkit/",
"repo": "writers-toolkit",
"source_directory": "docs/sources",
"website_directory": "content/docs/writers-toolkit"
}]
title: ${{ github.event.pull_request.title }}
107 changes: 70 additions & 37 deletions .github/workflows/deploy-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,54 @@ name: deploy-preview
on:
workflow_call:
inputs:
sources:
description: |
sources is a JSON string describing multiple source mounts.

It replaces the individual inputs used in the previous version of this action.

The structure is an array of objects, each with the following keys:

index_file:
description: Path to index file used to redirect versioned projects. For example, "content/docs/mimir/_index.md".
required: true
type: string
relative_prefix:
description: The URL path prefix for the mount destination. For example, "/docs/mimir/latest/".
required: true
type: string
repo:
description: The Grafana repository name for the source files. For example, "mimir".
required: true
type: string
source_directory:
description: The path to the source files in the repository. For example, "docs/sources".
required: true
type: string
website_directory:
description: The path to mount the documentation in the website content structure. For example, "content/docs/mimir/latest".
required: true
type: string

The following example mounts both the Mimir documentation and the mimir-distributed Helm chart documentation:

[
{
"index_file": true,
"relative_prefix": "/docs/mimir/latest/",
"repo": "mimir",
"source_directory": "docs/sources/mimir",
"website_directory": "content/docs/mimir/latest"
},
{
"index_file": true,
"relative_prefix": "/docs/helm-charts/mimir-distributed/latest/",
"repo": "mimir",
"source_directory": "docs/sources/mimir-distributed",
"website_directory": "content/docs/helm-charts/mimir-distributed/latest"
}
]
type: string
sha:
required: true
type: string
Expand All @@ -12,24 +60,12 @@ on:
event_number:
required: true
type: string
title:
required: true
type: string
repo:
required: true
type: string
source_directory:
default: docs/sources
type: string
website_directory:
required: true
type: string
relative_prefix:
title:
required: true
type: string
index_file: # creates the necessary project _index.md file for versioned repos
required: true
type: boolean

env:
CLOUD_RUN_REGION: us-south1
Expand All @@ -41,7 +77,7 @@ concurrency:
jobs:
deploy-preview:
permissions:
contents: read
contents: read # Clone repositories.
id-token: write # Fetch Vault secrets.
pull-requests: write # Create or update PR comments.
statuses: write # Update GitHub status check with deploy preview link.
Expand All @@ -66,20 +102,12 @@ jobs:
body: |
:building_construction: Updating deploy preview...

- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
if: github.event.action == 'opened' || github.event.action == 'synchronize'
with:
persist-credentials: false
sparse-checkout-cone-mode: false # exclude root files
sparse-checkout: docs

# get the Dockerfile and nginx conf
- name: Sparse checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
if: github.event.action == 'opened' || github.event.action == 'synchronize'
with:
repository: "grafana/writers-toolkit"
ref: "main"
ref: jdb/deploy-preview
path: deploy-preview-files
persist-credentials: false
sparse-checkout: |
Expand All @@ -90,25 +118,19 @@ jobs:
- name: Keep only necessary files
if: github.event.action == 'opened' || github.event.action == 'synchronize'
shell: bash
env:
SOURCE_DIRECTORY: ${{ inputs.source_directory }}
run: |
shopt -s extglob
rm -rf !(docs|deploy-preview-files|.git)
ls -al
ls -al "${SOURCE_DIRECTORY}"
rm -rf !(deploy-preview-files|.git)
ls -al .
ls -al deploy-preview-files

- name: Build website
if: github.event.action == 'opened' || github.event.action == 'synchronize'
shell: bash
env:
SOURCE_DIRECTORY: ${{ inputs.source_directory }}
WEBSITE_DIRECTORY: ${{ inputs.website_directory }}
INDEX_FILE: ${{ inputs.index_file }}
REPO: ${{ inputs.repo }}
run: |
./deploy-preview-files/deploy-preview/build.sh
GH_TOKEN: ${{ github.token }}
SOURCES: ${{ inputs.SOURCES }}
run: ./deploy-preview-files/deploy-preview/build

- name: Print build header value
if: github.event.action == 'opened' || github.event.action == 'synchronize'
Expand Down Expand Up @@ -204,16 +226,26 @@ jobs:
with:
name: deploy_preview
status: "${{ job.status }}"
url: "${{ steps.deploy.outputs.url }}${{ inputs.relative_prefix }}"
url: "${{ steps.deploy.outputs.url }}${{ (fromJSON(inputs.sources))[0].relative_prefix }}"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think only a single URL here is fine but it might cause some confusion.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I meant to check if I can put multiple URLs into the status check but I don't think I can

description: "Public deploy preview"

- name: Get preview URLs
id: urls
if: github.event.action == 'opened' || github.event.action == 'synchronize'
shell: bash
env:
SOURCES: ${{ inputs.SOURCES }}
URL: ${{ steps.deploy.outputs.url }}
run: ./deploy-preview-files/deploy-preview/urls

- name: Create comment with available deploy preview
if: (github.event.action == 'opened' || github.event.action == 'synchronize') && steps.fc.outputs.comment-id == ''
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
with:
issue-number: ${{ inputs.event_number }}
body: |
:computer: Deploy preview available: ${{ steps.deploy.outputs.url }}${{ inputs.relative_prefix }}
:computer: Deploy preview available:
${{ steps.urls.outputs.urls }}

- name: Update comment with deleted deploy preview
if: github.event.action == 'closed'
Expand All @@ -233,4 +265,5 @@ jobs:
issue-number: ${{ inputs.event_number }}
edit-mode: replace
body: |
:computer: Deploy preview available: ${{ steps.deploy.outputs.url }}${{ inputs.relative_prefix }}
:computer: Deploy preview available:
${{ steps.urls.outputs.urls }}
120 changes: 120 additions & 0 deletions deploy-preview/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env bash

set -euf -o pipefail

if [[ -n "${RUNNER_DEBUG+x}" ]]; then
set -x
fi

# All input comes from environment variables that are capitalized by convention in this script.
readonly SOURCES="${SOURCES:-[]}"

if ! jq -e . <<<"${SOURCES}" >/dev/null; then
echo "SOURCES environment variable is not valid JSON"

exit 1
fi

# Clone a repository to a specific directory.
function clone {
local repo=$1
local directory=$2

if [[ -d "${directory}" ]]; then
echo "Directory ${directory} already exists, skipping clone"

return
fi

gh repo clone "grafana/${repo}" "${directory}"
}

# Check out all the source repositories defined in SOURCES.
function check_out_sources {
index_files=()
while read -r repo; do
if [[ -z "${repo}" ]]; then
continue
fi

clone "${repo}" "src/${repo}"

done < <(jq -r '.[].repo' <<<"${SOURCES}")
}

# Create an entrypoint script for the container.
function prepare_entrypoint {
tempfile="$(mktemp -t deploy-preview.XXX)"
# Pull out the relevant fields from the JSON because the grafana/docs-base image doesn't have jq.
index_files=()
while read -r index_file relative_prefix; do
if [[ "${index_file}" != 'null' ]]; then
index_files+=("${index_file}:${relative_prefix}")
fi
done < <(jq -r '.[] | "\(.index_file) \(.relative_prefix)"' <<<"${SOURCES}")
cat <<EOSCRIPT >"${tempfile}"
#!/usr/bin/env bash

# Create an index file to redirect a project root to the correct versioned URL.
for pair in ${index_files[@]}; do
IFS=':' read -r index_file relative_prefix <<<"\${pair}"
dst="/hugo/\${index_file}"
parent="\${dst%/*}"
echo "Creating custom index: \${dst} -> \${relative_prefix}"

title="\${dst%/*}"
while [[ -n "\${parent}" ]]; do
if [[ ! -f "\${parent}/_index.md" ]]; then
cat > "\${parent}/_index.md" <<EOPARENT
---
title: \${title}
---

# \${title}

{{< section >}}
EOPARENT
fi
parent="\${parent%/*}"
done

cat > "\${dst}" <<EOINDEX
---
type: redirect
redirectURL: \${relative_prefix}
versioned: true
---
EOINDEX
cat "\${dst}"
done

HUGO_SSI=false hugo --environment=docs --destination=dist/ --baseURL= --minify
EOSCRIPT
chmod +x "${tempfile}"
}

# Add mounts to the volumes variable.
function configure_volumes {
volumes=("--volume=${PWD}/dist:/hugo/dist" "--volume=${tempfile}:/entrypoint:z")
while read -r repo source_directory website_directory; do
volumes+=("--volume=${PWD}/src/${repo}/${source_directory}:/hugo/${website_directory}:z")
done < <(jq -r '.[] | "\(.repo) \(.source_directory) \(.website_directory)"' <<<"${SOURCES}")
}

# Prepare the command to run the container.
function prepare_cmd {
cat <<EOF
docker run \
${volumes[@]} \
--rm grafana/docs-base:latest \
/entrypoint
EOF
}

check_out_sources
prepare_entrypoint
declare -a volumes; configure_volumes; readonly volumes
cmd=$(prepare_cmd)

echo "${cmd}"
${cmd}
24 changes: 0 additions & 24 deletions deploy-preview/build.sh

This file was deleted.

24 changes: 24 additions & 0 deletions deploy-preview/urls
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

set -euf -o pipefail

if [[ -n "${RUNNER_DEBUG+x}" ]]; then
set -x
fi

# All input comes from environment variables that are capitalized by convention in this script.
readonly GITHUB_OUTPUT="${GITHUB_OUTPUT:-/dev/stdout}"
readonly SOURCES="${SOURCES:-[]}"
readonly URL="${URL:-}"

if ! jq -e . <<<"${SOURCES}" >/dev/null; then
echo "SOURCES environment variable is not valid JSON"

exit 1
fi

echo "urls<<EOF" >> "${GITHUB_OUTPUT}"
while read -r relative_prefix; do
echo "- ${URL}${relative_prefix}" >> "${GITHUB_OUTPUT}"
done < <(jq -r '.[].relative_prefix' <<<"${SOURCES}")
echo "EOF" >> "${GITHUB_OUTPUT}"
2 changes: 2 additions & 0 deletions docs/sources/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ If it's your first time using the guide, start with the [Get started](https://gr

Writers' Toolkit is open source and available at [`grafana/writers-toolkit`](https://github.com/grafana/writers-toolkit).
If you have questions, or feedback on how to improve this documentation, [open an issue](https://github.com/grafana/writers-toolkit/issues/new) and help make this an even better resource.

Test deploy preview