Fix external repo-banner creation #33
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: GitHub Repo Banner | |
on: | |
pull_request: | |
types: [opened, synchronize, reopened] | |
jobs: | |
generate: | |
runs-on: ubuntu-latest | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | |
cancel-in-progress: true | |
permissions: | |
# Give the default GITHUB_TOKEN write permission to commit and push the | |
# added or changed files to the repository. | |
contents: write | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Fetch repo metadata | |
id: metadata | |
uses: ahmadnassri/action-metadata@v2 | |
- name: Checkout ${{ steps.metadata.outputs.owner_login}}/.github | |
if: steps.metadata.outputs.repository_name != '.github' | |
uses: actions/checkout@v3 | |
with: | |
repository: '${{ steps.metadata.outputs.owner_login }}/.github' | |
ref: 'fix-banner-workflow' | |
path: 'github' | |
- name: Symlink ${{github.workspace}} to `github/` | |
if: steps.metadata.outputs.repository_name == '.github' | |
run: | | |
ln -s ${{github.workspace}} ${{github.workspace}}/github | |
- uses: actions-tools/yaml-outputs@v2 | |
id: readme | |
with: | |
file-path: 'README.yaml' | |
fail-on-file-not-found: false | |
- name: Format Repo Metadata | |
id: meta | |
shell: zsh | |
env: | |
LANG: en_US.UTF-8 | |
run: | | |
export LANG | |
# Function to wrap emojis in a <span> tag | |
function wrapEmoji() { | |
emoji="\U1f300-\U1f5ff\U1f900-\U1f9ff\U1f600-\U1f64f\U1f680-\U1f6ff\U2600-\U26ff\U2700-\U27bf\U1f1e6-\U1f1ff\U1f191-\U1f251\U1f004\U1f0cf\U1f170-\U1f171\U1f17e-\U1f17f\U1f18e\U3030\U2b50\U2b55\U2934-\U2935\U2b05-\U2b07\U2b1b-\U2b1c\U3297\U3299\U303d\U00a9\U00ae\U2122\U23f3\U24c2\U23e9-\U23ef\U25b6\U23f8-\U23fa" | |
sed -e "s/\([$(printf $emoji)]\)/<span class=\"emoji\">\1<\/span>/g" <<<"$*" | |
} | |
# Pull the name from the README | |
name="${{ steps.readme.outputs.name }}" | |
if [ -z "${name}" ]; then | |
# If the name was empty, then default to the repository_name | |
name="${{ steps.metadata.outputs.repository_name }}" | |
fi | |
type="Project" | |
desc="${{ steps.metadata.outputs.repository_description }}" | |
output=.github/banner.png | |
if [[ "${name}" == "terraform-*-provider" ]]; then | |
type="Terraform Provider" | |
echo "name=${name#terraform-}" | |
elif [[ "${name}" == "terraform-aws*" ]]; then | |
echo "type=Terraform Module" | |
echo "name=${name#terraform-}" | |
elif [[ "${name}" == "github-action-*" ]]; then | |
echo "type=GitHub Action" | |
echo "name=${name#github-action-}" | |
elif [[ "${name}" == "example-*" ]]; then | |
type=Example" | |
name=${name#example-}" | |
elif [[ "${name}" == "infra-*" ]]; then | |
type=Infrastructure" | |
name=${name#infra-}" | |
elif [[ "${name}" == ".github" ]]; then | |
type=Organization | |
output="banner/image.png" | |
else | |
echo "Repo type is indeterminable" | |
fi | |
if [[ "${desc}" == "null" ]]; then | |
# Change it to an empty string | |
desc="" | |
else | |
# Only keep the first sentence and wrap emojis in a <span/> tag. | |
# Ths span tag is added so we can style the emoji | |
desc="${desc/.*}" | |
fi | |
echo "type=$(wrapEmoji ${type})" >> $GITHUB_OUTPUT | |
echo "name=$(wrapEmoji ${name})" >> $GITHUB_OUTPUT | |
echo "desc=$(wrapEmoji ${desc})" >> $GITHUB_OUTPUT | |
echo "output=${output}" >> $GITHUB_OUTPUT | |
cat ${GITHUB_OUTPUT} | |
- name: Generate banner image | |
id: screenshot | |
uses: cloudposse-github-actions/screenshot@main | |
with: | |
url: "file://${{github.workspace}}/github/banner/index.html" | |
output: "${{ steps.meta.outputs.output }}" | |
customizations: | | |
"#name": "${{ steps.meta.outputs.name }}" | |
"#type": "${{ steps.meta.outputs.type }}" | |
"#desc": "${{ steps.meta.outputs.desc }}" | |
viewportWidth: 1280 | |
viewportHeight: 320 | |
omitBackground: true | |
- uses: stefanzweifel/git-auto-commit-action@v4 | |
name: Commit artifact | |
id: auto-commit | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
commit_message: "chore: update repo banner image" | |
commit_user_name: screenshot-action 📷 | |
commit_user_email: [email protected] | |
commit_author: screenshot-action 📷 <[email protected]> | |
file_pattern: 'banner/*.png' | |
- name: Add Image to Step Summary | |
if: steps.auto-commit.outputs.changes_detected == 'true' | |
run: | | |
echo "## Generated Screenshot" >> $GITHUB_STEP_SUMMARY | |
echo "![Generated Screenshot](https://github.com/${{ github.repository }}/blob/${{ steps.auto-commit.outputs.commit_hash }}/${{ steps.screenshot.outputs.file }}?raw=true)" >> $GITHUB_STEP_SUMMARY | |
- name: No changes | |
if: steps.auto-commit.outputs.changes_detected == 'false' | |
run: | | |
echo "No changes to screenshot" >> $GITHUB_STEP_SUMMARY |