Skip to content

Commit d12d5c5

Browse files
authored
CLOUDP-307033: Sign also release PRs and SBOMs (#2289)
* CLOUDP-307033: Sign also releas PRs and SBOMs * trace create commit * Send blobs from files * Sync remote created commit locally * Allow set a different upstream branch * force clean * fetch remote * Debug changing the branch reference * Ensure target branch exists, no need for upstream Signed-off-by: jose.vazquez <[email protected]> * Checkout and pull local if present * Create remote branch on demand Signed-off-by: jose.vazquez <[email protected]> * pull rebase * Don't pull * Remove reviewers * Remove traces --------- Signed-off-by: jose.vazquez <[email protected]>
1 parent 35ff9f3 commit d12d5c5

File tree

6 files changed

+59
-76
lines changed

6 files changed

+59
-76
lines changed

.github/actions/create-pr/Dockerfile

-13
This file was deleted.

.github/actions/create-pr/action.yml

-13
This file was deleted.

.github/actions/create-pr/entrypoint.sh

-25
This file was deleted.

.github/workflows/release-branch.yml

+15-10
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,28 @@ jobs:
5151
- name: Configure Git
5252
run: |
5353
git remote set-url origin https://${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git
54-
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
55-
git config --global user.name "github-actions[bot]"
5654
5755
- name: Create branch and push it
56+
id: generate_branch
57+
env:
58+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5859
run: |
5960
devbox run -- 'make check-major-version'
60-
BRANCH="release/${VERSION}"
61+
export BRANCH="release/${VERSION}"
62+
export COMMIT_MESSAGE="Release ${VERSION}"
6163
git checkout -b $BRANCH
6264
git add -f ./deploy ./bundle bundle.Dockerfile docs/releases
63-
git commit -m "Release ${VERSION}"
64-
git reset --hard
65-
git push --set-upstream origin $BRANCH
65+
scripts/create-signed-commit.sh
66+
echo "BRANCH=$BRANCH" >> $GITHUB_OUTPUT # Export branch name
67+
echo "COMMIT_MESSAGE=$COMMIT_MESSAGE" >> $GITHUB_OUTPUT # Export commit message
6668
6769
- name: Create PR
68-
uses: ./.github/actions/create-pr
6970
env:
7071
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
71-
REVIEWERS: ${{ env.REVIEWERS }}
72-
with:
73-
REVIEWERS: ${{ env.REVIEWERS }}
72+
BRANCH: ${{ steps.generate_branch.outputs.BRANCH }}
73+
COMMIT_MESSAGE: ${{ steps.generate_branch.outputs.COMMIT_MESSAGE }}
74+
run: |
75+
gh pr create --head="${BRANCH}" \
76+
--title "${COMMIT_MESSAGE}" \
77+
--body "This is an autogenerated PR to prepare for the release" \
78+
&& echo "Pull request created"

.github/workflows/sboms-pr.yaml

+13-9
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,30 @@ jobs:
3131
- name: Configure Git
3232
run: |
3333
git remote set-url origin https://${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git
34-
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
35-
git config --global user.name "github-actions[bot]"
3634
3735
- name: Create SBOMs branch
36+
id: generate_branch
3837
env:
3938
BRANCH: version-${{ env.VERSION }}-sboms
4039
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
40+
COMMIT_MESSAGE: "Add SBOMs for version ${{ env.VERSION }}"
4141
run: |
4242
OS=linux ARCH=amd64 DOCKER_SBOM_PLUGIN_VERSION=0.6.1 ./scripts/get-docker-sbom-plugin.sh
4343
devbox run -- make generate-sboms VERSION=${{ env.VERSION }}
4444
git checkout -b $BRANCH
4545
git add docs/releases
46-
git commit -m "Add SBOMs for version ${{ env.VERSION }}"
47-
git push --set-upstream origin $BRANCH
46+
scripts/create-signed-commit.sh
47+
echo "BRANCH=$BRANCH" >> $GITHUB_OUTPUT # Export branch name
48+
echo "COMMIT_MESSAGE=$COMMIT_MESSAGE" >> $GITHUB_OUTPUT # Export commit message
4849
4950
- name: Create SBOMs PR
50-
uses: ./.github/actions/create-pr
5151
env:
5252
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
53-
REVIEWERS: ${{ env.REVIEWERS }}
54-
with:
55-
REVIEWERS: ${{ env.REVIEWERS }}
56-
TITLE_PREFIX: "SBOMs for version "
53+
BRANCH: ${{ steps.generate_branch.outputs.BRANCH }}
54+
COMMIT_MESSAGE: ${{ steps.generate_branch.outputs.COMMIT_MESSAGE }}
55+
VERSION: ${{ env.VERSION }}
56+
run: |
57+
gh pr create --head="${BRANCH}" \
58+
--title "${COMMIT_MESSAGE}" \
59+
--body ""SBOMs for version ${VERSION}" \
60+
&& echo "Pull request created"

scripts/create-signed-commit.sh

+31-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ repo_owner="${REPO_OWNER:-mongodb}"
2525
repo_name="${REPO_NAME:-mongodb-atlas-kubernetes}"
2626
branch="${BRANCH:?}"
2727
commit_message="${COMMIT_MESSAGE:?}"
28+
remote=${REMOTE:-origin}
29+
30+
# Ensure remote branch exists
31+
echo "Check remote branch ${branch} exists..."
32+
33+
function create_branch() {
34+
echo "Creating branch ${remote}/${branch}"
35+
git stash
36+
git push "${remote}" "${branch}"
37+
git stash pop
38+
git add .
39+
}
40+
41+
git ls-remote --exit-code --heads origin "${branch}" >/dev/null || create_branch
2842

2943
# Fetch the latest commit SHA
3044
LATEST_COMMIT_SHA=$(curl -s -H "Authorization: token $github_token" \
@@ -47,12 +61,14 @@ for FILE_PATH in $MODIFIED_FILES; do
4761
# Read file content encoded to base64
4862
ENCODED_CONTENT=$(base64 -w0 < "${FILE_PATH}")
4963

50-
# Create blob
64+
# Create blob
65+
echo "{\"content\": \"$ENCODED_CONTENT\", \"encoding\": \"base64\"}" > content.blob
5166
BLOB_JSON=$(curl -s -X POST -H "Authorization: token $github_token" \
5267
-H "Accept: application/vnd.github.v3+json" \
53-
-d "{\"content\": \"$ENCODED_CONTENT\", \"encoding\": \"base64\"}" \
68+
-d @content.blob \
5469
"https://api.github.com/repos/$repo_owner/$repo_name/git/blobs")
5570
BLOB_SHA=$(echo "${BLOB_JSON}" | jq -r '.sha')
71+
rm content.blob
5672

5773
# Append file info to tree JSON
5874
NEW_TREE_ARRAY="${NEW_TREE_ARRAY}{\"path\": \"$FILE_PATH\", \"mode\": \"100644\", \"type\": \"blob\", \"sha\": \"$BLOB_SHA\"},"
@@ -61,11 +77,13 @@ done
6177
# Remove trailing comma and close the array
6278
NEW_TREE_ARRAY="${NEW_TREE_ARRAY%,}]"
6379

64-
# Create new tree
80+
# Create new tree
81+
echo "{\"base_tree\": \"$LATEST_TREE_SHA\", \"tree\": $NEW_TREE_ARRAY}" > new_tree_spec.json
6582
NEW_TREE_SHA=$(curl -s -X POST -H "Authorization: token $github_token" \
6683
-H "Accept: application/vnd.github.v3+json" \
67-
-d "{\"base_tree\": \"$LATEST_TREE_SHA\", \"tree\": $NEW_TREE_ARRAY}" \
84+
-d @new_tree_spec.json \
6885
"https://api.github.com/repos/$repo_owner/$repo_name/git/trees" | jq -r '.sha')
86+
rm new_tree_spec.json
6987

7088
echo "New tree SHA: $NEW_TREE_SHA"
7189

@@ -76,9 +94,16 @@ NEW_COMMIT_SHA=$(curl -s -X POST -H "Authorization: token $github_token" \
7694
"https://api.github.com/repos/$repo_owner/$repo_name/git/commits" | jq -r '.sha')
7795
echo "New commit SHA: $NEW_COMMIT_SHA"
7896

79-
# Update the reference of the branch to point to the new commit
97+
# Update the reference of the branch to point to the new commit
8098
curl -s -X PATCH -H "Authorization: token $github_token" \
8199
-H "Accept: application/vnd.github.v3+json" -d "{\"sha\": \"$NEW_COMMIT_SHA\"}" \
82-
"https://api.github.com/repos/$repo_owner/$repo_name/git/refs/heads/$branch"
100+
"https://api.github.com/repos/$repo_owner/$repo_name/git/refs/heads/$branch"
83101

84102
echo "Branch ${branch} updated to new commit ${NEW_COMMIT_SHA}."
103+
git restore --staged .
104+
git restore .
105+
git clean -f
106+
git fetch "${remote}"
107+
git checkout -b "${branch}" "${remote}/${branch}" || git checkout "${branch}"
108+
109+
echo "Local branch set to ${remote}/${branch}"

0 commit comments

Comments
 (0)