-
Notifications
You must be signed in to change notification settings - Fork 68
134 lines (124 loc) · 5.34 KB
/
metadata-docs.yml
File metadata and controls
134 lines (124 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# This GH action has the goal to fetch the pattern-metadata.yaml file (if existing)
# and output all of its flattened yaml structure into asciidoc variables for the pattern
#
# It needs a secret called DOCS_TOKEN to be set in the pattern's repository
# that calls this GH action. It has to be a PAT token with the following
# permissions on the `validatedpatterns/docs` repository
# * Read access to actions and metadata
# * Read and Write access to code and pull requests
#
# This job will checkout the docs repo and propose a PR to where it updates
# the file ./modules/<pattern>/metadata-<pattern>.yaml where the pattern's
# metadata will land transformed into asciidoc variables.
#
# Note: This action is to be imported from a pattern and not used in the docs repo
# itself. We maintain it in the docs repo in order to make it easier to have a single
# workflow across all patterns
---
name: Update docs from pattern's metadata
on:
workflow_call:
secrets:
METADATA_SYNC_PRIVATE_KEY:
required: false
description: The private key for the Validated Patterns Metadata Sync app (must be set if DOCS_TOKEN is not)
DOCS_TOKEN:
required: false
description: PAT with read/write (content and PRs) for validatedpatterns/docs repo (must be set if METADATA_SYNC_PRIVATE_KEY is not)
inputs:
DOCS_BRANCH:
description: "Branch of the docs git repo to use"
required: false
type: string
default: "main"
METADATA_SYNC_APP_ID:
description: "Validated Patterns Metadata Sync app id"
required: false
type: string
default: "3329715"
env:
DOCS_DIR: docs
PATTERN_DIR: pattern
METADATA: pattern-metadata.yaml
GIT_EMAIL: vp-team@redhat.com
GIT_USER: Github Actions
jobs:
docs-push:
# We do not want to run this job on forked repositories
if: |
github.repository_owner == 'validatedpatterns' ||
github.repository_owner == 'validatedpatterns-sandbox' ||
github.repository_owner == 'validatedpatterns-demos'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout pattern repository
uses: actions/checkout@v6
with:
path: ${{ env.PATTERN_DIR }}
- name: Fail if this repository is different than the one in metadata
run: |-
set -e
repo=$(yq -r .repo_url "${{ env.METADATA }}")
full_url="https://github.com/${{ github.repository }}"
if [ "${full_url}" != "${repo}" ]; then
echo "Error ${repo} != ${full_url}"
exit 1
fi
docs_repo=$(yq -r .docs_repo_url "${{ env.METADATA }}" | sed -e 's%https://github.com/%%')
pattern=$(yq -r .name "${{ env.METADATA }}")
{
echo "DOCS_PR_BRANCH=sizing-pr-${pattern}"
echo "DOCS_REPO=${docs_repo}"
echo "PATTERN=${pattern}"
} >> "${GITHUB_ENV}"
working-directory: ${{ env.PATTERN_DIR }}
- name: Generate token with Metadata Sync GitHub App
uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3.1.1
id: app-token
with:
client-id: ${{ vars.METADATA_SYNC_APP_ID }}
private-key: ${{ secrets.METADATA_SYNC_PRIVATE_KEY }}
owner: validatedpatterns
repositories: docs
continue-on-error: true
- name: Resolve Documentation Token
# Use App token if available, else fall back to the legacy secret
run: |
if [ -n "${{ steps.app-token.outputs.token }}" ]; then
echo "FINAL_TOKEN=${{ steps.app-token.outputs.token }}" >> $GITHUB_ENV
else
echo "FINAL_TOKEN=${{ secrets.DOCS_TOKEN }}" >> $GITHUB_ENV
fi
- name: Checkout docs repository
uses: actions/checkout@v6
with:
path: ${{ env.DOCS_DIR }}
repository: ${{ env.DOCS_REPO }}
ref: ${{ inputs.DOCS_BRANCH }}
token: ${{ env.FINAL_TOKEN }}
- name: Template the cluster variables on to the patterns-variables .adoc file
run: |-
set -e
mkdir -p "${{ env.DOCS_DIR }}/modules/${{ env.PATTERN }}"
./${{ env.DOCS_DIR }}/utils/flatten_yaml.rb \
./${{ env.PATTERN_DIR }}/${{ env.METADATA }} | \
tee "${{ env.DOCS_DIR }}/modules/${{ env.PATTERN }}/metadata-${{ env.PATTERN }}.adoc"
- name: Push to docs git repo
run: |-
set -e
git config --global user.email "${{ env.GIT_EMAIL }}"
git config --global user.name "${{ env.GIT_USER }}"
git checkout -B "${{ env.DOCS_PR_BRANCH }}" "${{ inputs.DOCS_BRANCH }}"
git add modules/${{ env.PATTERN }}/metadata-${{ env.PATTERN }}.adoc
git commit -m "Update cluster variables for ${{ env.PATTERN }}" || (echo "Nothing to commit"; exit 0)
git push origin "${{ env.DOCS_PR_BRANCH }}" -f
gh pr create -B "${{ inputs.DOCS_BRANCH }}" -H "${{ env.DOCS_PR_BRANCH }}" \
--title 'Merge cluster variables change for ${{ env.PATTERN }}' --body 'Created by Github action' || \
gh pr edit -B "${{ inputs.DOCS_BRANCH }}" --title 'Cluster variables change for ${{ env.PATTERN }}' --body 'Created by Github action'
working-directory: ${{ env.DOCS_DIR }}
env:
GITHUB_TOKEN: ${{ env.FINAL_TOKEN }}