Skip to content

Commit

Permalink
feat(RELEASE-1244): make create-github-release task idempotent
Browse files Browse the repository at this point in the history
Signed-off-by: Jing Qi <jinqi@redhat.com>
  • Loading branch information
jinqi7 committed Nov 4, 2024
1 parent 8057db4 commit 7249e6c
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 10 deletions.
3 changes: 3 additions & 0 deletions tasks/create-github-release/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 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.1.1"
app.kubernetes.io/version: "2.2.0"
annotations:
tekton.dev/pipelines.minVersion: "0.12.1"
tekton.dev/tags: release
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 9 additions & 4 deletions tasks/create-github-release/tests/mocks.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#!/usr/bin/env sh
#!/usr/bin/env bash
set -eux

# mocks to be injected into task step scripts

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
15 changes: 11 additions & 4 deletions tasks/create-github-release/tests/test-create-github-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7249e6c

Please sign in to comment.