Add Jenkins lab

This commit is contained in:
Begona Guereca
2022-08-16 10:28:34 -07:00
parent 5f6572bdbb
commit 1b16db06f1
3 changed files with 184 additions and 272 deletions
-271
View File
@@ -1,271 +0,0 @@
# Dry run the migration of an Jenkins pipeline to GitHub Actions
In this lab, you will use the Valet `dry-run` command to convert a Jenkins pipeline to its equivalent GitHub Actions workflow.
The end result of this command will be the actions workflow written to your local filesystem.
- [Prerequisites](#prerequisites)
- [Perform a dry run](#perform-a-dry-run)
- [Review dry-run output](#review-dry-run-output)
- [Includes Dry-Run](#includes-dry-run)
- [Next Lab](#next-lab)
## Prerequisites
1. Followed [steps](../Jenkins#readme) to set up your codespace environment.
2. Completed the [configure lab](../Jenkins/valet-configure-lab.md)
## Perform a dry run
We will be performing a dry-run against a preconfigured project in the Jenkins instance. Before running the command we need to collect some information:
1. What is the project we want to convert? __basic-pipeline-example__
2. What is the namespace for that project? __Valet. In this case the namespace is the same as the group the project is in__
3. Where do we want to store the result? __./tmp/dry-run-lab. This can be any valid path on the system. In the case of codespaces it is generally best to use `./tmp/SOME_DIRECTORY_HERE` so the files show in explorer__
### Steps
1. Navigate to the codespace terminal
2. Run the dry-run command using the values determined above
```
gh valet dry-run Jenkins --output-dir ./tmp/dry-run-lab --namespace valet --project basic-pipeline-example
```
3. When the command finishes the output files should be printed to the terminal.
<img width="1112" alt="dry-run-terminal" src="https://user-images.githubusercontent.com/18723510/184173635-aec28d1c-8c61-4dcf-a743-f86cbdc836c5.png">
4. Open generated actions workflow
- Find `./tmp/dry-run-lab/valet` in the file explorer pane in codespaces.
- Click `basic-pipeline-example.yml` to open
<img width="231" alt="dry-run-explorer" src="https://user-images.githubusercontent.com/18723510/184177477-747905a8-32f3-4c15-8955-32079844a509.png">
## Review dry-run output
The dry-run output will show you the GitHub Actions yaml that would be migrated to GitHub with the `migrate` command. We will now take a quick look at what was generated.
__Click to Expand__
<details>
<summary><em>Jenkins Pipeline</em> </summary>
```yaml
stages:
- build
- test
- deploy
image: alpine
build_a:
stage: build
script:
- echo "This job builds something."
- sleep 100
build_b:
stage: build
script:
- echo "This job builds something else."
- sleep 70
test_a:
stage: test
script:
- echo "This job tests something. It will only run when all jobs in the"
- echo "build stage are complete."
test_b:
stage: test
script:
- echo "This job tests something else. It will only run when all jobs in the"
- echo "build stage are complete too. It will start at about the same time as test_a."
- sleep 300
deploy_a:
stage: deploy
script:
- echo "This job deploys something. It will only run when all jobs in the"
- echo "test stage complete."
- sleep 600
deploy_b:
stage: deploy
script:
- echo "This job deploys something else. It will only run when all jobs in the"
- echo "test stage complete. It will start at about the same time as deploy_a."
- sleep 400
```
</details>
<details>
<summary><em>Actions Workflow</em></summary>
```yaml
name: valet/basic-pipeline-example
on:
push:
workflow_dispatch:
concurrency:
group: "${{ github.ref }}"
cancel-in-progress: true
jobs:
build_a:
runs-on: ubuntu-latest
container:
image: alpine
timeout-minutes: 60
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 20
lfs: true
- run: echo "This job builds something."
- run: sleep 100
build_b:
runs-on: ubuntu-latest
container:
image: alpine
timeout-minutes: 60
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 20
lfs: true
- run: echo "This job builds something else."
- run: sleep 70
test_a:
needs:
- build_a
- build_b
runs-on: ubuntu-latest
container:
image: alpine
timeout-minutes: 60
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 20
lfs: true
- run: echo "This job tests something. It will only run when all jobs in the"
- run: echo "build stage are complete."
test_b:
needs:
- build_a
- build_b
runs-on: ubuntu-latest
container:
image: alpine
timeout-minutes: 60
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 20
lfs: true
- run: echo "This job tests something else. It will only run when all jobs in the"
- run: echo "build stage are complete too. It will start at about the same time as test_a."
- run: sleep 300
deploy_a:
needs:
- test_a
- test_b
runs-on: ubuntu-latest
container:
image: alpine
timeout-minutes: 60
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 20
lfs: true
- run: echo "This job deploys something. It will only run when all jobs in the"
- run: echo "test stage complete."
- run: sleep 600
deploy_b:
needs:
- test_a
- test_b
runs-on: ubuntu-latest
container:
image: alpine
timeout-minutes: 60
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 20
lfs: true
- run: echo "This job deploys something else. It will only run when all jobs in the"
- run: echo "test stage complete. It will start at about the same time as deploy_a."
- run: sleep 400
```
</details>
In the Jenkins pipeline we had 3 stages and 6 jobs that run on a alpine image
In the Actions workflow we have the same jobs (`build_a`, `build_b`, `test_a`, `test_b`, `deploy_a`, `deploy_b`) and the stages are now being enforced using the `needs` keyword. We can see this if we examine the `needs` for `test_a` and `test_b`, which make the test jobs depend on the build jobs.
```diff
- stages: test
+ needs:
+ - build_a
+ - build_b
```
The `image` in the Jenkins pipeline has be transformed to `container` on each of the jobs.
```diff
- image: alpine
+ container:
+ image: alpine
```
And `script` has been transformed to `run`
```diff
- script:
- - echo "This job builds something."
+ run: echo "This job builds something."
```
## Includes Dry-Run
In the previous dry-run we migrated a basic pipeline that mapped very nicely to concepts in GitHub Actions. In this section we will examine the results of a dry-run that does not map directly to Actions using the `included-files-example` pipeline, which looks like
```yaml
include:
- local: /config/build.Jenkins-ci.yml
- local: /config/test.Jenkins-ci.yml
```
and results in the below yaml. The difference to note here is that Valet transformed the pipeline into a single workflow, it did not create reusable workflows for the `include` files `/config/build.Jenkins-ci.yml` and `/config/test.Jenkins-ci.yml`. The reason for this is that the dependency graph of how the jobs run could not be guaranteed using reusable workflows. This is an example of how concepts in Jenkins don't always map directly to Actions and Valet has to make a decision on the safest path forward. It is likely this could be refactored to use [reusable workflow](https://docs.github.com/en/actions/using-workflows/reusing-workflows) at a later date with a deeper understanding of the pipeline.
```yaml
name: valet/included-files-example
on:
push:
pull_request:
workflow_dispatch:
concurrency:
group: "${{ github.ref }}"
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 50
lfs: true
- run: echo "[BEFORE_SCRIPT] this is from test.Jenkins-ci.yml"
- run: echo "this is from a local file"
test:
needs: build
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 50
lfs: true
- run: echo "[BEFORE_SCRIPT] this is from test.Jenkins-ci.yml"
- run: echo "this is from a local file"
```
Try constructing and running the `dry-run` command yourself. Hint, you should just have to change the project name.
## Next Lab
[Audit Jenkins Pipelines to GitHub Actions](../Jenkins/valet-audit-lab.md)
+1 -1
View File
@@ -75,4 +75,4 @@ To verify Valet works we are going to run a `update` and `dry-run` command. We
### Next Lab
TODO
[Dry-run Jenkins Pipelines to a GitHub Action Workflow](../Jenkins/valet-dry-run-lab.md)
+183
View File
@@ -0,0 +1,183 @@
# Dry run the migration of an Jenkins pipeline to GitHub Actions
In this lab, you will use the Valet `dry-run` command to convert a Jenkins pipeline to its equivalent GitHub Actions workflow.
The end result of this command will be the actions workflow written to your local filesystem.
- [Prerequisites](#prerequisites)
- [Perform a dry run](#perform-a-dry-run)
- [Review dry-run output](#review-dry-run-output)
- [Next Lab](#next-lab)
## Prerequisites
1. Followed [steps](../Jenkins#readme) to set up your codespace environment.
2. Completed the [configure lab](../Jenkins/valet-configure-lab.md)
3. Completed the [audit lab](../Jenkins/valet-audit-lab.md)
## Perform a dry run
We will be performing a dry-run against a preconfigured pipeline in the Jenkins instance. Before running the command we need to collect some information:
1. What is the name of the pipeline we want to convert? __test_pipeline__
2. What is the source 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 valid path on the system. In the case of codespaces it is generally best to use `./tmp/SOME_DIRECTORY_HERE` so the files show in explorer__
### Steps
1. Navigate to the codespace terminal
2. Run the dry-run command using the values determined above
```
gh valet dry-run jenkins --source-url http://localhost:8080/job/test_pipeline -o .tmp/jenkins/dry-run
```
3. When the command finishes the output files should be printed to the terminal.
<img width="915" alt="Screen Shot 2022-08-16 at 9 54 26 AM" src="https://user-images.githubusercontent.com/19557880/184935603-5c2d4dfe-66ef-4cb1-9398-e96954ca72e3.png">
4. Open generated actions workflow
- Find `./tmp/dry-run-lab/valet` in the file explorer pane in codespaces.
- Click `test_pipeline.yml` to open
<img width="234" alt="Screen Shot 2022-08-16 at 9 55 44 AM" src="https://user-images.githubusercontent.com/19557880/184935840-d4bdcbc9-75e5-4918-a055-28b765eac50c.png">
## Review dry-run output
The dry-run output will show you the GitHub Actions yaml that would be migrated to GitHub with the `migrate` command. We will now take a quick look at what was generated.
__Click to Expand__
<details>
<summary><em>Jenkins Pipeline</em> </summary>
```yaml
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>
<summary><em>Actions Workflow</em></summary>
```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>
In the Jenkins pipeline we had 2 stages and 4 steps that run on the `TeamARunner` self-hosted runner.
In the Actions workflow we have the same steps and the stages are now being enforced using the `needs` keyword. We can see this if we examine the `needs` for `needs: build`, which make the test jobs depend on the build jobs.
```diff
- stages: test
+ needs: build
```
The `agent` in the Jenkins pipeline has be transformed to `container` on each of the jobs.
```diff
- agent {
- label 'TeamARunner'
- }
+ runs-on:
+ - self-hosted
+ - TeamARunner
```
And the `echo` commands remain mostly the same
```diff
- echo "Database engine is ${DB_ENGINE}"
+ - name: echo message
+ run: echo "DISABLE_AUTH is ${{ env.DISABLE_AUTH }}"
```
Note how Valet was not able to find a suitable conversion for the `sleep` command.
```diff
- sleep 80
+ # # This item has no matching transformer
+ # - sleep:
+ # - key: time
+ # value:
+ # isLiteral: true
+ # value: 80
```
Lastly, the `junit` command was transformed using a third party action `EnricoMi/publish-unit-test-result-action@v1.7`
```diff
- junit '**/target/*.xml'
+ - name: Publish test results
+ uses: EnricoMi/[email protected]
+ if: always()
+ with:
+ files: "**/target/*.xml"
```
Try constructing and running the `dry-run` command yourself. Hint, you should just have to change the project name.
## Next Lab
TODO