diff --git a/task/create-gitlab-release/0.2/README.md b/task/create-gitlab-release/0.2/README.md new file mode 100644 index 0000000000..b5c8630645 --- /dev/null +++ b/task/create-gitlab-release/0.2/README.md @@ -0,0 +1,60 @@ +# Create Gitlab Release + +It is typical to create a Gitlab tag at the moment of release to introduce a checkpoint in your source code history, +but in most cases users will need compiled objects or other assets output, not just the raw source code. + +Gitlab Releases are a way to track deliverables in your project. Consider them a snapshot in time of the source, +build output, artifacts, and other metadata associated with a released version of your code. + +This `task` can be used to make the `gitlab release`. + +Task can also be used to upload `assets` including `binaries` of the released version, with the release. + +## Install the Task + +``` +kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/main/task/create-gitlab-release/0.2/create-gitlab-release.yaml +``` + +## Parameters + +- **TAG_NAME**: A git tag name that will be created with this release(_e.g:_`v1.0.0`). +- **NAME**: The Name of the release (_e.g:_`First release`). +- **DESCRIPTION**: A short description of the release (_default:_`""`). +- **RELEASE_REF**: It can be a commit SHA, another tag name, or a branch name (_default:_`master`). +- **PROJECT_ID**: The Gitlab id of the project, can be found on the repository page of the gitlab (_e.g:_`18587362`) +- **UPLOAD_ASSET_NAME**: The name of the asset that needs to be uploaded (_default:_`""`). +- **UPLOAD_ASSET_URL**: The uplaod URL for the hosted asset (_default:_`""`). +- **GITLAB_TOKEN_SECRET**: The name of the `secret` holding the gitlab-token (_default:_`gitlab-token`). +- **GITLAB_TOKEN_SECRET_KEY**: The name of the `secret key` holding the gitlab-token (_default:_`GITLAB_TOKEN`). + + +## Secrets +* `Secret` to provide personal `access token` of the Gitlab. + +Check [this](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html) to get personal access token for `Gitlab`. + +## Platforms + +The Task can be run on `linux/amd64` platform. + +## Usage + +This task expects a secret named gitlab-token to exists, with a Gitlab personal access token in `GITLAB_TOKEN` with enough privileges to create a release. + +At present, `Gitlab` doesn't provide the functionality to upload any file to the release directly, however file that is hosted on any platform (i.e `aws s3` or `gitlab`) can be uploaded with the release by providing the hosted file `URL path` as the param to the task. + +To make a release put all the required params in the Taskrun, add required secrets and release will be done. + +`Secrets` can be created as follows: +``` +apiVersion: v1 +kind: Secret +metadata: + name: gitlab-token +type: Opaque +stringData: + GITLAB_TOKEN: $(personal-gitlab-token) +``` + +This [samples](../0.1/samples/run.yaml) can be referred to create Taskrun for Gitlab release. diff --git a/task/create-gitlab-release/0.2/create-gitlab-release.yaml b/task/create-gitlab-release/0.2/create-gitlab-release.yaml new file mode 100644 index 0000000000..4bbeec9f9a --- /dev/null +++ b/task/create-gitlab-release/0.2/create-gitlab-release.yaml @@ -0,0 +1,92 @@ +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: create-gitlab-release + labels: + app.kubernetes.io/version: "0.2" + annotations: + tekton.dev/pipelines.minVersion: "0.50.0" + tekton.dev/categories: Git + tekton.dev/tags: gitlab + tekton.dev/displayName: "create gitlab release" + tekton.dev/platforms: "linux/amd64" +spec: + description: >- + This `task` can be used to make a gitlab release. + + params: + - name: TAG_NAME + description: Name of the tag. + type: string + - name: NAME + description: Name of the release. + type: string + - name: DESCRIPTION + type: string + description: Short description of the release. + default: "" + - name: RELEASE_REF + type: string + description: Required in the case of Gitlab, It can be a commit SHA, another tag name, or a branch name. + default: master + - name: PROJECT_ID + type: string + description: Project id for gitlab project. + - name: UPLOAD_ASSET_NAME + type: string + description: Name of the asset to be uploaded. + default: "" + - name: UPLOAD_ASSET_URL + type: string + description: Uplaod URL for the hosted asset. + default: "" + - name: GITLAB_TOKEN_SECRET + type: string + description: Name of the secret holding the gitlab-token. + default: gitlab-token + - name: GITLAB_TOKEN_SECRET_KEY + type: string + description: Name of the secret key holding the gitlab-token. + default: GITLAB_TOKEN + steps: + - name: create-release + image: registry.access.redhat.com/ubi8/ubi-minimal:8.2@sha256:5cfbaf45ca96806917830c183e9f37df2e913b187aadb32e89fd83fa455ebaa6 + script: | + #!/usr/bin/env bash + + # Creating asset json if file is to be uploaded with gitlab release + # + assetJson="" + # Checking whether asset is provided to upload with release or not. + # + if [ "$(params.UPLOAD_ASSET_NAME)" != "" ] || [ "$(params.UPLOAD_ASSET_URL)" != "" ]; then + assetJson=',"assets": {"links": [{"name": "$(params.UPLOAD_ASSET_NAME)","url": "$(params.UPLOAD_ASSET_URL)"}]}' + fi + + # Creating the api post data + # + generate_api_post_data() + { + cat <"] + helper = store + .git-credentials: | + https://:@ +``` diff --git a/task/git-cli/0.5/git-cli.yaml b/task/git-cli/0.5/git-cli.yaml new file mode 100644 index 0000000000..ab1d7c00e7 --- /dev/null +++ b/task/git-cli/0.5/git-cli.yaml @@ -0,0 +1,147 @@ +--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: git-cli + labels: + app.kubernetes.io/version: "0.5" + annotations: + tekton.dev/pipelines.minVersion: "0.50.0" + tekton.dev/categories: Git + tekton.dev/tags: git + tekton.dev/displayName: "git cli" + tekton.dev/platforms: "linux/amd64,linux/s390x,linux/ppc64le" +spec: + description: >- + This task can be used to perform git operations. + + Git command that needs to be run can be passed as a script to + the task. This task needs authentication to git in order to push + after the git operation. + + workspaces: + - name: source + description: A workspace that contains the fetched git repository. + + - name: input + optional: true + description: | + An optional workspace that contains the files that need to be added to git. You can + access the workspace from your script using `$(workspaces.input.path)`, for instance: + + cp $(workspaces.input.path)/file_that_i_want . + git add file_that_i_want + # etc + + - name: ssh-directory + optional: true + description: | + A .ssh directory with private key, known_hosts, config, etc. Copied to + the user's home before git commands are executed. Used to authenticate + with the git remote when performing the clone. Binding a Secret to this + Workspace is strongly recommended over other volume types. + + - name: basic-auth + optional: true + description: | + A Workspace containing a .gitconfig and .git-credentials file. These + will be copied to the user's home before any git commands are run. Any + other files in this Workspace are ignored. It is strongly recommended + to use ssh-directory over basic-auth whenever possible and to bind a + Secret to this Workspace over other volume types. + params: + - name: BASE_IMAGE + description: | + The base image for the task. + type: string + # TODO: Deprecate use of root image. + default: cgr.dev/chainguard/git:root-2.39@sha256:7759f87050dd8bacabe61354d75ccd7f864d6b6f8ec42697db7159eccd491139 + + - name: GIT_USER_NAME + type: string + description: | + Git user name for performing git operation. + default: "" + + - name: GIT_USER_EMAIL + type: string + description: | + Git user email for performing git operation. + default: "" + + - name: GIT_SCRIPT + description: The git script to run. + type: string + default: | + git help + + - name: USER_HOME + description: | + Absolute path to the user's home directory. Set this explicitly if you are running the image as a non-root user or have overridden + the gitInitImage param with an image containing custom user configuration. + type: string + default: "/root" + + - name: VERBOSE + description: Log the commands that are executed during `git-clone`'s operation. + type: string + default: "true" + + results: + - name: commit + description: The precise commit SHA after the git operation. + + steps: + - name: git + image: $(params.BASE_IMAGE) + workingDir: $(workspaces.source.path) + env: + - name: HOME + value: $(params.USER_HOME) + - name: PARAM_VERBOSE + value: $(params.VERBOSE) + - name: PARAM_USER_HOME + value: $(params.USER_HOME) + - name: WORKSPACE_SSH_DIRECTORY_BOUND + value: $(workspaces.ssh-directory.bound) + - name: WORKSPACE_SSH_DIRECTORY_PATH + value: $(workspaces.ssh-directory.path) + - name: WORKSPACE_BASIC_AUTH_DIRECTORY_BOUND + value: $(workspaces.basic-auth.bound) + - name: WORKSPACE_BASIC_AUTH_DIRECTORY_PATH + value: $(workspaces.basic-auth.path) + script: | + #!/usr/bin/env sh + set -eu + + if [ "${PARAM_VERBOSE}" = "true" ] ; then + set -x + fi + + if [ "${WORKSPACE_BASIC_AUTH_DIRECTORY_BOUND}" = "true" ] ; then + cp "${WORKSPACE_BASIC_AUTH_DIRECTORY_PATH}/.git-credentials" "${PARAM_USER_HOME}/.git-credentials" + cp "${WORKSPACE_BASIC_AUTH_DIRECTORY_PATH}/.gitconfig" "${PARAM_USER_HOME}/.gitconfig" + chmod 400 "${PARAM_USER_HOME}/.git-credentials" + chmod 400 "${PARAM_USER_HOME}/.gitconfig" + fi + + if [ "${WORKSPACE_SSH_DIRECTORY_BOUND}" = "true" ] ; then + cp -R "${WORKSPACE_SSH_DIRECTORY_PATH}" "${PARAM_USER_HOME}"/.ssh + chmod 700 "${PARAM_USER_HOME}"/.ssh + chmod -R 400 "${PARAM_USER_HOME}"/.ssh/* + fi + + # Setting up the config for the git. + git config --global user.email "$(params.GIT_USER_EMAIL)" + git config --global user.name "$(params.GIT_USER_NAME)" + + eval '$(params.GIT_SCRIPT)' + + RESULT_SHA="$(git rev-parse HEAD | tr -d '\n')" + EXIT_CODE="$?" + if [ "$EXIT_CODE" != 0 ] + then + exit $EXIT_CODE + fi + # Make sure we don't add a trailing newline to the result! + printf "%s" "$RESULT_SHA" > "$(results.commit.path)" diff --git a/task/git-cli/0.5/tests/run.yaml b/task/git-cli/0.5/tests/run.yaml new file mode 100644 index 0000000000..5a610fed93 --- /dev/null +++ b/task/git-cli/0.5/tests/run.yaml @@ -0,0 +1,90 @@ +--- +apiVersion: tekton.dev/v1 +kind: Pipeline +metadata: + name: pull-revision +spec: + description: | + pull-revision takes a git script and a commit SHA + and validates that the task returns a correct commit SHA. This is an example + Pipeline demonstrating the following: + - Using the git-cli catalog Task that execute a script to pull a repository + - Passing a pulled repo to subsequent Tasks using a Workspace. + - Ordering Tasks in a Pipeline using "runAfter" so that + git-cli completes before we try to read from the Workspace. + - Using a volumeClaimTemplate Volume as a Workspace. + - Avoiding hard-coded paths by using a Workspace's path + variable instead. + params: + - name: GIT_SCRIPT + type: string + description: The script that git-cli needs to execute. + - name: commit + type: string + description: The git commit to check. + workspaces: + - name: shared-data + description: | + This workspace will receive the pulled git repo and be passed + to the next Task for the commit to be checked. + tasks: + - name: fetch-repo + taskRef: + name: git-cli + workspaces: + - name: source + workspace: shared-data + params: + - name: GIT_SCRIPT + value: $(params.GIT_SCRIPT) + - name: compare-received-commit-to-expected + runAfter: ["fetch-repo"] # Wait until the pull is done before checking the expected commit SHA. + params: + - name: expected-commit + value: $(params.commit) + workspaces: + - name: source + workspace: shared-data + taskSpec: + params: + - name: expected-commit + workspaces: + - name: source + steps: + - image: alpine/git:v2.36.2 + script: | + #!/usr/bin/env sh + cd $(workspaces.source.path) + receivedCommit=$(git rev-parse HEAD) + if [ $receivedCommit != $(params.expected-commit) ]; then + echo "Expected commit $(params.expected-commit) but received $receivedCommit." + exit 1 + else + echo "Received commit $receivedCommit as expected." + fi +--- +apiVersion: tekton.dev/v1 +kind: PipelineRun +metadata: + generateName: git-cli-checking-out-a-commit- +spec: + pipelineRef: + name: pull-revision + workspaces: + - name: shared-data + volumeClaimTemplate: + spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 500Mi + volumeMode: Filesystem + params: + - name: GIT_SCRIPT + value: | + git init + git remote add origin https://github.com/kelseyhightower/nocode + git pull origin master + - name: commit + value: 6c073b08f7987018cbb2cb9a5747c84913b3608e # Latest commit of repository diff --git a/task/git-rebase/0.2/README.md b/task/git-rebase/0.2/README.md new file mode 100644 index 0000000000..b62942e52a --- /dev/null +++ b/task/git-rebase/0.2/README.md @@ -0,0 +1,76 @@ +# Git Task + +This `Task` is Git task to work with repositories used by other tasks +in your Pipeline. + +## `git-rebase` + +This [task](../0.1/git-rebase.yaml) will `rebase` the branch based +on the user input. Before rebase, if `squashing` of the commits is required, +then it can be done by providing the `squash count` i.e number of commits to squash. + +### Workspaces + +* **source**: A workspace that contains the fetched git repository. + +### Parameters + +* **SQUASH_COUNT**: Number of commits to squash in the branch. +(_only required if squashing needs to be performed_). +* **COMMIT_MSG**: Commit message to add in commit after the + squashing is done (_only required if squashing needs to be performed_). +* **GIT_USER_NAME**: Git user name to use for rebase (_required_). +* **GIT_USER_EMAIL**: Git user email to use for rebase (_required_). +* **PULL_REMOTE_NAME**: Git remote name from which we have to pull +and rebase.(_default_: `origin`). +* **PULL_REMOTE_URL**: Git remote URL from which we have to +pull and rebase (_required to set remote_). +* **PULL_BRANCH_NAME**: Git branch name from which we have to +pull and rebase (_required_). +* **PUSH_REMOTE_NAME**: Git remote name to push after rebase(_default_: `origin`). +* **PUSH_REMOTE_URL**: Git remote URL to push after rebase(_required_). +* **PUSH_BRANCH_NAME**: Git branch name to push after rebase (_required_). + +### Results + +* **commit**: The precise commit SHA after the rebase. + +### Platforms + +The Task can be run on `linux/amd64`, `linux/s390x`, `linux/arm64`, and `linux/ppc64le` platforms. + +## Platforms + +The Task can be run on `linux/amd64` platform. + +### Usage + +This task needs authentication to git in order to push after the rebase. +This can be done via `secrets` and `service account`. Refer [this](../0.1/samples/git-rebase) +example to create secret and service account for the git authentication. + +[Git Rebase](../0.1/samples/run.yaml) pipeline has two tasks, one to clone a +git repository in the provided workspace and other to perform rebase operation. +Here, [git-clone](../../git-clone/0.1/git-clone.yaml) task is used to clone the repository. + +A workspace called "shared-workspace" is passed first to the `git-clone` +Task for the code to be checked out on and then to the `git-rebase` Task +to perform the rebase operation. + +Remote name, Remote URL and Branch name needs to provided in the input +to set the remote in order to perfrom push and pull operations. + +[Git Rebase](../0.1/samples/run.yaml) pipeline can be referred to +create a `pipeline` and `pipelinerun`. + +**NOTE** + +* If `two factor authentication` is enabled in your git account, then +`access-token` for the respective git platform must be provided +in the `password` field while adding secrets. + +* In the params for `git-clone` task, make sure that param value +for `depth` must be greater than param value for `SQUASH_COUNT` in `git-rebase` task. + +* If there are chances for `merge conflicts`, then that needs to resolved before +doing rebase through this task as task will fail, if any merge conflict is found. \ No newline at end of file diff --git a/task/git-rebase/0.2/git-rebase.yaml b/task/git-rebase/0.2/git-rebase.yaml new file mode 100644 index 0000000000..d7608babc6 --- /dev/null +++ b/task/git-rebase/0.2/git-rebase.yaml @@ -0,0 +1,126 @@ +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: git-rebase + labels: + app.kubernetes.io/version: "0.2" + annotations: + tekton.dev/pipelines.minVersion: "0.50.0" + tekton.dev/categories: Git + tekton.dev/tags: git + tekton.dev/displayName: "git rebase" + tekton.dev/platforms: "linux/amd64,linux/s390x,linux/ppc64le,linux/arm64" +spec: + description: >- + These Tasks are Git tasks to work with repositories used by other tasks + in your Pipeline. + + This task will rebase the branch based on the user input. Before rebase, + if squashing of the commits is required, then it can be done by providing + the squash count i.e number of commits to squash. + + workspaces: + - name: source + description: A workspace that contains the fetched git repository. + params: + - name: SQUASH_COUNT + type: string + description: | + Number of commits to squash in the branch. + default: "0" + + - name: COMMIT_MSG + type: string + description: | + Commit message after the squash is done. + default: "" + + - name: GIT_USER_NAME + type: string + description: | + Git user name to use for rebase. + + - name: GIT_USER_EMAIL + type: string + description: | + Git user email to use for rebase. + + - name: PULL_REMOTE_NAME + type: string + description: | + Git remote name from which we have to pull and rebase. + default: origin + + - name: PULL_REMOTE_URL + type: string + description: | + Git remote URL from which we have to pull and rebase. + + - name: PULL_BRANCH_NAME + type: string + description: | + Git branch name from which we have to pull and rebase. + + - name: PUSH_REMOTE_NAME + type: string + description: | + Git remote name to push after rebase. + default: origin + + - name: PUSH_REMOTE_URL + type: string + description: | + Git remote URL to push after rebase. + + - name: PUSH_BRANCH_NAME + type: string + description: | + Git branch to push after rebase. + results: + - name: commit + description: The precise commit SHA after the rebase. + steps: + - name: rebase + workingDir: $(workspaces.source.path) + image: cgr.dev/chainguard/git:2.39@sha256:fdaef225e3fd5cf190520553ff765f186a4363390af3f19912897b0b28f87aeb + script: | + + # Setting up the config for the git. + git config user.email "$(params.GIT_USER_EMAIL)" + git config user.name "$(params.GIT_USER_NAME)" + + # Squashing the commits if required. + if [ "$(params.SQUASH_COUNT)" != 0 ] ; then + + git reset --soft HEAD~$(params.SQUASH_COUNT) + git add . + git commit -m "$(params.COMMIT_MSG)" + + fi + + # Checking if remote is already set up or not. + git ls-remote --exit-code $(params.PULL_REMOTE_NAME) + + # Setting up remote url for pull. + if test $? != 0; then + git remote add $(params.PULL_REMOTE_NAME) $(params.PULL_REMOTE_URL) + fi + + # Pull and rebase + git pull --rebase $(params.PULL_REMOTE_NAME) $(params.PULL_BRANCH_NAME) + + # Checking if remote is already set up or not. + git ls-remote --exit-code $(params.PUSH_REMOTE_NAME) + + # Setting up remote url for push. + if test $? != 0; then + git remote add $(params.PUSH_REMOTE_NAME) $(params.PUSH_REMOTE_URL) + fi + + # Force push after the rebase. + git push $(params.PUSH_REMOTE_NAME) $(params.PUSH_BRANCH_NAME) -f + + RESULT_SHA="$(git rev-parse HEAD | tr -d '\n')" + + # Make sure we don't add a trailing newline to the result! + echo -n "$RESULT_SHA" > $(results.commit.path) diff --git a/task/git-rebase/0.2/samples/run.yaml b/task/git-rebase/0.2/samples/run.yaml new file mode 100644 index 0000000000..410b21ea57 --- /dev/null +++ b/task/git-rebase/0.2/samples/run.yaml @@ -0,0 +1,75 @@ +apiVersion: tekton.dev/v1 +kind: Pipeline +metadata: + name: git-rebase-pipeline +spec: + workspaces: + - name: shared-workspace + tasks: + - name: fetch-repository + taskRef: + name: git-clone + workspaces: + - name: output + workspace: shared-workspace + params: + - name: url + value: https://github.com/divyansh42/squash-test.git + - name: subdirectory + value: "" + - name: deleteExisting + value: "true" + - name: depth + value: "10" + - name: refspec + value: "refs/heads/master:refs/heads/master" + - name: git-rebase + taskRef: + name: git-rebase + runAfter: + - fetch-repository + workspaces: + - name: source + workspace: shared-workspace + params: + - name: SQUASH_COUNT + value: "2" + - name: COMMIT_MSG + value: "squashed commit" + - name: GIT_USER_NAME + value: "divyansh42" + - name: GIT_USER_EMAIL + value: diagrwa@redhat.com + - name: PULL_REMOTE_NAME + value: origin + - name: PULL_REMOTE_URL + value: https://github.com/divyansh42/squash-test.git + - name: PULL_BRANCH_NAME + value: feature + - name: PUSH_REMOTE_NAME + value: origin + - name: PUSH_REMOTE_URL + value: https://github.com/divyansh42/squash-test.git + - name: PUSH_BRANCH_NAME + value: master + results: + - name: commit +--- +apiVersion: tekton.dev/v1 +kind: PipelineRun +metadata: + name: git-rebase-pipeline-run +spec: + taskRunTemplate: + serviceAccountName: git-rebase-service-account + pipelineRef: + name: git-rebase-pipeline + workspaces: + - name: shared-workspace + volumeClaimTemplate: + spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 500Mi diff --git a/task/git-rebase/0.2/samples/secret.yaml b/task/git-rebase/0.2/samples/secret.yaml new file mode 100644 index 0000000000..62b68ea726 --- /dev/null +++ b/task/git-rebase/0.2/samples/secret.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Secret +metadata: + name: github-auth + annotations: + tekton.dev/git-0: https://github.com # Replace with the desired domain name. +type: kubernetes.io/basic-auth +stringData: + username: $(username) + password: $(password) # Access token should be provided here, if 2 factor authentication is enabled. diff --git a/task/git-rebase/0.2/samples/service-account.yaml b/task/git-rebase/0.2/samples/service-account.yaml new file mode 100644 index 0000000000..bd3e27cb3d --- /dev/null +++ b/task/git-rebase/0.2/samples/service-account.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: git-rebase-service-account +secrets: + - name: github-auth \ No newline at end of file diff --git a/task/git-version/0.2/README.md b/task/git-version/0.2/README.md new file mode 100644 index 0000000000..0d188322f9 --- /dev/null +++ b/task/git-version/0.2/README.md @@ -0,0 +1,31 @@ +## Generate a version from git history + +The `git-version` task let you generate a version from git history using `https://gitversion.net/` + +### Install the Task + +``` +kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/main/task/git-version/0.2/git-version.yaml +``` + +## Workspaces + +* **source**: A workspace with a checked out git repository. + +### Parameters + +* **branch:**: The branch to checkout to create a version for e.g. "develop" + +## Results + +* **gitVersion**: The calculated git version you could use for git tagging e.g. "0.1.0-tektonize.1-188" +* **packageVersion**: A normalized version for use in container images e.g. "0.1.0-tektonize.1-188" + +## Usage + +You will need to make sure the used workspace has a git clone with all the history available. +Git clone with a depth of "0" is therefore needed. + +## Platforms + +The Task can be run on `linux/amd64` platform. diff --git a/task/git-version/0.2/git-version.yaml b/task/git-version/0.2/git-version.yaml new file mode 100644 index 0000000000..dd5f1094c7 --- /dev/null +++ b/task/git-version/0.2/git-version.yaml @@ -0,0 +1,53 @@ +--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: git-version + labels: + app.kubernetes.io/version: "0.2" + annotations: + tekton.dev/pipelines.minVersion: "0.50.0" + tekton.dev/displayName: "git version" + tekton.dev/categories: Git + tekton.dev/tags: git + tekton.dev/platforms: "linux/amd64" +spec: + description: >- + This task can be used to create a version from git history + params: + - description: branch to checkout to create a version for e.g. "develop" + name: branch + type: string + results: + - description: The calculated git version you could use for git tagging e.g. "0.1.0-tektonize.1-188" + name: gitVersion + - description: A normalized version for use in container images e.g. "0.1.0-tektonize.1-188" + name: packageVersion + steps: + - image: mcr.microsoft.com/dotnet/sdk:3.1-focal@sha256:1e34d3881f9e1da0b2e81de59656abc29be6993eef2362d9134d66530db8b6ba + name: set-git-version + workingDir: $(workspaces.source.path) + securityContext: + runAsUser: 0 + env: + - name: PARAM_BRANCH + value: $(params.branch) + script: | + #!/usr/bin/env bash + export PATH="$PATH:/tekton/home/.dotnet/tools" + dotnet tool install GitVersion.Tool --version 5.5.0 --tool-path "/tekton/home/.dotnet/tools" + + git checkout "${PARAM_BRANCH}" + + GITVERSION=$(dotnet gitversion /showvariable FullSemVer) + export GITVERSION + echo -n "${GITVERSION}" | tee "$(results.gitVersion.path)" + + # normalize a bit because + # image tags can only contain `abcdefghijklmnopqrstuvwxyz0123456789_-.ABCDEFGHIJKLMNOPQRSTUVWXYZ` + PACKAGEVERSION=$(echo -n "$GITVERSION" | sed 's/[^-._0-9A-Za-z]/-/g') + export PACKAGEVERSION + echo -n "${PACKAGEVERSION}" | tee "$(results.packageVersion.path)" + workspaces: + - name: source + description: A workspace that contains the fetched git repository to create a version for. diff --git a/task/git-version/0.2/tests/pre-apply-task-hook.sh b/task/git-version/0.2/tests/pre-apply-task-hook.sh new file mode 100755 index 0000000000..2208b28188 --- /dev/null +++ b/task/git-version/0.2/tests/pre-apply-task-hook.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +# Add git-cli +add_task git-cli latest \ No newline at end of file diff --git a/task/git-version/0.2/tests/resources.yaml b/task/git-version/0.2/tests/resources.yaml new file mode 100644 index 0000000000..72914cc672 --- /dev/null +++ b/task/git-version/0.2/tests/resources.yaml @@ -0,0 +1,11 @@ +--- +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: git-version-pvc +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 100Mi diff --git a/task/git-version/0.2/tests/run.yaml b/task/git-version/0.2/tests/run.yaml new file mode 100644 index 0000000000..b697456385 --- /dev/null +++ b/task/git-version/0.2/tests/run.yaml @@ -0,0 +1,68 @@ +--- +apiVersion: tekton.dev/v1 +kind: Pipeline +metadata: + name: git-version-test +spec: + workspaces: + - name: shared-workspace + tasks: + - name: git-fetch-all + taskRef: + name: git-cli + params: + # we need all branches available for git-version + - name: GIT_SCRIPT + value: | + git clone https://github.com/GijsvanDulmen/git-version-test.git . + git fetch --all + workspaces: + - name: source + workspace: shared-workspace + - name: input + workspace: shared-workspace + - name: git-version + params: + - name: branch + value: develop + runAfter: + - git-fetch-all + taskRef: + name: git-version + workspaces: + - name: source + workspace: shared-workspace + - name: check-results-of-git-version + runAfter: + - git-version + params: + - name: gitVersion + value: $(tasks.git-version.results.gitVersion) + - name: packageVersion + value: $(tasks.git-version.results.packageVersion) + taskSpec: + params: + - name: gitVersion + - name: packageVersion + steps: + - name: test-git-version + image: ubuntu + script: | + #!/usr/bin/env bash + echo $(params.gitVersion) + echo $(params.packageVersion) + + [[ "$(params.gitVersion)" == "0.1.0-alpha.0" ]] && exit 0 || exit 1 + [[ "$(params.packageVersion)" == "0.1.0-alpha.0" ]] && exit 0 || exit 1 +--- +apiVersion: tekton.dev/v1 +kind: PipelineRun +metadata: + name: git-version-test-run +spec: + pipelineRef: + name: git-version-test + workspaces: + - name: shared-workspace + persistentVolumeClaim: + claimName: git-version-pvc \ No newline at end of file diff --git a/task/gitea-set-status/0.2/README.md b/task/gitea-set-status/0.2/README.md new file mode 100644 index 0000000000..6370ea68f8 --- /dev/null +++ b/task/gitea-set-status/0.2/README.md @@ -0,0 +1,91 @@ +# Gitea Set Status + +Gitea Set Status is for updating the status of a commit. See the Gitea API documentation +for more information about it's usage at [Gitea API](https://try.gitea.io/api/swagger). + +## Gitea token + +This task expects a secret set in the kubernetes secret `gitea` +with a Gitea access token in the key `token`; you can easily create it on the +command line with `kubectl` like this : + +``` +kubectl create secret generic gitea --from-literal token="MY_ACCESS_TOKEN" +``` + +## Set Status on a Commit/PR + +The `gitea-set-status` task uses the [commit status api](https://try.gitea.io/api/swagger) +to mark Gitea commits with an `error`, `failure`, `pending`, `warning` or `success` +state, which is then reflected in pull requests and commit history involving those commits. + +Statuses include as well a `description` and a `target_url`, to give the user +informations about the CI statuses or a direct link to the full log. + +### Install the Task + +``` +kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/main/task/gitea-set-status/0.2/gitea-set-status.yaml +``` + +### Parameters + +* **REPO_FULL_NAME**: The Gitea repository full name, _e.g:_ `tektoncd/catalog` +* **GITEA_HOST_URL**: The Gitea host, _e.g:_ `git.yourcompany.com` +* **GITEA_HTTPS_OR_HTTP**: If we should connect with HTTP or HTTPS. Use `http` or `https` here. _default:_ `https` +* **API_PATH_PREFIX**: The API path prefix of Gitea, _default:_ `/api/v1` +* **SHA**: The commit SHA to set the status for _e.g_: `tektoncd/catalog` +* **TARGET_URL**: The target URL to associate with this status. This URL will + be linked from the Gitea UI to allow users to easily see the source of the + status. For example you can link to a + [dashboard](https://github.com/tektoncd/dashboard) URL so users can follow a + Pipeline/Task run. +* **DESCRIPTION**: A short description of the status. _e.g:_ `Building your PR` +* **CONTEXT**: The Gitea context, A string label to differentiate this status + from the status of other systems. _e.g:_ `continuous-integration/tekton` +* **STATE**: The state of the status. Can be one of the following `error`, + `failure`, `pending`, `warning` or `success`. +* **GITEA_TOKEN_SECRET_NAME** \[optional\]: The name of the kubernetes secret that + contains the Gitea token. Default value: `gitea`. +* **GITEA_TOKEN_SECRET_KEY** \[optional\]: The key within the kubernetes secret that + contains the Gitea token. Default value: `token`. +* **IMAGE** \[optional\]: Image providing the python binary which this task uses. Default + value: `3.10.1-alpine3.15`, the smallest python image. +* **SHEBANG** \[optional\]: The shebang relevant for the image. Default value: `/usrb/bin/env python`. + + +### Platforms + +The Task can be run on any platform supporting a python image. The default image runs on `linux/386` +`linux/amd64`, `linux/arm/v6`, `linux/arm/v7`, `linux/arm64/v8`, `linux/ppc64le`, `linux/s390x`. + +## Example usage + +This TaskRun sets a commit on Gitea to `pending` getting tested by the CI. + +```yaml +--- +apiVersion: tekton.dev/v1 +kind: TaskRun +metadata: + labels: + tekton.dev/task: gitea-set-status + name: gitea-set-status-run-on-commit-bd93 +spec: + taskRef: + kind: Task + name: gitea-set-status + params: + - name: REPO_FULL_NAME + value: tektoncd/catalog + - name: GITEA_HOST_URL + value: gitea.com + - name: SHA + value: bd93869b489258cef567ccf85e7ef6bc0d6949ea + - name: DESCRIPTION + value: "Build has started" + - name: STATE + value: pending + - name: TARGET_URL + value: https://tekton/dashboard/taskrun/log +``` diff --git a/task/gitea-set-status/0.2/gitea-set-status.yaml b/task/gitea-set-status/0.2/gitea-set-status.yaml new file mode 100644 index 0000000000..15086e8d57 --- /dev/null +++ b/task/gitea-set-status/0.2/gitea-set-status.yaml @@ -0,0 +1,162 @@ +--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: gitea-set-status + labels: + app.kubernetes.io/version: "0.2" + annotations: + tekton.dev/categories: Git + tekton.dev/pipelines.minVersion: "0.50.0" + tekton.dev/tags: gitea + tekton.dev/displayName: "set gitea status" + tekton.dev/platforms: "linux/amd64,linux/s390x,linux/ppc64le" +spec: + description: >- + This task will set the status of the CI job to the specified value along + with a link to the specified target URL where developers can follow the + progress of the CI job. + + The `gitea-set-status` task allows external services to mark Gitea commits + with an `error`, `failure`, `pending`, or `success` state, which is then + reflected in pull requests involving those commits. Statuses include as well a + `description` and a `target_url`, to give the user informations about the CI + statuses or a direct link to the full log. + + params: + - name: GITEA_HOST_URL + description: | + The Gitea host, e.g: git.yourcompany.com. Can include port. + type: string + + - name: GITEA_HTTPS_OR_HTTP + description: | + If we should connect with HTTP or HTTPS. Use "http" or "https" here. + type: string + default: https + + - name: API_PATH_PREFIX + description: | + The API path prefix of Gitea, default: /api/v1 + default: "/api/v1" + type: string + + - name: REPO_FULL_NAME + description: | + The Gitea repository full name, e.g.: tektoncd/catalog + type: string + + - name: GITEA_TOKEN_SECRET_NAME + description: | + The name of the kubernetes secret that contains the Gitea token, default: gitea + type: string + default: gitea + + - name: GITEA_TOKEN_SECRET_KEY + description: | + The key within the kubernetes secret that contains the Gitea token, default: token + type: string + default: token + + - name: SHA + description: | + Commit SHA to set the status for. + type: string + + - name: TARGET_URL + description: | + The target URL to associate with this status. This URL will be linked + from the Gitea UI to allow users to easily see the source of the + status. + type: string + + - name: DESCRIPTION + description: | + A short description of the status. + type: string + + - name: CONTEXT + description: | + The Gitea context, A string label to differentiate this status from + the status of other systems. ie: "continuous-integration/tekton" + default: "continuous-integration/tekton" + type: string + + - name: STATE + description: | + The state of the status. Can be one of the following `error`, + `failure`, `pending`, `warning` or `success`. + type: string + + - name: IMAGE + description: | + Image providing the python binary which this task uses. + type: string + default: python:3.10.1-alpine3.15@sha256:affe0faa14e7553fc570beec3864e74b5e36f8c19b2bb49ae8ba79c0e9e7236e + + - name: SHEBANG + description: | + Python path. Depends on the image. + type: string + default: /usr/bin/env python + + volumes: + - name: giteatoken + secret: + secretName: $(params.GITEA_TOKEN_SECRET_NAME) + + steps: + - name: set-status + volumeMounts: + - name: giteatoken + mountPath: /etc/gitea-set-status + + image: $(params.IMAGE) + script: | + #!$(params.SHEBANG) + + """This script will set the CI status on a Gitea commit""" + + import json + import sys + import http.client + + gitea_token = open("/etc/gitea-set-status/$(params.GITEA_TOKEN_SECRET_KEY)", "r").read() + + status_url = "$(params.API_PATH_PREFIX)" + "/repos/$(params.REPO_FULL_NAME)/" + \ + "statuses/$(params.SHA)" + + data = { + "state": "$(params.STATE)", + "target_url": "$(params.TARGET_URL)", + "description": "$(params.DESCRIPTION)", + "context": "$(params.CONTEXT)" + } + print("Sending this data to Gitea: ") + print(data) + + authHeader = "token " + gitea_token + + if "$(params.GITEA_HTTPS_OR_HTTP)" == "https": + conn = http.client.HTTPSConnection("$(params.GITEA_HOST_URL)") + else: + conn = http.client.HTTPConnection("$(params.GITEA_HOST_URL)") + + conn.request( + "POST", + status_url, + body=json.dumps(data), + headers={ + "User-Agent": "TektonCD, the peaceful cat", + "Authorization": authHeader, + "Accept": "application/json", + "Content-Type": "application/json", + }) + resp = conn.getresponse() + if not str(resp.status).startswith("2"): + print("Error: %d" % (resp.status)) + print(resp.read()) + sys.exit(1) + else: + print("Gitea status '$(params.STATE)' has been set on " + "$(params.REPO_FULL_NAME)#$(params.SHA) ") diff --git a/task/gitea-set-status/0.2/tests/fixtures/gitea-post.yaml b/task/gitea-set-status/0.2/tests/fixtures/gitea-post.yaml new file mode 100644 index 0000000000..e15d70f88c --- /dev/null +++ b/task/gitea-set-status/0.2/tests/fixtures/gitea-post.yaml @@ -0,0 +1,8 @@ +--- +headers: + method: POST + path: /api/v1/repos/tektoncd/catalog/statuses/d87daf015916c89cc65817cc9d2ccd90d3406746 +response: + status: 201 + output: '{"status": 201}' + content-type: application/json diff --git a/task/gitea-set-status/0.2/tests/pre-apply-task-hook.sh b/task/gitea-set-status/0.2/tests/pre-apply-task-hook.sh new file mode 100644 index 0000000000..568674e13e --- /dev/null +++ b/task/gitea-set-status/0.2/tests/pre-apply-task-hook.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +kubectl -n ${tns} create secret generic gitea --from-literal token="secret" diff --git a/task/gitea-set-status/0.2/tests/run.yaml b/task/gitea-set-status/0.2/tests/run.yaml new file mode 100644 index 0000000000..30cf615dc8 --- /dev/null +++ b/task/gitea-set-status/0.2/tests/run.yaml @@ -0,0 +1,25 @@ +--- +apiVersion: tekton.dev/v1 +kind: Pipeline +metadata: + name: gitea-set-status +spec: + tasks: + - name: set-status + taskRef: + name: gitea-set-status + params: + - name: GITEA_HOST_URL + value: "127.0.0.1:8080" + - name: GITEA_HTTPS_OR_HTTP + value: http + - name: REPO_FULL_NAME + value: tektoncd/catalog + - name: SHA + value: d87daf015916c89cc65817cc9d2ccd90d3406746 + - name: TARGET_URL + value: "https://tekton.dev/" + - name: DESCRIPTION + value: "Gitea set status" + - name: STATE + value: success \ No newline at end of file diff --git a/task/gitlab-add-label/0.2/README.md b/task/gitlab-add-label/0.2/README.md new file mode 100644 index 0000000000..516f768b17 --- /dev/null +++ b/task/gitlab-add-label/0.2/README.md @@ -0,0 +1,83 @@ +# GitLab + +A collection of tasks to help working with the [GitLab API](https://docs.gitlab.com/ee/api/). + +## GitLab token + +Most tasks would expect to have a secret set in the kubernetes secret `gitlab` +with a GitLab token in the key `token`. +Check [this](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html) +to get personal access token for `Gitlab`. +You can easily create it on the command line with `kubectl` like this : + +``` +kubectl create secret generic github --from-literal token="MY_TOKEN" +``` + +or + +``` +apiVersion: v1 +kind: Secret +metadata: + name: gitlab +type: Opaque +stringData: + token: $(personal_access_token) +``` + +## Add labels to the GitLab issue or Merge Request + +[This task](../../gitlab-add-label/0.2/gitlab-add-label.yaml) can be used to add labels to the gitlab `issue` or `merge request` (pull request). + + +### Install the Task + +``` +kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/main/task/gitlab-add-label/0.2/gitlab-add-label.yaml +``` + +### Parameters + +- **GITLAB_HOST_URL**: The Gitlab host(_default:_`gitlab.com`). +- **API_PATH_PREFIX**: The API path prefix (_default.:_`api/v4`). +- **REQUEST_URL**: The Gitlab issue or merge request URL where we want to add labels (_e.g._`https://gitlab.com/foo/bar/issues/10`). +- **LABELS**: The actual labels to add. +- **GITLAB_TOKEN_SECRET**: The name of the `secret` holding the gitlab-token (_default:_`gitlab`). +- **GITLAB_TOKEN_SECRET_KEY**: The name of the `secret key` holding the gitlab-token (_default:_`token`). + + +### Secrets + +* `Secret` to provide Gitlab `access token` to authenticate to the Gitlab. + +### Platforms + +The Task can be run on `linux/amd64` platform. + +### Usage + + +This task expects a secret named `gitlab` to exist, with a Gitlab token in `token` with enough privileges to add label to an issue. + +To add labels to an issue or merge request, put all the required params, add required secrets and labels will be created. + + +`Taskrun` can be created as follows: + +``` +apiVersion: tekton.dev/v1 +kind: TaskRun +metadata: + name: gitlab-add-label-run +spec: + taskRef: + name: gitlab-add-label + params: + - name: REQUEST_URL + value: https://gitlab.com/Divyanshu42/aws-cli/-/issues/1 # or https://gitlab.com/Divyanshu42/aws-cli/-/merge_requests/1 + - name: LABELS + value: + - bug + - kind/feature +``` diff --git a/task/gitlab-add-label/0.2/gitlab-add-label.yaml b/task/gitlab-add-label/0.2/gitlab-add-label.yaml new file mode 100644 index 0000000000..fd7c86eb54 --- /dev/null +++ b/task/gitlab-add-label/0.2/gitlab-add-label.yaml @@ -0,0 +1,116 @@ +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: gitlab-add-label + labels: + app.kubernetes.io/version: "0.2" + annotations: + tekton.dev/pipelines.minVersion: "0.50.0" + tekton.dev/categories: Git + tekton.dev/tags: gitlab + tekton.dev/displayName: "gitlab add label" + tekton.dev/platforms: "linux/amd64" +spec: + description: >- + This Task will add a label to an issue in Gitlab. + + A collection of tasks to help working with the GitLab API. + Most tasks would expect to have a secret set in the kubernetes + secret `gitlab`with a GitLab token in the key `token`. + + params: + - name: GITLAB_HOST_URL + description: | + The Gitlab host url + default: "gitlab.com" + type: string + + - name: API_PATH_PREFIX + description: | + The API path prefix + default: "/api/v4" + type: string + + - name: REQUEST_URL + description: | + The Gitlab issue or merge request URL where we want to add a label. + type: string + + - name: LABELS + description: | + The actual labels to add. + type: array + + - name: GITLAB_TOKEN_SECRET + description: | + Name of the secret holding the gitlab-token. + type: string + default: gitlab + + - name: GITLAB_TOKEN_SECRET_KEY + description: | + Name of the secret key holding the gitlab-token. + type: string + default: token + + steps: + - name: add-label + image: registry.access.redhat.com/ubi8/ubi:8.2@sha256:d748c21a55796827da82c61f162b505c59451cba24f1874e5185f21275b6bddc + args: + - "$(params.LABELS)" + script: | + #!/usr/libexec/platform-python + import json + import os + import sys + import http.client + import urllib.parse + + split_url = urllib.parse.urlparse( + "$(params.REQUEST_URL)").path.split("/") + + # Creating the API URL + + # This will convert https://gitlab.com/foo/bar/issues/202 to + # api url path /projects/foo/bar/issues/202 + + api_url = "$(params.API_PATH_PREFIX)" + "/projects/" + "%2F".join(split_url[1:3]) + \ + "/" + split_url[4] + "/" + split_url[-1] + + + # Get array of Labels + labels = sys.argv[1: ] + labels_string = ','.join(labels) + + data = { + "labels": labels_string + } + + # Adding the data as the query parameter + params = urllib.parse.urlencode(data) + api_url += "?" + params + + print("Sending the data to GitLab: ") + print(data) + conn = http.client.HTTPSConnection("$(params.GITLAB_HOST_URL)") + r = conn.request( + "PUT", + api_url, + headers={ + "Authorization": "Bearer " + os.environ["TOKEN"], + }) + + resp = conn.getresponse() + if not str(resp.status).startswith("2"): + print("Error: %d" % (resp.status)) + print(resp.read()) + else: + print("Labels has been added to " + "$(params.REQUEST_URL)") + + env: + - name: TOKEN + valueFrom: + secretKeyRef: + name: $(params.GITLAB_TOKEN_SECRET) + key: $(params.GITLAB_TOKEN_SECRET_KEY) diff --git a/task/gitlab-set-status/0.4/README.md b/task/gitlab-set-status/0.4/README.md new file mode 100644 index 0000000000..6bcd90c955 --- /dev/null +++ b/task/gitlab-set-status/0.4/README.md @@ -0,0 +1,41 @@ +# GitLab Set Status + +GitLab Set Status is part of a collection of GitLab tasks to help working +with the [GitLab API](https://docs.gitlab.com/ee/api/). + +## Set Status on a Commit/MR + +The `gitlab-set-status` task uses the [commit api](https://docs.gitlab.com/ee/api/commits.html#post-the-build-status-to-a-commit) +to mark GitLab commits with a `pending`,`running`, `success`, `failed`, or `canceled` state, which is then reflected in merge requests involving those commits. + +Statuses include as well a `description`, a `target_url` and a `coverage`, to give the user +informations about the CI statuses or a direct link to the full log. + +### Install the Task + +```shell +kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/main/task/gitlab-set-status/0.4/gitlab-set-status.yaml +``` + +### Parameters + +* **STATE** (string): The state of the status. Can be one of the following `pending`, `running`, `success`, `failed`, or `canceled`. +* **GITLAB_TOKEN_SECRET_NAME** (string, optional): The name of the kubernetes secret that contains the GitLab access token. _default:_ `gitlab-api-secret` +* **GITLAB_TOKEN_SECRET_KEY** (string, optional): The key within the kubernetes secret that contains the GitLab token. _default:_ `token` +* **SHA** (string): The commit SHA to set the status for. +* **TARGET_URL** (string): The target URL to associate with this status. This URL will + be linked from the GitLab UI to allow users to easily see the source of the + status. For example you can link to a + [dashboard](https://github.com/tektoncd/dashboard) URL so users can follow a + Pipeline/Task run. +* **GITLAB_HOST_URL** (string, optional): The GitLab host domain. _default:_ `gitlab.com` +* **API_PATH_PREFIX** (string, optional): The GitLab Enterprise has a prefix for the API path. _default:_ `/api/v4` +* **REPO_FULL_NAME** (string, optional): The GitLab repository full name. _default:_ `tektoncd/catalog` +* **DESCRIPTION** (string): A short description of the status. _e.g:_ `Building your MR` +* **CONTEXT** (string, optional): The GitLab context, A string label to differentiate this status + from the status of other systems. _e.g:_ `continuous-integration/tekton` +* **COVERAGE** (string, optional): The total code coverage. Should be a float string. _default:_ `""` + +## Platforms + +The Task can be run on `linux/amd64` platform. diff --git a/task/gitlab-set-status/0.4/gitlab-set-status.yaml b/task/gitlab-set-status/0.4/gitlab-set-status.yaml new file mode 100644 index 0000000000..912d77c730 --- /dev/null +++ b/task/gitlab-set-status/0.4/gitlab-set-status.yaml @@ -0,0 +1,163 @@ +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: gitlab-set-status + labels: + app.kubernetes.io/version: "0.4" + annotations: + tekton.dev/pipelines.minVersion: "0.50.0" + tekton.dev/categories: Git + tekton.dev/tags: gitlab, git + tekton.dev/displayName: "Set Gitlab commit status" + tekton.dev/platforms: "linux/amd64" +spec: + description: >- + This task will set the status of the CI job to the specified value along + with a link to the specified target URL where developers can follow the + progress of the CI job. + + The `gitlab-set-status` task allows external services to mark GitLab commits + with an `error`, `failure`, `pending`, or `success` state, which is then + reflected in merge requests involving those commits. Statuses include as well a + `description`, a `target_url` and a `coverage`, to give the user informations + about the CI statuses or a direct link to the full log. + + params: + - name: GITLAB_HOST_URL + description: | + The GitLab host, adjust this if you run a GitLab enterprise. + default: "gitlab.com" + type: string + + - name: API_PATH_PREFIX + description: | + The API path prefix, GitLab Enterprise has a prefix e.g. /api/v4 + default: "/api/v4" + type: string + + - name: REPO_FULL_NAME + description: | + The GitLab repository full name, e.g.: tektoncd/catalog + type: string + + - name: GITLAB_TOKEN_SECRET_NAME + description: | + The name of the kubernetes secret that contains the GitLab token, default: gitlab-api-secret + type: string + default: gitlab-api-secret + + - name: GITLAB_TOKEN_SECRET_KEY + description: | + The key within the kubernetes secret that contains the GitLab token, default: token + type: string + default: token + + - name: SHA + description: | + Commit SHA to set the status for. + type: string + + - name: TARGET_URL + description: | + The target URL to associate with this status. This URL will be linked + from the GitLab UI to allow users to easily see the source of the + status. + type: string + + - name: DESCRIPTION + description: | + A short description of the status. + type: string + + - name: CONTEXT + description: | + The GitLab context, A string label to differentiate this status from + the status of other systems. ie: "continuous-integration/tekton" + default: "continuous-integration/tekton" + type: string + + - name: STATE + description: | + The state of the status. Can be one of the following `pending`, + `running`, `success`, `failed`, or `canceled`. + type: string + + - name: COVERAGE + description: | + The total code coverage. + type: string + default: "" + + steps: + - name: set-status + image: registry.access.redhat.com/ubi8/python-38@sha256:af6f93b81f9313de95966e8cd681edb9dbcb5fdbddc5a4cc365af8e4534096ef + script: | + #!/usr/libexec/platform-python + + """This script will set the pipeline status of a commit on GitLab.""" + import os + import sys + import json + import http.client + import urllib.parse + + GITLAB_TOKEN = os.getenv("GITLAB_TOKEN") + GITLAB_HOST_URL = "$(params.GITLAB_HOST_URL)" + API_PATH_PREFIX = "$(params.API_PATH_PREFIX)" + REPO_FULL_NAME = "$(params.REPO_FULL_NAME)" + SHA = "$(params.SHA)" + STATE = "$(params.STATE)" + CONTEXT = "$(params.CONTEXT)" + TARGET_URL = "$(params.TARGET_URL)" + DESCRIPTION = "$(params.DESCRIPTION)" + COVERAGE = "$(params.COVERAGE)" + + headers = { + "User-Agent": "TektonCD, the peaceful cat", + "Authorization": f"Bearer {GITLAB_TOKEN}", + } + + URLENCODED_REPO_NAME = urllib.parse.quote(REPO_FULL_NAME, safe="") + + params = { + "state": STATE, + "context": CONTEXT, + "target_url": TARGET_URL, + "description": DESCRIPTION + } + + if COVERAGE: + params["coverage"] = float(COVERAGE) + + encoded_params = urllib.parse.urlencode(params) + + api_url = f"{API_PATH_PREFIX}/projects/{URLENCODED_REPO_NAME}/statuses/{SHA}?{encoded_params}" + + print(f"POST to {GITLAB_HOST_URL}{api_url}") + + if GITLAB_HOST_URL.startswith("http://"): + conn = http.client.HTTPConnection(GITLAB_HOST_URL[7:]) + elif GITLAB_HOST_URL.startswith("https://"): + conn = http.client.HTTPSConnection(GITLAB_HOST_URL[8:]) + else: + conn = http.client.HTTPSConnection(GITLAB_HOST_URL) + try: + conn.request("POST", api_url, headers=headers) + + resp = conn.getresponse() + if not str(resp.status).startswith("2"): + print(f"{resp.status} | Unable to set status") + response_data = json.dumps(json.loads(resp.read()), indent=4) + print(response_data) + sys.exit(1) + else: + print(f"Just set status of {REPO_FULL_NAME}#{SHA} to {STATE}") + finally: + conn.close() + + env: + - name: GITLAB_TOKEN + valueFrom: + secretKeyRef: + name: $(params.GITLAB_TOKEN_SECRET_NAME) + key: $(params.GITLAB_TOKEN_SECRET_KEY) diff --git a/task/gitlab-set-status/0.4/tests/fixtures/gitlab-set-status.yaml b/task/gitlab-set-status/0.4/tests/fixtures/gitlab-set-status.yaml new file mode 100644 index 0000000000..6e75a712ef --- /dev/null +++ b/task/gitlab-set-status/0.4/tests/fixtures/gitlab-set-status.yaml @@ -0,0 +1,11 @@ +--- +headers: + method: POST + path: /api/v4/projects/{repo:.+}/statuses/{[^/]+} +response: + status: 201 + output: | + { + "some": "data" + } + content-type: application/json \ No newline at end of file diff --git a/task/gitlab-set-status/0.4/tests/pre-apply-task-hook.sh b/task/gitlab-set-status/0.4/tests/pre-apply-task-hook.sh new file mode 100644 index 0000000000..96d386d4ab --- /dev/null +++ b/task/gitlab-set-status/0.4/tests/pre-apply-task-hook.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +kubectl -n ${tns} create secret generic gitlab-secret --from-literal token="secret" \ No newline at end of file diff --git a/task/gitlab-set-status/0.4/tests/run.yaml b/task/gitlab-set-status/0.4/tests/run.yaml new file mode 100644 index 0000000000..48ca0a90ce --- /dev/null +++ b/task/gitlab-set-status/0.4/tests/run.yaml @@ -0,0 +1,29 @@ +apiVersion: tekton.dev/v1 +kind: PipelineRun +metadata: + name: gitlab-set-status +spec: + pipelineSpec: + tasks: + - name: set-status + taskRef: + name: gitlab-set-status + params: + - name: GITLAB_HOST_URL + value: http://localhost:8080 + - name: REPO_FULL_NAME + value: tektoncd/catalog + - name: SHA + value: 1234567890abcdef + - name: TARGET_URL + value: http://localhost:8080/this-could-be-your-pipeline-dashboard + - name: DESCRIPTION + value: An update to Gitlab about our pipeline status + - name: STATE + value: success + - name: COVERAGE + value: "100.0" + - name: GITLAB_TOKEN_SECRET_NAME + value: gitlab-secret + - name: GITLAB_TOKEN_SECRET_KEY + value: token diff --git a/task/scorecard/0.2/README.md b/task/scorecard/0.2/README.md new file mode 100644 index 0000000000..91575455bf --- /dev/null +++ b/task/scorecard/0.2/README.md @@ -0,0 +1,38 @@ +# Scorecards + +These tasks provides scorecard results based on the [OSSF Scorecards](https://github.com/ossf/scorecard) project. + +## Install the Task + +```bash +kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/main/task/scorecard/0.2/scorecard.yaml +``` + +## Parameters + +- **REPO**: The url to the repository to inspect. +- **GITHUB-OAUTH_SECRET**: The name of the secret storing GitHub credentials. They should be in the `token` field. + +## Platforms + +The Task can be run on `linux/amd64` platform. + +## Usage + +This `TaskRun` runs `scorecard` on a repository: + +```yaml +apiVersion: tekton.dev/v1 +kind: TaskRun +metadata: + generateName: scorecard-run- +spec: + params: + - name: REPO + value: github.com/ossf/scorecard + - name: GITHUB-OAUTH_SECRET + value: github + taskRef: + kind: Task + name: scorecard +``` diff --git a/task/scorecard/0.2/scorecard.yaml b/task/scorecard/0.2/scorecard.yaml new file mode 100644 index 0000000000..6354120094 --- /dev/null +++ b/task/scorecard/0.2/scorecard.yaml @@ -0,0 +1,37 @@ +--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: scorecard + labels: + app.kubernetes.io/version: "0.2" + annotations: + tekton.dev/categories: Git + tekton.dev/pipelines.minVersion: "0.50.0" + tekton.dev/tags: scorecard + tekton.dev/displayName: OSSF scorecard + tekton.dev/platforms: "linux/amd64" +spec: + description: >- + This task can be used to calculate the scorecard results for a repository + + params: + - name: REPO + description: The repository URL to inspect + type: string + - name: GITHUB-OAUTH_SECRET + description: The GitHub secret with credentials + type: string + default: "github" + steps: + - name: scorecard + image: gcr.io/openssf/scorecard@sha256:c16e433f68799fe69dc0f9675c7ba5d2553846b57262550c86c71165269a7961 # tag v1 + script: | + /go/src/github.com/ossf/scorecard/scorecard --repo=$(inputs.params.REPO) + + env: + - name: GITHUB_AUTH_TOKEN + valueFrom: + secretKeyRef: + name: $(params.GITHUB-OAUTH_SECRET) + key: token \ No newline at end of file