Push ODTP YAML to Zoo within the Organization #5
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: Push ODTP YAML to Zoo within the Organization | |
on: | |
workflow_dispatch: | |
env: | |
UPSTREAM_REPO: "odtp-org/odtp-zoo" # Original repo to fork from | |
FORKED_REPO: "${{ github.repository_owner }}/odtp-zoo" # Forked repo under your username | |
COMPONENTS_BRANCH: "components" # Branch name in upstream repo for PR target | |
NEW_BRANCH_NAME: "update-components-${{ github.sha }}" # New branch for this update | |
SOURCE_FILE: "odtp.yml" # The file to rename and copy | |
TARGET_FOLDER: "components" # Target folder in fork | |
GITHUB_USERNAME: "${{ github.repository_owner }}" # Your GitHub username | |
jobs: | |
fork_and_pr: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Fork Repository if Not Already Forked | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
# Fork the upstream repository if it doesn't exist in the user's account | |
curl -X POST -H "Authorization: token $GITHUB_TOKEN" \ | |
https://api.github.com/repos/${{ env.UPSTREAM_REPO }}/forks || echo "Fork already exists." | |
- name: Checkout Source Repository | |
uses: actions/checkout@v3 | |
with: | |
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }} | |
- name: Extract Component Name and Version | |
id: extract_values | |
run: | | |
component_name=$(yq e '.component-name' ${{ env.SOURCE_FILE }}) | |
component_version=$(yq e '.component-version' ${{ env.SOURCE_FILE }}) | |
echo "Component Name: $component_name" | |
echo "Component Version: $component_version" | |
echo "component_name=$component_name" >> $GITHUB_ENV | |
echo "component_version=$component_version" >> $GITHUB_ENV | |
- name: Rename File | |
run: | | |
mv ${{ env.SOURCE_FILE }} "${component_name}_${component_version}.yml" | |
echo "Renamed file to ${component_name}_${component_version}.yml" | |
- name: Checkout Forked Repository | |
uses: actions/checkout@v3 | |
with: | |
repository: ${{ env.FORKED_REPO }} | |
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }} | |
ref: main # Check out the main branch of your fork | |
path: forked-repo | |
- name: Create New Branch | |
run: | | |
cd forked-repo | |
git checkout -b ${{ env.NEW_BRANCH_NAME }} | |
- name: Copy Renamed File to Components Folder | |
run: | | |
mkdir -p forked-repo/${{ env.TARGET_FOLDER }} | |
cp "${component_name}_${component_version}.yml" forked-repo/${{ env.TARGET_FOLDER }}/ | |
- name: Commit and Push Changes to New Branch in Fork | |
run: | | |
cd forked-repo | |
git config user.name "github-actions[bot]" | |
git config user.email "github-actions[bot]@users.noreply.github.com" | |
git add ${{ env.TARGET_FOLDER }}/"${component_name}_${component_version}.yml" | |
git commit -m "Add component file: ${component_name}_${component_version}.yml" | |
git push origin ${{ env.NEW_BRANCH_NAME }} | |
- name: Create Pull Request to Upstream | |
uses: peter-evans/create-pull-request@v5 | |
with: | |
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }} | |
repository: ${{ env.UPSTREAM_REPO }} | |
head: "${{ env.GITHUB_USERNAME }}:${{ env.NEW_BRANCH_NAME }}" # Branch in your fork | |
base: ${{ env.COMPONENTS_BRANCH }} # Target branch in upstream repo | |
title: "Add component file: ${component_name}_${component_version}.yml" | |
body: "This PR adds the component file for ${component_name} version ${component_version}." |