Files
importer-labs/jenkins/3-dry-run.md
T

145 lines
4.0 KiB
Markdown
Raw Normal View History

2022-09-06 11:15:07 -07:00
# Perform a dry-run of a Jenkins pipeline
2022-08-16 10:28:34 -07:00
2022-09-06 12:20:34 -07:00
In this lab you will use the `dry-run` command to convert a Jenkins pipeline to its equivalent GitHub Actions workflow.
2022-08-16 10:28:34 -07:00
## Prerequisites
2022-09-06 11:15:07 -07:00
1. Followed the steps [here](./readme.md#configure-your-codespace) to set up your Codespace environment and start a Jenkins server.
2. Completed the [configure lab](./1-configure-lab.md#configuring-credentials).
2022-09-06 12:20:34 -07:00
3. Completed the [audit lab](./2-audit.md).
2022-08-16 10:28:34 -07:00
2022-08-17 15:19:45 -07:00
## Perform a dry-run
2022-08-16 10:28:34 -07:00
2022-09-06 11:15:07 -07:00
We will be performing a dry-run against a pipeline in the preconfigured Jenkins server. We will need to answer the following questions before running this command:
2022-08-16 10:28:34 -07:00
2022-09-06 11:15:07 -07:00
1. What is the name of the pipeline we want to convert?
- __test_pipeline__
2. What is the URL of the pipeline we want to convert?
- __<http://localhost:8080/job/test_pipeline>__
3. Where do we want to store the result?
- __./tmp/dry-run-lab__. This can be any path within the working directory that Valet commands are executed from.
2022-08-16 10:28:34 -07:00
### Steps
1. Navigate to the codespace terminal
2022-09-06 11:15:07 -07:00
2. Run the following command from the root directory:
2022-08-16 10:28:34 -07:00
2022-09-06 11:15:07 -07:00
```bash
gh valet dry-run jenkins --source-url http://localhost:8080/job/test_pipeline -o .tmp/jenkins/dry-run
```
2022-08-16 10:28:34 -07:00
2022-09-06 11:15:07 -07:00
3. The command will list all the files written to disk when the command succeeds.
2022-08-16 10:28:34 -07:00
2022-09-06 11:15:07 -07:00
![img](https://user-images.githubusercontent.com/19557880/184935603-5c2d4dfe-66ef-4cb1-9398-e96954ca72e3.png)
2022-08-16 10:28:34 -07:00
2022-09-06 11:15:07 -07:00
4. View the converted workflow:
- Find `./tmp/dry-run` in the file explorer pane in codespaces.
- Click `test_pipeline.yml` to open
2022-08-16 10:28:34 -07:00
2022-09-06 11:15:07 -07:00
## Inspect the output files
The files generated from the `dry-run` command represent the equivalent Actions workflow for the given Jenkins pipeline. The Jenkins pipeline and converted workflow can be seen below:
2022-08-16 10:28:34 -07:00
<details>
2022-09-06 11:15:07 -07:00
<summary><em>Jenkins Pipeline 👇</em></summary>
2022-08-16 10:28:34 -07:00
2022-09-06 11:15:07 -07:00
```groovy
2022-08-16 10:28:34 -07:00
pipeline {
agent {
label 'TeamARunner'
}
environment {
DISABLE_AUTH = 'true'
DB_ENGINE = 'sqlite'
}
stages {
stage('build') {
steps {
echo "Database engine is ${DB_ENGINE}"
sleep 80
echo "DISABLE_AUTH is ${DISABLE_AUTH}"
}
}
stage('test') {
steps{
junit '**/target/*.xml'
}
}
}
}
```
</details>
<details>
2022-09-06 11:15:07 -07:00
<summary><em>Converted workflow 👇</em></summary>
2022-08-16 10:28:34 -07:00
```yaml
name: test_pipeline
on:
push:
paths: "*"
schedule:
- cron: 0-29/10 * * * *
env:
DISABLE_AUTH: 'true'
DB_ENGINE: sqlite
jobs:
build:
runs-on:
- self-hosted
- TeamARunner
steps:
- name: checkout
uses: actions/checkout@v2
- name: echo message
run: echo "Database engine is ${{ env.DB_ENGINE }}"
# # This item has no matching transformer
# - sleep:
# - key: time
# value:
# isLiteral: true
# value: 80
- name: echo message
run: echo "DISABLE_AUTH is ${{ env.DISABLE_AUTH }}"
test:
runs-on:
- self-hosted
- TeamARunner
needs: build
steps:
- name: checkout
uses: actions/checkout@v2
- name: Publish test results
uses: EnricoMi/[email protected]
if: always()
with:
files: "**/target/*.xml"
```
</details>
2022-09-06 11:15:07 -07:00
These 2 pipelines function equivantly despite using different syntax. In this case, the pipeline conversion was “partially successful” (i.e. there were item(s) not automatically converted) and the unconverted item was placed as comment in the location the Jenkins pipeline used it. For example:
2022-08-16 10:28:34 -07:00
```diff
- sleep 80
+ # # This item has no matching transformer
+ # - sleep:
+ # - key: time
+ # value:
+ # isLiteral: true
+ # value: 80
```
2022-09-06 11:15:07 -07:00
In the next lab, we'll learn how to override Valet's default behavior and customize the converted workflow that is generate.
2022-08-16 10:28:34 -07:00
2022-09-06 11:15:07 -07:00
Try running the `dry-run` command for different pipelines in the Jenkins server. As a hint, you just have to change the `--source-url` CLI option.
2022-08-16 10:28:34 -07:00
2022-09-06 11:15:07 -07:00
## Next lab
2022-08-16 10:28:34 -07:00
2022-09-06 11:15:07 -07:00
[Use custom transformers to customize Valet's behavior](4-custom-transformers.md)