Commit ec9a0a3e authored by Janosch Deurer's avatar Janosch Deurer Committed by Ian Ernst
Browse files

Improve Labs for the Gitlab Advanced CI/CD training

parent e364bae6
Loading
Loading
Loading
Loading
+18 −24
Original line number Diff line number Diff line
@@ -175,15 +175,13 @@ Next, we will create a `.gitlab-ci.yml` file to define our tests.
        
    test binarysearch:
      script:
        - npm i -g jest
        - npm install jest-junit
        - jest binarysearch.test.js
        - npm install jest
        - node_modules/.bin/jest binarysearch.test.js

    test linearsearch:
      script:
        - npm i -g jest
        - npm install jest-junit
        - jest linearsearch.test.js
        - npm install jest
        - node_modules/.bin/jest linearsearch.test.js
    ```

1. Select **Commit changes**.
@@ -198,7 +196,7 @@ Let’s take a look at this set of job definitions to see if they can be made mo
    install deps:
      stage: deps
      script:
        - npm install jest-junit
        - npm install jest
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
@@ -226,7 +224,7 @@ The `.gitlab-ci.yml` file should now look like this:
  install deps:
    stage: deps
    script:
      - npm install jest-junit
      - npm install jest
    cache:
      key: $CI_COMMIT_REF_SLUG
      paths:
@@ -235,23 +233,21 @@ The `.gitlab-ci.yml` file should now look like this:
  test binarysearch:
    stage: test
    script:
      - npm i -g jest
      - npm install jest-junit
      - jest binarysearch.test.js
      - npm install jest
      - node_modules/.bin/jest binarysearch.test.js

  test linearsearch:
    stage: test
    script:
      - npm i -g jest
      - npm install jest-junit
      - jest linearsearch.test.js
      - npm install jest
      - node_modules/.bin/jest linearsearch.test.js
  ```

> With this definition, we create a cache with a key that matches the `CI_COMMIT_REF_SLUG`. This ensures that each job will receive a unique cache. The data being cached is the `node_modules` folder. To set up the cache for use, we use the script to run the install command for the `jest-junit` package, which we will use for report formatting in a later lab.
> With this definition, we create a cache with a key that matches the `CI_COMMIT_REF_SLUG`. This ensures that each job will receive a unique cache. The data being cached is the `node_modules` folder. To set up the cache for use, we use the script to run the install command for the `jest` package, which we will use to run the tests.

Now that we have a cache defined, we can remove the `jest-junit` package install from each job.
Now that we have a cache defined, we can remove the `jest` package install from each job.

