Skip to content

Commit

Permalink
Update CI to merge automated PR is one already exists for the workflow
Browse files Browse the repository at this point in the history
Signed-off-by: gabriel-farache <[email protected]>
  • Loading branch information
gabriel-farache committed Oct 10, 2024
1 parent 6ff1e93 commit 9f9bf19
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 40 deletions.
37 changes: 18 additions & 19 deletions .github/workflows/m2k-func.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ jobs:
- name: Send PRs to config repo
if: ${{ ! inputs.it_mode }}
run: |
# determine pr or commit url
if [ ${{ fromJSON(steps.get_pr_data.outputs.result)}} != "" ]; then
PR_OR_COMMIT_URL="${{ fromJSON(steps.get_pr_data.outputs.result).html_url }}"
else
PR_OR_COMMIT_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
fi
git config --global user.email "${{ github.event.head_commit.author.email || '[email protected]' }}"
git config --global user.name ${{ github.event.head_commit.author.name || github.triggering_actor }}
gh repo clone $WF_CONFIG_REPO config-repo
Expand All @@ -94,23 +101,15 @@ jobs:
./hack/bump_chart_version.sh move2kube --bump-tag-version
yq --inplace '.kfunction.image="quay.io/orchestrator/serverless-workflow-m2k-kfunc:${{ github.sha }}"' charts/move2kube/values.yaml
cd ..
git add -A
# determine pr or commit url
if [ ${{ fromJSON(steps.get_pr_data.outputs.result)}} != "" ]; then
PR_OR_COMMIT_URL="${{ fromJSON(steps.get_pr_data.outputs.result).html_url }}"
else
PR_OR_COMMIT_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
fi
git commit -m "(m2k-kfunc) Automated PR for m2k-kfunc"
echo "Automated PR from $PR_OR_COMMIT_URL" | git commit --amend --file=-
git remote set-url origin https://${{ env.GH_TOKEN }}@github.com/$WF_CONFIG_REPO
git push origin HEAD
gh pr create -f --title "m2k-kfunc: Automatic m2k-kfunc image tag bump" \
--body "
Updating generated image tag for m2k-kfunc application
This PR was created automatically as a result of merging $PR_OR_COMMIT_URL
"
sh scripts/create_or_update_automated_pr.sh $WF_CONFIG_REPO \
${{ inputs.workflow_id }} \
${PR_OR_COMMIT_URL} \
config-repo/charts/move2kube/values.yaml \
scripts/create_automated_m2k_kfunc_pr.sh \
${{ github.event.head_commit.author.email || '[email protected]' }} \
${{ github.event.head_commit.author.name || github.triggering_actor }} \
$WORKDIR \
${{ env.GH_TOKEN }} \
$WF_CONFIG_REPO
32 changes: 11 additions & 21 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,6 @@ jobs:
if: ${{ ! inputs.it_mode }}
run: |
WORKDIR=${{ runner.temp }}/serverless-workflows
git config --global user.email "${{ github.event.head_commit.author.email || '[email protected]' }}"
git config --global user.name ${{ github.event.head_commit.author.name || github.triggering_actor }}
gh repo clone $WF_CONFIG_REPO config-repo
cd config-repo
git switch -c ${{ inputs.workflow_id }}-autopr-$RANDOM
./hack/bump_chart_version.sh ${{ inputs.workflow_id }} --bump-tag-version
mkdir -p charts/${{ inputs.workflow_id }}/templates
cp ${WORKDIR}/${{ inputs.workflow_id }}/manifests/* charts/${{ inputs.workflow_id }}/templates
git add -A
# determine pr or commit url
if [ ${{ fromJSON(steps.get_pr_data.outputs.result)}} != "" ]; then
Expand All @@ -131,13 +120,14 @@ jobs:
PR_OR_COMMIT_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
fi
git commit -m "(${{ inputs.workflow_id }}) Automated PR"
echo "Automated PR from $PR_OR_COMMIT_URL" | git commit --amend --file=-
git remote set-url origin https://${{ env.GH_TOKEN }}@github.com/$WF_CONFIG_REPO
git push origin HEAD
gh pr create -f --title "${{ inputs.workflow_id }}: Automatic manifests generation" \
--body "
Updating generated manifests for ${{ inputs.workflow_id }} workflow
This PR was created automatically as a result of merging $PR_OR_COMMIT_URL
"
sh scripts/create_or_update_automated_pr.sh $WF_CONFIG_REPO \
${{ inputs.workflow_id }} \
${PR_OR_COMMIT_URL} \
"" \
scripts/create_automated_pr.sh \
${{ github.event.head_commit.author.email || '[email protected]' }} \
${{ github.event.head_commit.author.name || github.triggering_actor }} \
$WORKDIR \
${{ env.GH_TOKEN }} \
$WF_CONFIG_REPO
34 changes: 34 additions & 0 deletions scripts/create_automated_m2k_kfunc_pr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
USER_EMAIL=$1
USER_NAME=$2
WORKDIR=$3
WORKFLOW_ID=$4
PR_OR_COMMIT_URL=$5
GH_TOKEN=$6
WF_CONFIG_REPO=$7
INPUT_VALUES_FILEPATH=$8

git config --global user.email "${USER_EMAIL}"
git config --global user.name "${USER_NAME}"
gh repo clone "${WF_CONFIG_REPO}" config-repo
cd config-repo || exit
git switch -c "${WORKFLOW_ID}"-autopr-"${RANDOM}"

./../hack/bump_chart_version.sh "${WORKFLOW_ID}" --bump-tag-version

cp "${INPUT_VALUES_FILEPATH}" charts/move2kube/values.yaml

mkdir -p charts/"${WORKFLOW_ID}"/templates
cp "${WORKDIR}"/"${WORKFLOW_ID}"/manifests/* charts/"${WORKFLOW_ID}"/templates
git add -A

git commit -m "(m2k-kfunc) Automated PR"
echo "Automated PR from ${PR_OR_COMMIT_URL}" | git commit --amend --file=-
git remote set-url origin https://"${GH_TOKEN}"@github.com/"${WF_CONFIG_REPO}"
git push origin HEAD
gh pr create -f --title "m2k-kfunc: Automatic manifests generation" \
--body "
Updating generated manifests for m2k-kfunc application
This PR was created automatically as a result of merging ${PR_OR_COMMIT_URL}
"
30 changes: 30 additions & 0 deletions scripts/create_automated_pr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash
USER_EMAIL=$1
USER_NAME=$2
WORKDIR=$3
WORKFLOW_ID=$4
PR_OR_COMMIT_URL=$5
GH_TOKEN=$6
WF_CONFIG_REPO=$7

git config --global user.email "${USER_EMAIL}"
git config --global user.name "${USER_NAME}"
gh repo clone "${WF_CONFIG_REPO}" config-repo
cd config-repo || exit
git switch -c "${WORKFLOW_ID}"-autopr-"${RANDOM}"

./../hack/bump_chart_version.sh "${WORKFLOW_ID}" --bump-tag-version
mkdir -p charts/"${WORKFLOW_ID}"/templates
cp "${WORKDIR}"/"${WORKFLOW_ID}"/manifests/* charts/"${WORKFLOW_ID}"/templates
git add -A

git commit -m "(${WORKFLOW_ID}) Automated PR"
echo "Automated PR from $PR_OR_COMMIT_URL" | git commit --amend --file=-
git remote set-url origin https://"${GH_TOKEN}"@github.com/"${WF_CONFIG_REPO}"
git push origin HEAD
gh pr create -f --title "${WORKFLOW_ID}: Automatic manifests generation" \
--body "
Updating generated manifests for ${WORKFLOW_ID} workflow
This PR was created automatically as a result of merging ${PR_OR_COMMIT_URL}
"
67 changes: 67 additions & 0 deletions scripts/create_or_update_automated_pr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash

# Set repository (update with your repo)
REPO=$1
WORKFLOW_ID=$2
PR_OR_COMMIT_URL=$3
INPUT_VALUES_FILEPATH=$4

CREATE_PR_SCRIPT=$5
USER_EMAIL=$6
USER_NAME=$7
WORKDIR=$8
GH_TOKEN=$9
WF_CONFIG_REPO=${10}

# Get all open PRs for the repository
prs=$(gh pr list --repo "${REPO}" --json number --jq '.[].number')

# Loop through each PR and check for the version in Chart.yaml
for pr in $prs; do
echo "Checking PR #$pr..."

# Fetch the list of changed files for the PR
files=$(gh pr diff "$pr" --name-only --repo "${REPO}")

# Check if Chart.yaml is in the list of changed files
if echo "$files" | grep -q "charts/${WORKFLOW_ID}/Chart.yaml"; then
echo "Chart.yaml found in PR #${pr}"

# Get the contents of the Chart.yaml file from the PR
chart_content=$(gh pr diff "$pr" --repo "${REPO}" -- "charts/${WORKFLOW_ID}/Chart.yaml")

# Check if the content has a version entry
if echo "$chart_content" | grep -q "^version: .*"; then
# Get the PR details (branch)
pr_branch=$(gh pr view "$pr" --repo "$REPO" --json headRefName --jq '.headRefName')

# Checkout the PR branch
git fetch origin "$pr_branch"
git checkout "$pr_branch"

# Create the new file
cp "${WORKDIR}/${WORKFLOW_ID}/manifests/*" "charts/${WORKFLOW_ID}/templates"
if [ "${INPUT_VALUES_FILEPATH}" != "" ]; then
cp "${INPUT_VALUES_FILEPATH}" "charts/${WORKFLOW_ID}/values.yaml"
fi
git add -A

git commit -m "(${WORKFLOW_ID}) Automated PR"
echo "Automated PR from $PR_OR_COMMIT_URL" | git commit --amend --file=-

git push origin "$pr_branch"

echo "Changes pushed to PR #$pr on branch $pr_branch"

exit 0
fi
fi
done

sh "${CREATE_PR_SCRIPT}" "${USER_EMAIL}" \
"${USER_NAME}" \
"${WORKDIR}" \
"${WORKFLOW_ID}" \
"${PR_OR_COMMIT_URL}" \
"${GH_TOKEN}" \
"${WF_CONFIG_REPO}"

0 comments on commit 9f9bf19

Please sign in to comment.