├── .github ├── dependabot.yml └── workflows │ ├── pr_workflow.yml │ ├── release.yml │ ├── test_and_build.yml │ └── update-gradle-wrapper.yml ├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── images └── screenshot.png ├── settings.gradle └── src ├── main ├── java │ └── com │ │ └── tw │ │ └── go │ │ └── plugin │ │ └── task │ │ └── GoPluginImpl.java └── resources │ └── views │ └── task.template.html └── test └── java └── com └── tw └── go └── plugin └── task └── GoPluginImplTest.java /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: github-actions 4 | directory: / 5 | schedule: 6 | interval: monthly 7 | groups: 8 | github-actions: 9 | patterns: 10 | - "*" 11 | - package-ecosystem: gradle 12 | directory: / 13 | schedule: 14 | interval: monthly 15 | groups: 16 | gradle-deps: 17 | patterns: 18 | - "*" 19 | -------------------------------------------------------------------------------- /.github/workflows/pr_workflow.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Gradle 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle 3 | 4 | name: Testing For PRs 5 | 6 | on: [ pull_request ] 7 | 8 | permissions: 9 | contents: read 10 | 11 | jobs: 12 | test: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - name: Harden the runner (Audit all outbound calls) 16 | uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0 17 | with: 18 | egress-policy: audit 19 | 20 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 21 | - name: Set up JDK 22 | uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1 23 | with: 24 | java-version: 17 25 | distribution: temurin 26 | - name: Build with Gradle 27 | run: ./gradlew assemble check 28 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | 2 | name: Create Stable Release 3 | 4 | # Controls when the action will run. Workflow runs when manually triggered using the UI 5 | # or API. 6 | on: 7 | workflow_dispatch: 8 | # Inputs the workflow accepts. 9 | inputs: 10 | prerelease: 11 | description: 'The release should be an experimental release' 12 | default: 'NO' 13 | required: true 14 | 15 | jobs: 16 | build_and_release: 17 | runs-on: ubuntu-latest 18 | env: 19 | GITHUB_USER: "gocd-contrib" 20 | GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" 21 | PRERELEASE: "${{ github.event.inputs.prerelease }}" 22 | steps: 23 | - name: Harden the runner (Audit all outbound calls) 24 | uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0 25 | with: 26 | egress-policy: audit 27 | 28 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 29 | with: 30 | fetch-depth: 0 31 | - name: Set up JDK 32 | uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1 33 | with: 34 | java-version: 17 35 | distribution: temurin 36 | - name: Release 37 | run: ./gradlew githubRelease 38 | -------------------------------------------------------------------------------- /.github/workflows/test_and_build.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Gradle 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle 3 | 4 | name: Test and Build 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | 10 | jobs: 11 | test: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: Harden the runner (Audit all outbound calls) 15 | uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0 16 | with: 17 | egress-policy: audit 18 | 19 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 20 | - name: Set up JDK 21 | uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1 22 | with: 23 | java-version: 17 24 | distribution: temurin 25 | - name: Test with Gradle 26 | run: ./gradlew assemble check 27 | previewGithubRelease: 28 | needs: test 29 | runs-on: ubuntu-latest 30 | env: 31 | GITHUB_USER: "gocd-contrib" 32 | GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" 33 | steps: 34 | - name: Harden the runner (Audit all outbound calls) 35 | uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0 36 | with: 37 | egress-policy: audit 38 | 39 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 40 | with: 41 | fetch-depth: 0 42 | - name: Set up JDK 43 | uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1 44 | with: 45 | java-version: 17 46 | distribution: temurin 47 | - name: Test with Gradle 48 | run: ./gradlew githubRelease 49 | -------------------------------------------------------------------------------- /.github/workflows/update-gradle-wrapper.yml: -------------------------------------------------------------------------------- 1 | name: Update Gradle Wrapper 2 | 3 | on: 4 | schedule: 5 | - cron: "0 0 1 * *" 6 | workflow_dispatch: 7 | 8 | jobs: 9 | update-gradle-wrapper: 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - name: Harden the runner (Audit all outbound calls) 14 | uses: step-security/harden-runner@0634a2670c59f64b4a01f0f96f84700a4088b9f0 # v2.12.0 15 | with: 16 | egress-policy: audit 17 | 18 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 19 | 20 | - name: Update Gradle Wrapper 21 | uses: gradle-update/update-gradle-wrapper-action@512b1875f3b6270828abfe77b247d5895a2da1e5 # v2.1.0 22 | with: 23 | labels: dependencies 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.war 8 | *.ear 9 | 10 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 11 | hs_err_pid* 12 | 13 | # Eclipse 14 | .classpath 15 | .project 16 | .settings/ 17 | 18 | # Intellij 19 | .idea/ 20 | *.iml 21 | *.iws 22 | 23 | # Mac 24 | .DS_Store 25 | 26 | # Maven 27 | log/ 28 | target/ 29 | dist/ 30 | 31 | 32 | # gradle's output dir 33 | build/ 34 | 35 | # the gradle temp dir 36 | .gradle 37 | 38 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 39 | !gradle-wrapper.jar 40 | 41 | classes/ 42 | out 43 | src/main/resources-generated 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GoCD task plugin to run Shell scripts. 2 | 3 | *Usage:* 4 | 5 | ![usage screenshot](images/screenshot.png) 6 | 7 | *Notes:* 8 | 9 | This is only for convenience since you can run all commands directly through GoCD anyway. 10 | 11 | This could be preferable because: 12 | - Its exactly like shell script. GoCD's custom command takes a little getting used to. 13 | - Its easier to CRUD commands since its a script, specially re-order them if required. 14 | 15 | It should not be used as a replacement for important scripts like deployment scripts. They should remain in your repository. 16 | 17 | *Usage:* 18 | 19 | Download jar from releases & place it in `/plugins/external` & restart GoCD Server. 20 | 21 | ## Contributing 22 | 23 | We encourage you to contribute to GoCD. For information on contributing to this project, please see our [contributor's guide](http://www.go.cd/contribute). 24 | A lot of useful information like links to user documentation, design documentation, mailing lists etc. can be found in the [resources](http://www.go.cd/community/resources.html) section. 25 | 26 | ## License 27 | 28 | ```plain 29 | Copyright 2022 Thoughtworks, Inc. 30 | 31 | Licensed under the Apache License, Version 2.0 (the "License"); 32 | you may not use this file except in compliance with the License. 33 | You may obtain a copy of the License at 34 | 35 | http://www.apache.org/licenses/LICENSE-2.0 36 | 37 | Unless required by applicable law or agreed to in writing, software 38 | distributed under the License is distributed on an "AS IS" BASIS, 39 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 40 | See the License for the specific language governing permissions and 41 | limitations under the License. 42 | ``` 43 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Thoughtworks, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | apply plugin: 'java' 18 | apply from: "https://raw.githubusercontent.com/gocd/gocd-plugin-gradle-task-helpers/master/helper.gradle?_=${(int) (new Date().toInstant().epochSecond / 60)}" 19 | 20 | gocdPlugin { 21 | id = 'script-executor' 22 | pluginVersion = '1.0.3' 23 | goCdVersion = '20.1.0' 24 | name = 'Script Executor' 25 | description = 'Thoughtworks GoCD plugin to run scripts' 26 | vendorName = 'Srinivas Upadhya' 27 | vendorUrl = 'https://github.com/gocd-contrib/script-executor-task' 28 | 29 | githubRepo { 30 | owner = System.getenv('GITHUB_USER') ?: 'bob' 31 | repo = 'script-executor-task' 32 | token = System.getenv('GITHUB_TOKEN') ?: 'bad-token' 33 | } 34 | 35 | pluginProject = project 36 | 37 | prerelease = !"No".equalsIgnoreCase(System.getenv('PRERELEASE')) 38 | assetsToRelease = [project.tasks.jar] 39 | } 40 | 41 | java { 42 | sourceCompatibility = JavaVersion.VERSION_11 43 | targetCompatibility = JavaVersion.VERSION_11 44 | } 45 | 46 | allprojects { 47 | group = 'cd.go.contrib' 48 | version = gocdPlugin.fullVersion(project) 49 | } 50 | 51 | repositories { 52 | mavenCentral() 53 | } 54 | 55 | ext { 56 | deps = [ 57 | gocdPluginApi: 'cd.go.plugin:go-plugin-api:25.2.0', 58 | ] 59 | 60 | versions = project.ext.deps.collectEntries { lib, libGav -> [lib, libGav.split(':').last()] } 61 | } 62 | 63 | dependencies { 64 | compileOnly project.deps.gocdPluginApi 65 | implementation group: 'com.google.code.gson', name: 'gson', version: '2.13.1' 66 | 67 | testImplementation platform('org.junit:junit-bom:5.13.0') 68 | testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api' 69 | testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params' 70 | testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine' 71 | testRuntimeOnly group: 'org.junit.platform', name: 'junit-platform-launcher' 72 | 73 | testImplementation group: 'org.mockito', name: 'mockito-core', version: '5.18.0' 74 | testImplementation group: 'org.assertj', name: 'assertj-core', version: '3.27.3' 75 | testImplementation group: 'cd.go.plugin', name: 'go-plugin-api', version: project.versions.gocdPluginApi 76 | } 77 | 78 | test { 79 | useJUnitPlatform() 80 | } 81 | 82 | jar { 83 | from(configurations.runtimeClasspath) { 84 | into "lib/" 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gocd-contrib/script-executor-task/08f75cdc889149b49edeadba5d7ae231a17bd697/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionSha256Sum=845952a9d6afa783db70bb3b0effaae45ae5542ca2bb7929619e8af49cb634cf 4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.1-bin.zip 5 | networkTimeout=10000 6 | validateDistributionUrl=true 7 | zipStoreBase=GRADLE_USER_HOME 8 | zipStorePath=wrapper/dists 9 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | # SPDX-License-Identifier: Apache-2.0 19 | # 20 | 21 | ############################################################################## 22 | # 23 | # Gradle start up script for POSIX generated by Gradle. 24 | # 25 | # Important for running: 26 | # 27 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 28 | # noncompliant, but you have some other compliant shell such as ksh or 29 | # bash, then to run this script, type that shell name before the whole 30 | # command line, like: 31 | # 32 | # ksh Gradle 33 | # 34 | # Busybox and similar reduced shells will NOT work, because this script 35 | # requires all of these POSIX shell features: 36 | # * functions; 37 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 38 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 39 | # * compound commands having a testable exit status, especially «case»; 40 | # * various built-in commands including «command», «set», and «ulimit». 41 | # 42 | # Important for patching: 43 | # 44 | # (2) This script targets any POSIX shell, so it avoids extensions provided 45 | # by Bash, Ksh, etc; in particular arrays are avoided. 46 | # 47 | # The "traditional" practice of packing multiple parameters into a 48 | # space-separated string is a well documented source of bugs and security 49 | # problems, so this is (mostly) avoided, by progressively accumulating 50 | # options in "$@", and eventually passing that to Java. 51 | # 52 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 53 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 54 | # see the in-line comments for details. 55 | # 56 | # There are tweaks for specific operating systems such as AIX, CygWin, 57 | # Darwin, MinGW, and NonStop. 58 | # 59 | # (3) This script is generated from the Groovy template 60 | # https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 61 | # within the Gradle project. 62 | # 63 | # You can find Gradle at https://github.com/gradle/gradle/. 64 | # 65 | ############################################################################## 66 | 67 | # Attempt to set APP_HOME 68 | 69 | # Resolve links: $0 may be a link 70 | app_path=$0 71 | 72 | # Need this for daisy-chained symlinks. 73 | while 74 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 75 | [ -h "$app_path" ] 76 | do 77 | ls=$( ls -ld "$app_path" ) 78 | link=${ls#*' -> '} 79 | case $link in #( 80 | /*) app_path=$link ;; #( 81 | *) app_path=$APP_HOME$link ;; 82 | esac 83 | done 84 | 85 | # This is normally unused 86 | # shellcheck disable=SC2034 87 | APP_BASE_NAME=${0##*/} 88 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) 89 | APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH="\\\"\\\"" 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | if ! command -v java >/dev/null 2>&1 137 | then 138 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 139 | 140 | Please set the JAVA_HOME variable in your environment to match the 141 | location of your Java installation." 142 | fi 143 | fi 144 | 145 | # Increase the maximum file descriptors if we can. 146 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 147 | case $MAX_FD in #( 148 | max*) 149 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 150 | # shellcheck disable=SC2039,SC3045 151 | MAX_FD=$( ulimit -H -n ) || 152 | warn "Could not query maximum file descriptor limit" 153 | esac 154 | case $MAX_FD in #( 155 | '' | soft) :;; #( 156 | *) 157 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 158 | # shellcheck disable=SC2039,SC3045 159 | ulimit -n "$MAX_FD" || 160 | warn "Could not set maximum file descriptor limit to $MAX_FD" 161 | esac 162 | fi 163 | 164 | # Collect all arguments for the java command, stacking in reverse order: 165 | # * args from the command line 166 | # * the main class name 167 | # * -classpath 168 | # * -D...appname settings 169 | # * --module-path (only if needed) 170 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 171 | 172 | # For Cygwin or MSYS, switch paths to Windows format before running java 173 | if "$cygwin" || "$msys" ; then 174 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 175 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 176 | 177 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 178 | 179 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 180 | for arg do 181 | if 182 | case $arg in #( 183 | -*) false ;; # don't mess with options #( 184 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 185 | [ -e "$t" ] ;; #( 186 | *) false ;; 187 | esac 188 | then 189 | arg=$( cygpath --path --ignore --mixed "$arg" ) 190 | fi 191 | # Roll the args list around exactly as many times as the number of 192 | # args, so each arg winds up back in the position where it started, but 193 | # possibly modified. 194 | # 195 | # NB: a `for` loop captures its iteration list before it begins, so 196 | # changing the positional parameters here affects neither the number of 197 | # iterations, nor the values presented in `arg`. 198 | shift # remove old arg 199 | set -- "$@" "$arg" # push replacement arg 200 | done 201 | fi 202 | 203 | 204 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 205 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 206 | 207 | # Collect all arguments for the java command: 208 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, 209 | # and any embedded shellness will be escaped. 210 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be 211 | # treated as '${Hostname}' itself on the command line. 212 | 213 | set -- \ 214 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 215 | -classpath "$CLASSPATH" \ 216 | -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ 217 | "$@" 218 | 219 | # Stop when "xargs" is not available. 220 | if ! command -v xargs >/dev/null 2>&1 221 | then 222 | die "xargs is not available" 223 | fi 224 | 225 | # Use "xargs" to parse quoted args. 226 | # 227 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 228 | # 229 | # In Bash we could simply go: 230 | # 231 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 232 | # set -- "${ARGS[@]}" "$@" 233 | # 234 | # but POSIX shell has neither arrays nor command substitution, so instead we 235 | # post-process each arg (as a line of input to sed) to backslash-escape any 236 | # character that might be a shell metacharacter, then use eval to reverse 237 | # that process (while maintaining the separation between arguments), and wrap 238 | # the whole thing up as a single "set" statement. 239 | # 240 | # This will of course break if any of these variables contains a newline or 241 | # an unmatched quote. 242 | # 243 | 244 | eval "set -- $( 245 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 246 | xargs -n1 | 247 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 248 | tr '\n' ' ' 249 | )" '"$@"' 250 | 251 | exec "$JAVACMD" "$@" 252 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | @rem SPDX-License-Identifier: Apache-2.0 17 | @rem 18 | 19 | @if "%DEBUG%"=="" @echo off 20 | @rem ########################################################################## 21 | @rem 22 | @rem Gradle startup script for Windows 23 | @rem 24 | @rem ########################################################################## 25 | 26 | @rem Set local scope for the variables with windows NT shell 27 | if "%OS%"=="Windows_NT" setlocal 28 | 29 | set DIRNAME=%~dp0 30 | if "%DIRNAME%"=="" set DIRNAME=. 31 | @rem This is normally unused 32 | set APP_BASE_NAME=%~n0 33 | set APP_HOME=%DIRNAME% 34 | 35 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 36 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 37 | 38 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 39 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 40 | 41 | @rem Find java.exe 42 | if defined JAVA_HOME goto findJavaFromJavaHome 43 | 44 | set JAVA_EXE=java.exe 45 | %JAVA_EXE% -version >NUL 2>&1 46 | if %ERRORLEVEL% equ 0 goto execute 47 | 48 | echo. 1>&2 49 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 50 | echo. 1>&2 51 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 52 | echo location of your Java installation. 1>&2 53 | 54 | goto fail 55 | 56 | :findJavaFromJavaHome 57 | set JAVA_HOME=%JAVA_HOME:"=% 58 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 59 | 60 | if exist "%JAVA_EXE%" goto execute 61 | 62 | echo. 1>&2 63 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 64 | echo. 1>&2 65 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 66 | echo location of your Java installation. 1>&2 67 | 68 | goto fail 69 | 70 | :execute 71 | @rem Setup the command line 72 | 73 | set CLASSPATH= 74 | 75 | 76 | @rem Execute Gradle 77 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* 78 | 79 | :end 80 | @rem End local scope for the variables with windows NT shell 81 | if %ERRORLEVEL% equ 0 goto mainEnd 82 | 83 | :fail 84 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 85 | rem the _cmd.exe /c_ return code! 86 | set EXIT_CODE=%ERRORLEVEL% 87 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 88 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 89 | exit /b %EXIT_CODE% 90 | 91 | :mainEnd 92 | if "%OS%"=="Windows_NT" endlocal 93 | 94 | :omega 95 | -------------------------------------------------------------------------------- /images/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gocd-contrib/script-executor-task/08f75cdc889149b49edeadba5d7ae231a17bd697/images/screenshot.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 Thoughtworks, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | rootProject.name = 'script-executor' 18 | -------------------------------------------------------------------------------- /src/main/java/com/tw/go/plugin/task/GoPluginImpl.java: -------------------------------------------------------------------------------- 1 | /*************************GO-LICENSE-START********************************* 2 | * Copyright 2022 Thoughtworks, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | *************************GO-LICENSE-END***********************************/ 16 | 17 | package com.tw.go.plugin.task; 18 | 19 | import com.google.gson.Gson; 20 | import com.google.gson.GsonBuilder; 21 | import com.thoughtworks.go.plugin.api.GoApplicationAccessor; 22 | import com.thoughtworks.go.plugin.api.GoPlugin; 23 | import com.thoughtworks.go.plugin.api.GoPluginIdentifier; 24 | import com.thoughtworks.go.plugin.api.annotation.Extension; 25 | import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest; 26 | import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse; 27 | import com.thoughtworks.go.plugin.api.task.JobConsoleLogger; 28 | 29 | import java.io.File; 30 | import java.io.IOException; 31 | import java.io.InputStream; 32 | import java.nio.charset.StandardCharsets; 33 | import java.nio.file.Files; 34 | import java.nio.file.Path; 35 | import java.nio.file.Paths; 36 | import java.util.*; 37 | 38 | 39 | @Extension 40 | public class GoPluginImpl implements GoPlugin { 41 | public final static String EXTENSION_NAME = "task"; 42 | public final static List SUPPORTED_API_VERSIONS = List.of("1.0"); 43 | 44 | public final static String REQUEST_CONFIGURATION = "configuration"; 45 | public final static String REQUEST_CONFIGURATION_2 = "go.plugin-settings.get-configuration"; 46 | public final static String REQUEST_VALIDATION = "validate"; 47 | public final static String REQUEST_VALIDATION_2 = "go.plugin-settings.validate-configuration"; 48 | public final static String REQUEST_TASK_VIEW = "view"; 49 | public final static String REQUEST_TASK_VIEW_2 = "go.plugin-settings.get-view"; 50 | public final static String REQUEST_EXECUTION = "execute"; 51 | 52 | public static final int SUCCESS_RESPONSE_CODE = 200; 53 | private static final Gson GSON = new GsonBuilder().create(); 54 | 55 | @Override 56 | public void initializeGoApplicationAccessor(GoApplicationAccessor goApplicationAccessor) { 57 | // ignore 58 | } 59 | 60 | @Override 61 | public GoPluginApiResponse handle(GoPluginApiRequest goPluginApiRequest) { 62 | if (goPluginApiRequest.requestName().equals(REQUEST_CONFIGURATION) || goPluginApiRequest.requestName().equals(REQUEST_CONFIGURATION_2)) { 63 | return handleConfiguration(); 64 | } else if (goPluginApiRequest.requestName().equals(REQUEST_VALIDATION) || goPluginApiRequest.requestName().equals(REQUEST_VALIDATION_2)) { 65 | return handleValidation(); 66 | } else if (goPluginApiRequest.requestName().equals(REQUEST_TASK_VIEW) || goPluginApiRequest.requestName().equals(REQUEST_TASK_VIEW_2)) { 67 | try { 68 | return handleView(); 69 | } catch (IOException e) { 70 | String message = "Failed to find template: " + e.getMessage(); 71 | return renderJSON(500, message); 72 | } 73 | } else if (goPluginApiRequest.requestName().equals(REQUEST_EXECUTION)) { 74 | return handleExecute(goPluginApiRequest); 75 | } 76 | return null; 77 | } 78 | 79 | @Override 80 | public GoPluginIdentifier pluginIdentifier() { 81 | return new GoPluginIdentifier(EXTENSION_NAME, SUPPORTED_API_VERSIONS); 82 | } 83 | 84 | private GoPluginApiResponse handleConfiguration() { 85 | Map response = new HashMap<>(); 86 | response.put("script", createField("Script", null, "0")); 87 | response.put("shtype", createField("Shell", "bash", "1")); 88 | return renderJSON(SUCCESS_RESPONSE_CODE, response); 89 | } 90 | 91 | private GoPluginApiResponse handleValidation() { 92 | Map response = new HashMap<>(); 93 | return renderJSON(SUCCESS_RESPONSE_CODE, response); 94 | } 95 | 96 | private GoPluginApiResponse handleView() throws IOException { 97 | try (InputStream resource = getClass().getResourceAsStream("/views/task.template.html")) { 98 | Map response = new HashMap<>(); 99 | response.put("displayValue", "Script Executor"); 100 | response.put("template", new String(Objects.requireNonNull(resource).readAllBytes(), StandardCharsets.UTF_8)); 101 | return renderJSON(SUCCESS_RESPONSE_CODE, response); 102 | } 103 | } 104 | 105 | @SuppressWarnings("unchecked") 106 | private GoPluginApiResponse handleExecute(GoPluginApiRequest goPluginApiRequest) { 107 | Map response = new HashMap<>(); 108 | String workingDirectory = null; 109 | String scriptFileName = null; 110 | boolean isWindows = isWindows(); 111 | try { 112 | Map map = (Map) GSON.fromJson(goPluginApiRequest.requestBody(), Object.class); 113 | 114 | Map configKeyValuePairs = (Map) map.get("config"); 115 | Map context = (Map) map.get("context"); 116 | workingDirectory = (String) context.get("workingDirectory"); 117 | Map environmentVariables = (Map) context.get("environmentVariables"); 118 | 119 | Map scriptConfig = (Map) configKeyValuePairs.get("script"); 120 | String scriptValue = scriptConfig.get("value"); 121 | JobConsoleLogger.getConsoleLogger().printLine("[script-executor] Script: \n "); 122 | JobConsoleLogger.getConsoleLogger().printLine("[script-executor] -------------------------"); 123 | JobConsoleLogger.getConsoleLogger().printLine(scriptValue); 124 | JobConsoleLogger.getConsoleLogger().printLine("[script-executor] -------------------------"); 125 | Map shTypeConfig = (Map) configKeyValuePairs.get("shtype"); 126 | String shType = shTypeConfig.get("value"); 127 | if (shType == null || shType.trim().equals("")) { 128 | shType = "bash"; 129 | } 130 | JobConsoleLogger.getConsoleLogger().printLine("[script-executor] Script Type: " + shType); 131 | 132 | scriptFileName = generateScriptFileName(isWindows); 133 | 134 | createScript(workingDirectory, scriptFileName, isWindows, scriptValue); 135 | 136 | int exitCode = executeScript(workingDirectory, shType, scriptFileName, isWindows, environmentVariables); 137 | 138 | if (exitCode == 0) { 139 | response.put("success", true); 140 | response.put("message", "[script-executor] Script completed successfully."); 141 | } else { 142 | response.put("success", false); 143 | response.put("message", "[script-executor] Script completed with exit code: " + exitCode + "."); 144 | } 145 | } catch (Exception e) { 146 | response.put("success", false); 147 | response.put("message", "[script-executor] Script execution interrupted. Reason: " + e.getMessage()); 148 | } 149 | deleteScript(workingDirectory, scriptFileName); 150 | return renderJSON(SUCCESS_RESPONSE_CODE, response); 151 | } 152 | 153 | private boolean isWindows() { 154 | String osName = System.getProperty("os.name"); 155 | boolean isWindows = osName.toLowerCase().contains("windows"); 156 | JobConsoleLogger.getConsoleLogger().printLine("[script-executor] OS detected: '" + osName + "'. Is Windows? " + isWindows); 157 | return isWindows; 158 | } 159 | 160 | private String generateScriptFileName(Boolean isWindows) { 161 | return UUID.randomUUID() + (isWindows ? ".bat" : ".sh"); 162 | } 163 | 164 | private Path getScriptPath(String workingDirectory, String scriptFileName) { 165 | return Paths.get(workingDirectory, scriptFileName); 166 | } 167 | 168 | private void createScript(String workingDirectory, String scriptFileName, Boolean isWindows, String scriptValue) throws IOException, InterruptedException { 169 | Path scriptPath = getScriptPath(workingDirectory, scriptFileName); 170 | Files.writeString(scriptPath, cleanupScript(scriptValue), StandardCharsets.UTF_8); 171 | 172 | if (!isWindows) { 173 | executeCommand(workingDirectory, null, "chmod", "u+x", scriptFileName); 174 | } 175 | 176 | JobConsoleLogger.getConsoleLogger().printLine("[script-executor] Script written into '" + scriptPath.toAbsolutePath() + "'."); 177 | } 178 | 179 | String cleanupScript(String scriptValue) { 180 | return scriptValue.replaceAll("(\\r\\n|\\n|\\r)", System.getProperty("line.separator")); 181 | } 182 | 183 | private int executeScript(String workingDirectory, String shType, String scriptFileName, Boolean isWindows, Map environmentVariables) throws IOException, InterruptedException { 184 | if (isWindows) { 185 | return executeCommand(workingDirectory, environmentVariables, "cmd", "/c", scriptFileName); 186 | } 187 | String shCmd = "/bin/" + shType; 188 | return executeCommand(workingDirectory, environmentVariables, shCmd, "-c", "./" + scriptFileName); 189 | } 190 | 191 | private int executeCommand(String workingDirectory, Map environmentVariables, String... command) throws IOException, InterruptedException { 192 | ProcessBuilder processBuilder = new ProcessBuilder(command); 193 | processBuilder.directory(new File(workingDirectory)); 194 | if (environmentVariables != null && !environmentVariables.isEmpty()) { 195 | processBuilder.environment().putAll(environmentVariables); 196 | } 197 | Process process = processBuilder.start(); 198 | 199 | JobConsoleLogger.getConsoleLogger().readOutputOf(process.getInputStream()); 200 | JobConsoleLogger.getConsoleLogger().readErrorOf(process.getErrorStream()); 201 | 202 | return process.waitFor(); 203 | } 204 | 205 | private void deleteScript(String workingDirectory, String scriptFileName) { 206 | if (scriptFileName != null && !scriptFileName.isEmpty()) { 207 | try { 208 | Files.deleteIfExists(getScriptPath(workingDirectory, scriptFileName)); 209 | } catch (IOException ignore) { 210 | } 211 | } 212 | } 213 | 214 | private Map createField(String displayName, String defaultValue, String displayOrder) { 215 | Map fieldProperties = new HashMap<>(); 216 | fieldProperties.put("display-name", displayName); 217 | fieldProperties.put("default-value", defaultValue); 218 | fieldProperties.put("required", true); 219 | fieldProperties.put("secure", false); 220 | fieldProperties.put("display-order", displayOrder); 221 | return fieldProperties; 222 | } 223 | 224 | private GoPluginApiResponse renderJSON(final int responseCode, Object response) { 225 | final String json = response == null ? null : GSON.toJson(response); 226 | return new GoPluginApiResponse() { 227 | @Override 228 | public int responseCode() { 229 | return responseCode; 230 | } 231 | 232 | @Override 233 | public Map responseHeaders() { 234 | return null; 235 | } 236 | 237 | @Override 238 | public String responseBody() { 239 | return json; 240 | } 241 | }; 242 | } 243 | } 244 | -------------------------------------------------------------------------------- /src/main/resources/views/task.template.html: -------------------------------------------------------------------------------- 1 |
2 | 3 | 9 | 10 | 11 | {{ GOINPUTNAME[script].$error.server }} 12 | {{ GOINPUTNAME[shtype].$error.server }} 13 |
-------------------------------------------------------------------------------- /src/test/java/com/tw/go/plugin/task/GoPluginImplTest.java: -------------------------------------------------------------------------------- 1 | package com.tw.go.plugin.task; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.assertj.core.api.Assertions.assertThat; 6 | 7 | public class GoPluginImplTest { 8 | @Test 9 | public void shouldCleanupScript() { 10 | GoPluginImpl plugin = new GoPluginImpl(); 11 | String lineSeparator = System.getProperty("line.separator"); 12 | 13 | assertThat(plugin.cleanupScript("")).isEqualTo(""); 14 | assertThat(plugin.cleanupScript("a")).isEqualTo("a"); 15 | assertThat(plugin.cleanupScript("a\n")).isEqualTo("a" + lineSeparator); 16 | assertThat(plugin.cleanupScript("a\nb")).isEqualTo("a" + lineSeparator + "b"); 17 | assertThat(plugin.cleanupScript("a\rb")).isEqualTo("a" + lineSeparator + "b"); 18 | assertThat(plugin.cleanupScript("a\r\nb")).isEqualTo("a" + lineSeparator + "b"); 19 | } 20 | } --------------------------------------------------------------------------------