1. Remove the `npm i jest-junit` commands from your jobs and replace it with a cache reference. Below is an example of the completed `.gitlab-ci.yml` file:
1. Remove the `npm install jest` commands from your jobs and replace it with a cache reference. Below is an example of the completed `.gitlab-ci.yml` file:

    ```yml
    stages:
@@ -264,27 +260,25 @@ Now that we have a cache defined, we can remove the `jest-junit` package install
    install deps:
      stage: deps
      script:
        - npm install jest-junit
        - npm install jest
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
          - node_modules

    test binarysearch:
      before_script:
        - npm install -g jest
      stage: test
      script:
        - jest binarysearch.test.js
        - node_modules/.bin/jest binarysearch.test.js
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
          - node_modules

    test linearsearch:
      before_script:
        - npm install -g jest
      stage: test
      script:
        - jest linearsearch.test.js
        - node_modules/.bin/jest linearsearch.test.js
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
+84 −83
Original line number Diff line number Diff line
@@ -26,27 +26,25 @@ In this lab, we will explore the different ways that we can configure testing in
  install deps:
    stage: deps
    script:
        - npm install jest-junit
      - npm install jest
    cache:
      key: $CI_COMMIT_REF_SLUG
      paths:
        - node_modules

  test binarysearch:
      before_script:
        - npm install -g jest
    stage: test
    script:
        - jest binarysearch.test.js
      - node_modules/.bin/jest binarysearch.test.js
    cache:
      key: $CI_COMMIT_REF_SLUG
      paths:
        - node_modules

  test linearsearch:
      before_script:
        - npm install -g jest
    stage: test
    script:
        - jest linearsearch.test.js
      - node_modules/.bin/jest linearsearch.test.js
    cache:
      key: $CI_COMMIT_REF_SLUG
      paths:
@@ -115,27 +113,25 @@ Now that we have verified the auto cancel works, let's remove the failing job.
    install deps:
      stage: deps
      script:
        - npm install jest-junit
        - npm install jest
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
          - node_modules

    test binarysearch:
      before_script:
        - npm install -g jest
      stage: test
      script:
        - jest binarysearch.test.js
        - node_modules/.bin/jest binarysearch.test.js
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
          - node_modules

    test linearsearch:
      before_script:
        - npm install -g jest
      stage: test
      script:
        - jest linearsearch.test.js
        - node_modules/.bin/jest linearsearch.test.js
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
@@ -154,24 +150,31 @@ In this task, we will add a test report to our test jobs.

1. Select **Edit > Edit in pipeline editor**.

1. We are going to adjust our `jest` commands for the `test binarysearch` and `test linearsearch` jobs to add a `testResultsProcessor` to the command.  We can do this by adding the `--ci --testResultsProcessor=jest-junit` flags to the command. The `--ci` option is provided will make Jest assume it is running in a CI environment. Below is an example of both jobs after the changes have been made:
1. We are going to adjust our `jest` commands for the `test binarysearch` and `test linearsearch` jobs to add a `testResultsProcessor` to the command.  We can do this by adding the `--ci --testResultsProcessor=jest-junit` flags to the command. The `--ci` option is provided will make Jest assume it is running in a CI environment. For this to work we also have to install `jest-junit`. Below is an example of the jobs after the changes have been made:

    ```yml
    install deps:
      stage: deps
      script:
        - npm install jest jest-junit
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
          - node_modules
    
    test binarysearch:
      before_script:
        - npm install -g jest
      stage: test
      script:
        - jest --ci --testResultsProcessor=jest-junit binarysearch.test.js
        - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit binarysearch.test.js
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
          - node_modules
    
    test linearsearch:
      before_script:
        - npm install -g jest
      stage: test
      script:
        - jest --ci --testResultsProcessor=jest-junit linearsearch.test.js
        - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit linearsearch.test.js
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
@@ -191,10 +194,9 @@ The tests will now look like this:

```yml
test binarysearch:
      before_script:
        - npm install -g jest
  stage: test
  script:
        - jest --ci --testResultsProcessor=jest-junit binarysearch.test.js
    - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit binarysearch.test.js
  artifacts:
    when: always
    reports:
@@ -205,10 +207,9 @@ The tests will now look like this:
      - node_modules

test linearsearch:
      before_script:
        - npm install -g jest
  stage: test
  script:
        - jest --ci --testResultsProcessor=jest-junit linearsearch.test.js
    - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit linearsearch.test.js
  artifacts:
    when: always
    reports:
+36 −41
Original line number Diff line number Diff line
@@ -31,27 +31,25 @@ default:
install deps:
  stage: deps
  script:
    - npm install jest-junit
    - npm install jest jest-junit
  cache:
    key: node_mod
    key: $CI_COMMIT_REF_SLUG
    paths:
      - node_modules
  
test binarysearch:
    before_script:
      - npm install -g jest
  stage: test
  script:
      - jest --ci --testResultsProcessor=jest-junit binarysearch.test.js
    - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit binarysearch.test.js
  cache:
    key: $CI_COMMIT_REF_SLUG
    paths:
     - node_modules

test linearsearch:
    before_script:
      - npm install -g jest
  stage: test
  script:
      - jest --ci --testResultsProcessor=jest-junit linearsearch.test.js
    - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit linearsearch.test.js
  artifacts:
    when: always
    reports:
@@ -84,8 +82,9 @@ install deps:

    ```yml
    test binarysearch:
      stage: test
      script:
        - jest --ci --testResultsProcessor=jest-junit binarysearch.test.js
        - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit binarysearch.test.js
      <<: *artifactdef
      cache:
        key: $CI_COMMIT_REF_SLUG
