Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(RELEASE-1244): make create-github-release task idempotent #666

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,74 @@
---
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
name: test-create-github-exist-release
spec:
description: |
Run the create-github-release task to create an existing release.
It will not be recreated.
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
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