Skip to content

Commit

Permalink
Merge pull request #14 from snow-actions/jcasc
Browse files Browse the repository at this point in the history
Support JCasC
  • Loading branch information
SnowCait authored May 16, 2022
2 parents c004fe2 + ffd47fe commit 0af8494
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,45 @@ jobs:
- run: java -jar jenkins-cli.jar -webSocket build job-1 -f -v
- run: java -jar jenkins-cli.jar -webSocket build job-2 -f -v -p param_1=p1

test-jcasc:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-20.04, ubuntu-18.04 ]
runs-on: ${{ matrix.os }}
timeout-minutes: 10

steps:
- uses: actions/checkout@v3
- run: mkdir -p $RUNNER_TEMP/jenkins_home/
- run: cp test-resources/jenkins.yaml $RUNNER_TEMP/jenkins_home/
- uses: ./
- run: curl $JENKINS_URL
- run: ls -la $JENKINS_HOME

# Environment variables
- run: test "$JENKINS_HOME" = "$RUNNER_TEMP/jenkins_home"
- run: test -n "$JENKINS_VERSION"
- run: test "$JENKINS_URL" = "http://localhost:8080"
- run: test -n "$COMPOSE_FILE"

# Jenkins container
- run: docker compose exec jenkins printenv
- run: docker compose exec jenkins jenkins-plugin-cli --list

# Remote Access API
- run: |
crumb=$(curl -sS -c cookie $JENKINS_URL/crumbIssuer/api/json)
echo "CRUMB_HEADER=$(echo $crumb | jq -r '.crumbRequestField'): $(echo $crumb | jq -r '.crumb')" >> $GITHUB_ENV
- run: test $(curl -sS -X POST -H "${CRUMB_HEADER}" -b cookie $JENKINS_URL/api/json | jq '.jobs | length') -eq 0

# Jenkins CLI
- run: wget $JENKINS_URL/jnlpJars/jenkins-cli.jar
- run: java -jar jenkins-cli.jar -webSocket list-plugins
- run: java -jar jenkins-cli.jar -webSocket list-jobs
- run: java -jar jenkins-cli.jar -webSocket create-job job-jcasc < test-resources/jobs/job-jcasc/config.xml
- run: java -jar jenkins-cli.jar -webSocket build job-jcasc -f -v

test-passing:
needs: [ test-mount, test-no-mount ]
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ A few features are available.
## Usage

1. Copy `$JENKINS_HOME/jobs/*/config.xml` from existing Jenkins to `jenkins_home/` in a repository
1. You can put `jenkins_home/jenkins.yaml` if [Jenkins Configuration as Code](https://www.jenkins.io/projects/jcasc/)
1. Create a workflow with the jenkins_home path

```yml
Expand Down
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ runs:
sudo chown -R 1000:1000 $JENKINS_HOME
shell: bash
if: env.JENKINS_HOME != ''
- name: Install JCasC plugin
run: |
if [ -f "$JENKINS_HOME/jenkins.yaml" ]; then
echo 'configuration-as-code' >> $GITHUB_ACTION_PATH/plugins.txt
fi
shell: bash
- run: cat $GITHUB_ACTION_PATH/plugins.txt
shell: bash
- run: docker compose config
shell: bash
env:
Expand Down
8 changes: 8 additions & 0 deletions test-resources/jenkins.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
jenkins:
globalNodeProperties:
- envVars:
env:
- key: VARIABLE1
value: foo
- key: VARIABLE2
value: bar
32 changes: 32 additions & 0 deletions test-resources/jobs/job-jcasc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version='1.1' encoding='UTF-8'?>
<flow-definition plugin="[email protected]">
<actions>
<org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction plugin="[email protected]"/>
<org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction plugin="[email protected]">
<jobProperties/>
<triggers/>
<parameters/>
<options/>
</org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobPropertyTrackerAction>
</actions>
<description></description>
<keepDependencies>false</keepDependencies>
<properties/>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="[email protected]_31b_f1">
<script>pipeline {
agent any

stages {
stage(&apos;stage 1&apos;) {
steps {
echo &quot;env: ${env.VARIABLE1}, ${env.VARIABLE2}&quot;
}
}
}
}
</script>
<sandbox>true</sandbox>
</definition>
<triggers/>
<disabled>false</disabled>
</flow-definition>

0 comments on commit 0af8494

Please sign in to comment.