Skip to content

Commit

Permalink
fix: check for existing github releases (#732)
Browse files Browse the repository at this point in the history
The grep was inadequate, because it would also match for substrings.
Now we'll use github api and check the string directly

Signed-off-by: Martin Malina <[email protected]>
  • Loading branch information
mmalina authored Dec 6, 2024
1 parent 13fe5ad commit 2fc609f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
7 changes: 6 additions & 1 deletion tasks/create-github-release/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ a `release` dir.
| content_directory | The directory inside the workspace to find files for release | No | - |
| resultsDirPath | Path to results directory in the data workspace | No | - |

## Changes in 2.2.1
* Fix check for existing release
* The grep was inadequate, because it would also match for substrings. Now we'll
use github api and check the string directly

## Changes in 2.2.0
* make task idempotent
* Make task idempotent

## Changes in 2.1.1
* Fixed shellcheck linting issues
Expand Down
12 changes: 7 additions & 5 deletions tasks/create-github-release/create-github-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ kind: Task
metadata:
name: create-github-release
labels:
app.kubernetes.io/version: "2.2.0"
app.kubernetes.io/version: "2.2.1"
annotations:
tekton.dev/pipelines.minVersion: "0.12.1"
tekton.dev/tags: release
Expand Down Expand Up @@ -42,15 +42,17 @@ spec:
set -ex
RESULTS_FILE="$(workspaces.data.path)/$(params.resultsDirPath)/create-github-release-results.json"
cd "$(workspaces.data.path)/$CONTENT_DIRECTORY"
set -o pipefail
shopt -s failglob
if gh release list --repo "$REPOSITORY" | grep -q "$RELEASE_VERSION"; then
OWNER_REPO=${REPOSITORY#https://github.com/}
RELEASE="$(gh api "repos/${OWNER_REPO}/releases" --jq ".[] | select(.tag_name == \"v${RELEASE_VERSION}\")")"
if [ -n "$RELEASE" ]; then
echo "Release v${RELEASE_VERSION} exists"
echo "https://github.com/$REPOSITORY/releases/tag/v${RELEASE_VERSION}" > "$(results.url.path)"
else
echo "$REPOSITORY/releases/tag/v${RELEASE_VERSION}" > "$(results.url.path)"
else
gh release create "v${RELEASE_VERSION}" ./*.zip ./*.json ./*SHA256SUMS ./*.sig \
--repo "$REPOSITORY" --title "Release $RELEASE_VERSION" | tee "$(results.url.path)"
fi
Expand Down
10 changes: 5 additions & 5 deletions tasks/create-github-release/tests/mocks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ set -eux
# mocks to be injected into task step scripts

function gh() {
echo "Mock gh called with: $*"
echo "Mock gh called with: $*" >&2
echo "$*" >> $(workspaces.data.path)/mock_gh.txt
if [[ "$*" == "release list --repo foo/repo_with_release" ]]; then
echo "v1.2.3"
if [[ "$*" == "api repos/foo/repo_with_release/releases"* ]]; then
echo "{"repodata": "lotsofdata"}"
exit 0
elif
[[ "$*" == "release create v1.2.3 ./foo.zip ./foo.json ./foo_SHA256SUMS ./foo_SHA256SUMS.sig --repo foo/bar --title Release 1.2.3" ]] || \
[[ "$*" == "release list --repo foo/bar" ]]; then
[[ "$*" == "release create v1.2.3 ./foo.zip ./foo.json ./foo_SHA256SUMS ./foo_SHA256SUMS.sig --repo https://github.com/foo/bar --title Release 1.2.3" ]] || \
[[ "$*" == "api repos/foo/bar/releases"* ]]; then
exit 0
else
echo Error: Unexpected call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
name: test-create-github-exist-release
spec:
description: |
Run the create-github-release task to create an existing release.
Run the create-github-release task to create an existing release.
It will not be recreated.
workspaces:
- name: tests-workspace
Expand Down Expand Up @@ -39,7 +39,7 @@ spec:
- name: githubSecret
value: test-create-github-release-token
- name: repository
value: foo/repo_with_release
value: https://github.com/foo/repo_with_release
- name: release_version
value: 1.2.3
- name: content_directory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ spec:
- name: githubSecret
value: test-create-github-release-token
- name: repository
value: foo/bar
value: https://github.com/foo/bar
- name: release_version
value: 1.2.3
- name: content_directory
Expand Down

0 comments on commit 2fc609f

Please sign in to comment.