From 7249e6c414b0a136c3ca2971405d7b01c4af1ae9 Mon Sep 17 00:00:00 2001 From: Jing Qi Date: Sat, 2 Nov 2024 11:22:03 +0800 Subject: [PATCH] feat(RELEASE-1244): make create-github-release task idempotent Signed-off-by: Jing Qi --- tasks/create-github-release/README.md | 3 + .../create-github-release.yaml | 11 ++- tasks/create-github-release/tests/mocks.sh | 13 +++- .../test-create-github-exist-release.yaml | 73 +++++++++++++++++++ .../tests/test-create-github-release.yaml | 15 +++- 5 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 tasks/create-github-release/tests/test-create-github-exist-release.yaml diff --git a/tasks/create-github-release/README.md b/tasks/create-github-release/README.md index 6ae51e856..41f278b5c 100644 --- a/tasks/create-github-release/README.md +++ b/tasks/create-github-release/README.md @@ -15,6 +15,9 @@ 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.0 +* make task idempotent + ## Changes in 2.1.1 * Fixed shellcheck linting issues diff --git a/tasks/create-github-release/create-github-release.yaml b/tasks/create-github-release/create-github-release.yaml index 44786107d..745825ed3 100644 --- a/tasks/create-github-release/create-github-release.yaml +++ b/tasks/create-github-release/create-github-release.yaml @@ -4,7 +4,7 @@ kind: Task metadata: name: create-github-release labels: - app.kubernetes.io/version: "2.1.1" + app.kubernetes.io/version: "2.2.0" annotations: tekton.dev/pipelines.minVersion: "0.12.1" tekton.dev/tags: release @@ -46,10 +46,17 @@ spec: cd "$(workspaces.data.path)/$CONTENT_DIRECTORY" set -o pipefail shopt -s failglob - gh release create "v${RELEASE_VERSION}" ./*.zip ./*.json ./*SHA256SUMS ./*.sig \ + + if gh release list --repo "$REPOSITORY" | grep -q "$RELEASE_VERSION"; then + echo "Release v${RELEASE_VERSION} exists" + echo "https://github.com/$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 jq -n --arg release "$(cat "$(results.url.path)")" '{"github-release": {"url": $release}}' | tee "$RESULTS_FILE" + env: - name: GH_TOKEN valueFrom: diff --git a/tasks/create-github-release/tests/mocks.sh b/tasks/create-github-release/tests/mocks.sh index 52a51272d..18ed84bc8 100644 --- a/tasks/create-github-release/tests/mocks.sh +++ b/tasks/create-github-release/tests/mocks.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env sh +#!/usr/bin/env bash set -eux # mocks to be injected into task step scripts @@ -6,9 +6,14 @@ set -eux function gh() { echo "Mock gh called with: $*" echo "$*" >> $(workspaces.data.path)/mock_gh.txt - - if [[ "$*" != "release create v1.2.3 ./foo.zip ./foo.json ./foo_SHA256SUMS ./foo_SHA256SUMS.sig --repo foo/bar --title Release 1.2.3" ]] - then + if [[ "$*" == "release list --repo foo/repo_with_release" ]]; then + echo "v1.2.3" + 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 + exit 0 + else echo Error: Unexpected call exit 1 fi diff --git a/tasks/create-github-release/tests/test-create-github-exist-release.yaml b/tasks/create-github-release/tests/test-create-github-exist-release.yaml new file mode 100644 index 000000000..8b4c8df8d --- /dev/null +++ b/tasks/create-github-release/tests/test-create-github-exist-release.yaml @@ -0,0 +1,73 @@ +--- +apiVersion: tekton.dev/v1 +kind: Pipeline +metadata: + name: test-create-github-release +spec: + description: | + Run the create-github-release task + workspaces: + - name: tests-workspace + tasks: + - name: setup + workspaces: + - name: data + workspace: tests-workspace + taskSpec: + workspaces: + - name: data + steps: + - name: setup-values + image: quay.io/konflux-ci/release-service-utils:e633d51cd41d73e4b3310face21bb980af7a662f + script: | + #!/usr/bin/env sh + set -eux + + mkdir $(workspaces.data.path)/results + mkdir $(workspaces.data.path)/release/ + cat > $(workspaces.data.path)/release/foo.json << EOF + { "example github release file": "just mock data" } + EOF + touch $(workspaces.data.path)/release/foo.zip + touch $(workspaces.data.path)/release/foo_SHA256SUMS + touch $(workspaces.data.path)/release/foo_SHA256SUMS.sig + - name: run-task + taskRef: + name: create-github-release + params: + - name: githubSecret + value: test-create-github-release-token + - name: repository + value: foo/repo_with_release + - name: release_version + value: 1.2.3 + - name: content_directory + value: release/ + - name: resultsDirPath + value: "results" + workspaces: + - name: data + workspace: tests-workspace + runAfter: + - setup + - name: check-result + workspaces: + - name: data + workspace: tests-workspace + taskSpec: + workspaces: + - name: data + steps: + - name: check-result + image: quay.io/konflux-ci/release-service-utils:e633d51cd41d73e4b3310face21bb980af7a662f + script: | + #!/usr/bin/env bash + set -eux + + if [ "$(wc -l < "$(workspaces.data.path)"/mock_gh.txt)" != 1 ]; then + echo Error: gh was expected to be called 1 times. Actual calls: + cat "$(workspaces.data.path)"/mock_gh.txt + exit 1 + fi + runAfter: + - run-task diff --git a/tasks/create-github-release/tests/test-create-github-release.yaml b/tasks/create-github-release/tests/test-create-github-release.yaml index e236f9a93..332ff5203 100644 --- a/tasks/create-github-release/tests/test-create-github-release.yaml +++ b/tasks/create-github-release/tests/test-create-github-release.yaml @@ -61,12 +61,19 @@ spec: - name: check-result image: quay.io/konflux-ci/release-service-utils:e633d51cd41d73e4b3310face21bb980af7a662f script: | - #!/usr/bin/env sh + #!/usr/bin/env bash set -eux - if [ $(cat $(workspaces.data.path)/mock_gh.txt | wc -l) != 1 ]; then - echo Error: gh was expected to be called 1 time. Actual calls: - cat $(workspaces.data.path)/mock_gh.txt + if ! grep "release create v1.2.3" < "$(workspaces.data.path)"/mock_gh.txt 2> /dev/null + then + echo Error: release create v1.2.3 was expected. Actual call: + cat "$(workspaces.data.path)"/mock_gh.txt + exit 1 + fi + + if [ "$(wc -l < "$(workspaces.data.path)"/mock_gh.txt)" != 2 ]; then + echo Error: gh was expected to be called 2 times. Actual calls: + cat "$(workspaces.data.path)"/mock_gh.txt exit 1 fi runAfter: