|
| 1 | +--- |
| 2 | +# Based on: https://github.com/redhat-appstudio/build-definitions/blob/main/task/update-infra-deployments/0.1/update-infra-deployments.yaml |
| 3 | +apiVersion: tekton.dev/v1beta1 |
| 4 | +kind: Task |
| 5 | +metadata: |
| 6 | + labels: |
| 7 | + app.kubernetes.io/version: "0.1" |
| 8 | + annotations: |
| 9 | + tekton.dev/pipelines.minVersion: "0.12.1" |
| 10 | + tekton.dev/tags: "appstudio, hacbs" |
| 11 | + name: update-repository |
| 12 | +spec: |
| 13 | + description: | |
| 14 | + Clones a repository, runs script in 'SCRIPT' parameter, and generates a pull-request to the repository if a change is detected. |
| 15 | + params: |
| 16 | + - name: COMMIT_BRANCH |
| 17 | + description: Name of the branch which holds the update created by the Pipeline |
| 18 | + - name: TARGET_BRANCH |
| 19 | + description: Name of the branch which is modified by the Pipeline |
| 20 | + - name: TARGET_GH_NAME |
| 21 | + description: Name of the repository which is modified by the Pipeline |
| 22 | + - name: TARGET_GH_OWNER |
| 23 | + description: Owner of the repository which is modified by the Pipeline |
| 24 | + - name: TARGET_GH_URL |
| 25 | + description: URL of github repository which is modified by the Pipeline |
| 26 | + - name: SCRIPT_IMAGE |
| 27 | + description: Image reference used to execute the script |
| 28 | + - name: SCRIPT_PATH |
| 29 | + description: Path to the script updating the repository |
| 30 | + - name: SCRIPT_ARGS |
| 31 | + description: Arguments to the bash script |
| 32 | + type: array |
| 33 | + - name: shared-secret |
| 34 | + default: infra-deployments-pr-creator |
| 35 | + description: secret in the namespace which contains private key for the GitHub App |
| 36 | + - name: GITHUB_APP_ID |
| 37 | + description: ID of Github app used for updating PR |
| 38 | + default: "305606" |
| 39 | + - name: GITHUB_APP_INSTALLATION_ID |
| 40 | + description: Installation ID of Github app in the organization |
| 41 | + default: "35269675" |
| 42 | + - name: GIT_IMAGE |
| 43 | + description: Image reference containing the git command |
| 44 | + default: registry.redhat.io/openshift-pipelines/pipelines-git-init-rhel8:v1.8.2-8@sha256:a538c423e7a11aae6ae582a411fdb090936458075f99af4ce5add038bb6983e8 |
| 45 | + - name: GIT_USER |
| 46 | + description: Username to appear in the commit |
| 47 | + default: "Tekton CI" |
| 48 | + - name: GIT_EMAIL |
| 49 | + description: Email to appear in the commit |
| 50 | + |
| 51 | + volumes: |
| 52 | + - name: infra-deployments-pr-creator |
| 53 | + secret: |
| 54 | + # 'private-key' - private key for Github app |
| 55 | + secretName: $(params.shared-secret) |
| 56 | + steps: |
| 57 | + - name: git-clone-repository |
| 58 | + image: $(params.GIT_IMAGE) |
| 59 | + workingDir: $(workspaces.workdir.path) |
| 60 | + env: |
| 61 | + - name: TARGET_BRANCH |
| 62 | + value: $(params.TARGET_BRANCH) |
| 63 | + - name: TARGET_GH_NAME |
| 64 | + value: $(params.TARGET_GH_NAME) |
| 65 | + - name: TARGET_GH_OWNER |
| 66 | + value: $(params.TARGET_GH_OWNER) |
| 67 | + - name: TARGET_GH_URL |
| 68 | + value: $(params.TARGET_GH_URL) |
| 69 | + script: | |
| 70 | + WORK_DIR="${PWD}/${TARGET_GH_OWNER}/${TARGET_GH_NAME}" |
| 71 | + if [ -e "${WORK_DIR}" ]; then |
| 72 | + echo "Clean checkout of '${TARGET_GH_URL}/${TARGET_BRANCH}' in '${WORK_DIR}'" |
| 73 | + cd "${WORK_DIR}" |
| 74 | + git clean -d --force |
| 75 | + git reset --hard |
| 76 | + git checkout "${TARGET_BRANCH}" |
| 77 | + else |
| 78 | + echo "Cloning '${TARGET_GH_URL}/${TARGET_BRANCH}' to '${WORK_DIR}'" |
| 79 | + mkdir -p "$(dirname "${WORK_DIR}")" |
| 80 | + cd "$(dirname "${WORK_DIR}")" |
| 81 | + git clone --branch "${TARGET_BRANCH}" "${TARGET_GH_URL}" "${TARGET_GH_NAME}" |
| 82 | + fi |
| 83 | + - name: run-update-script |
| 84 | + image: $(params.SCRIPT_IMAGE) |
| 85 | + workingDir: $(workspaces.workdir.path) |
| 86 | + env: |
| 87 | + - name: COMMIT_BRANCH |
| 88 | + value: $(params.COMMIT_BRANCH) |
| 89 | + - name: GIT_EMAIL |
| 90 | + value: $(params.GIT_EMAIL) |
| 91 | + - name: GIT_USER |
| 92 | + value: $(params.GIT_USER) |
| 93 | + - name: SCRIPT_PATH |
| 94 | + value: $(params.SCRIPT_PATH) |
| 95 | + - name: TARGET_BRANCH |
| 96 | + value: $(params.TARGET_BRANCH) |
| 97 | + - name: TARGET_GH_NAME |
| 98 | + value: $(params.TARGET_GH_NAME) |
| 99 | + - name: TARGET_GH_OWNER |
| 100 | + value: $(params.TARGET_GH_OWNER) |
| 101 | + args: ["$(params.SCRIPT_ARGS[*])"] |
| 102 | + script: | |
| 103 | + #!/bin/bash |
| 104 | + set -o errexit |
| 105 | + set -o nounset |
| 106 | + set -o pipefail |
| 107 | +
|
| 108 | + SCRIPT_ARGS=( "$@" ) |
| 109 | +
|
| 110 | + # Go to repository directory |
| 111 | + WORK_DIR="${PWD}/${TARGET_GH_OWNER}/${TARGET_GH_NAME}" |
| 112 | + cd "${WORK_DIR}" |
| 113 | + echo "${PWD}" |
| 114 | +
|
| 115 | + # Setup git |
| 116 | + git config --global safe.directory "${PWD}" |
| 117 | + git config --local user.email "$GIT_EMAIL" |
| 118 | + git config --local user.name "$GIT_USER" |
| 119 | +
|
| 120 | + # Create branch |
| 121 | + git branch --copy --force "$COMMIT_BRANCH" |
| 122 | + git checkout "$COMMIT_BRANCH" |
| 123 | +
|
| 124 | + # Run script |
| 125 | + UPSTREAM_COMMIT=$(git rev-parse HEAD) |
| 126 | + "${SCRIPT_PATH}" "${SCRIPT_ARGS[@]}" |
| 127 | +
|
| 128 | + # Log changes |
| 129 | + DATA=".commits.json" |
| 130 | + cat << EOF > "$DATA" |
| 131 | + { |
| 132 | + "branch": { |
| 133 | + "source": "$TARGET_BRANCH", |
| 134 | + "source_sha": "$UPSTREAM_COMMIT", |
| 135 | + "target": "$COMMIT_BRANCH" |
| 136 | + }, |
| 137 | + EOF |
| 138 | + echo -n ' "commits": [' >> "$DATA" |
| 139 | +
|
| 140 | + PREVIOUS_COMMIT=$UPSTREAM_COMMIT |
| 141 | + HEAD=$(git rev-parse HEAD) |
| 142 | + for COMMIT in $(git rev-list "$UPSTREAM_COMMIT..HEAD"); do |
| 143 | + git checkout "$COMMIT" |
| 144 | + if tail -1 "$DATA" | grep -q "}$" ; then |
| 145 | + echo "," |
| 146 | + else |
| 147 | + echo |
| 148 | + fi >> "$DATA" |
| 149 | + cat << EOF >> "$DATA" |
| 150 | + { |
| 151 | + "files": [ |
| 152 | + EOF |
| 153 | + for FILE in $(git diff --name-only "$PREVIOUS_COMMIT..$COMMIT"); do |
| 154 | + if tail -1 "$DATA" | grep -q "}$" ; then |
| 155 | + echo "," >> "$DATA" |
| 156 | + fi |
| 157 | + echo " {" >> "$DATA" |
| 158 | + if [ -e "$FILE" ]; then |
| 159 | + cat << EOF >> "$DATA" |
| 160 | + "content": "$(cat "$FILE" | base64 | tr -d "\n")", |
| 161 | + "mode": "$(git ls-files --format='%(objectmode)' "$FILE")", |
| 162 | + EOF |
| 163 | + fi |
| 164 | + cat << EOF >> "$DATA" |
| 165 | + "path": "$FILE" |
| 166 | + EOF |
| 167 | + echo -n " }" >> "$DATA" |
| 168 | + done |
| 169 | + MESSAGE=$(git log -1 --format="%B" "$COMMIT" | sed "s:$:\\\n:g" | tr -d "\n") 2>/dev/null |
| 170 | + cat << EOF >> "$DATA" |
| 171 | +
|
| 172 | + ], |
| 173 | + "message": "$MESSAGE" |
| 174 | + EOF |
| 175 | + echo -n " }" >> "$DATA" |
| 176 | + done |
| 177 | + if tail -1 "$DATA" | grep -q "\[$" ; then |
| 178 | + echo "]," |
| 179 | + else |
| 180 | + echo " |
| 181 | + ]," |
| 182 | + fi >> "$DATA" |
| 183 | + cat << EOF >> "$DATA" |
| 184 | + "user": { |
| 185 | + "email": "$GIT_EMAIL", |
| 186 | + "name": "$GIT_USER" |
| 187 | + } |
| 188 | + } |
| 189 | + EOF |
| 190 | + workspaces: |
| 191 | + - name: workdir |
| 192 | + description: Shared storage to keep a single copy of the repositories |
0 commit comments