@@ -93,9 +92,11 @@ install deps:
          - node_modules

    test linearsearch:
      stage: test
      script:
        - jest --ci --testResultsProcessor=jest-junit linearsearch.test.js
        - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit linearsearch.test.js
      <<: *artifactdef
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
          - node_modules
@@ -130,26 +131,20 @@ install deps:
    install deps:
      stage: deps
      script:
        - npm install jest-junit
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
          - node_modules
        - npm install jest jest-junit
      <<: *cachedef

    test binarysearch:
      before_script:
        - npm install -g jest
      stage: test
      script:
        - jest --ci --testResultsProcessor=jest-junit binarysearch.test.js
        - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit binarysearch.test.js
      <<: [*artifactdef, *cachedef]

    test linearsearch:
      before_script:
        - npm install -g jest
      stage: test
      script:
        - jest --ci --testResultsProcessor=jest-junit linearsearch.test.js
        - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit linearsearch.test.js
      <<: [*artifactdef, *cachedef]
      
    ```

    > This change not only reduces the total number of lines of code, but also makes it so if the artifact changes, you only need to change it in one place, rather than multiple locations.
+59 −70
Original line number Diff line number Diff line
@@ -83,24 +83,19 @@ The current pipeline should look like this:
  install deps:
    stage: deps
    script:
        - npm install jest-junit
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
          - node_modules
      - npm install jest jest-junit
    <<: *cachedef

  test binarysearch:
      before_script:
        - npm install -g jest
    stage: test
    script:
        - jest --ci --testResultsProcessor=jest-junit binarysearch.test.js
      - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit binarysearch.test.js
    <<: [*artifactdef, *cachedef]

  test linearsearch:
      before_script:
        - npm install -g jest
    stage: test
    script:
        - jest --ci --testResultsProcessor=jest-junit linearsearch.test.js
      - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit linearsearch.test.js
    <<: [*artifactdef, *cachedef]
    
  pause:
@@ -173,29 +168,23 @@ When multiple users work on a project at the same time, merge conflicts are ofte
        reports:
          junit: junit.xml

    .install deps: &cachedef
    install deps: &cachedef
      stage: deps
      script:
        - npm install jest-junit
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
          - node_modules
        - npm install jest jest-junit
      <<: *cachedef

    test binarysearch:
      before_script:
        - npm install -g jest
      stage: test
      script:
        - jest --ci --testResultsProcessor=jest-junit binarysearch.test.js
        - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit binarysearch.test.js
      <<: [*artifactdef, *cachedef]

    test linearsearch:
      before_script:
        - npm install -g jest
      stage: test
      script:
        - jest --ci --testResultsProcessor=jest-junit linearsearch.test.js
        - node_modules/.bin/jest --ci --testResultsProcessor=jest-junit linearsearch.test.js
      <<: [*artifactdef, *cachedef]
      
    ```

Now, let’s create two merge requests that conflict:
+47 −61
Original line number Diff line number Diff line
@@ -21,20 +21,6 @@ For this task, we will be creating a web application to run in our review enviro

1. Select **Edit > Edit in pipeline editor**.

1. In your `install deps` job, add an install for express:

    ```yml
    install deps: &cachedef
      stage: deps
      script:
        - npm install jest-junit
        - npm install express
      cache:
        key: $CI_COMMIT_REF_SLUG
        paths:
          - node_modules
    ```

1. When we add express code into our `index.js` file, our tests will no longer be able to run against `index.js`, since running this will create a webserver that waits for connections. For now, we will comment our tests out. To do this, place a `.` character in front of each test job as shown below:

    ```yml