hello-world-template issueshttps://code.axians.com/ci-cd-discovery/templates/hello-world-template/-/issues2020-12-01T17:42:33+01:00https://code.axians.com/ci-cd-discovery/templates/hello-world-template/-/issues/909-includes2020-12-01T17:42:33+01:00Philippe Charrière09-includesYou can split your jobs into several yaml files and include these files in the main file: `.gitlab-ci.yml`
### TODO
- [ ] create a merge request from this issue
- [ ] open the WebIDE
- [ ] create a new file `hello.yml` with this conten...You can split your jobs into several yaml files and include these files in the main file: `.gitlab-ci.yml`
### TODO
- [ ] create a merge request from this issue
- [ ] open the WebIDE
- [ ] create a new file `hello.yml` with this content:
```yaml
.hello:
image: python:slim
tags: [docker]
script:
- python -c "print('hello')"
```
- [ ] update the `.gitlab-ci.yml` file
- [ ] include the `hello.yml` file:
```
include: '/hello.yml'
```
> put it below the `stages` section
- [ ] add a new stage: `included`
- [ ] add a new job: `say_hello` belonging to `included` with this content:
```yaml
say_hello:
stage: included
extends: .hello
```
- [ ] commit your changes, it will trigger the pipeline
- [ ] observe the pipeline
- [ ] observe the output of the pipeline
- [ ] **do the same thing for one of the other jobs of the pipeline**
- [ ] commit your changes (again)
- [ ] observe the pipeline
- [ ] observe the output of the pipeline
- [ ] merge on masterprogresshttps://code.axians.com/ci-cd-discovery/templates/hello-world-template/-/issues/808-rules2020-12-02T07:24:28+01:00Philippe Charrière08-rulesYou may probably control if a job is executed when pushing on the `master` branch, or when updating a **merge request**.
For this, you can use **rules**.
### TODO
- [ ] create a merge request from this issue
- [ ] open the WebIDE
- [ ]...You may probably control if a job is executed when pushing on the `master` branch, or when updating a **merge request**.
For this, you can use **rules**.
### TODO
- [ ] create a merge request from this issue
- [ ] open the WebIDE
- [ ] update the `.gitlab-ci.yml` file
- [ ] add a new stage: `all_under_control`
- [ ] add a new job: `when_master` belonging to `all_under_control`
```yaml
when_master:
stage: all_under_control
tags: [docker]
rules:
- if: $CI_COMMIT_BRANCH == "master" # 1️⃣
script: |
echo "We are on the master branch"
```
> 1️⃣ we test if we are pushing on the `master` branch, and if yes, the job will be triggered
- [ ] add a new job: `when_merge_request` belonging to `all_under_control`
```yaml
when_merge_request:
stage: all_under_control
tags: [docker]
rules:
- if: $CI_MERGE_REQUEST_IID # 1️⃣
script: |
echo "We are on the branch $CI_COMMIT_REF_NAME"
echo "IID: $CI_MERGE_REQUEST_IID"
```
> 1️⃣ we test if we are doing a merge request, and if yes, the job will be triggered
- [ ] commit your changes, it will trigger the pipeline
- [ ] observe the pipeline (how many jobs in the `all_under_control` stage ?)
- [ ] observe the output of the pipeline
- [ ] merge on master (the pipe will be triggered again)
- [ ] observe the pipeline (and output) **again**progresshttps://code.axians.com/ci-cd-discovery/templates/hello-world-template/-/issues/707-generate-artifacts2020-12-01T16:53:02+01:00Philippe Charrière07-generate-artifactsTo generate a downloadable artifact from the CI script use this in your job:
```yaml
artifacts:
paths:
- report.html
```
### TODO
Write a Python script that will generate an HTML file (`report.html`) and add it as an artif...To generate a downloadable artifact from the CI script use this in your job:
```yaml
artifacts:
paths:
- report.html
```
### TODO
Write a Python script that will generate an HTML file (`report.html`) and add it as an artifact.
- [ ] create a merge request from this issue
- [ ] open the WebIDE
- [ ] update the `.gitlab-ci.yml` file
- [ ] add a new job: `artefact_generator` belonging to `polyglot`
- [ ] use the `python:slim` image
- [ ] add the `artifacts` section to the job
- [ ] `HELLO_MESSAGE` value is `Hello from Python`
- [ ] `SOURCE_CODE` contains your Python source code, the content of the HTML file will be:
```html
<h1>
{message}
</h1>
```
> the value of `{message}` is the value of `HELLO_MESSAGE`
- [ ] add a script that runs python to execute the content of `SOURCE_CODE`
- [ ] commit your changes, it will trigger the pipeline
- [ ] observe the pipeline
- [ ] observe the output of the pipeline
| Output |
| ------ |
| ![image](/uploads/87998d0fb24583bc0ed9d96cde8ffa8f/image.png) |
- Click on the <kbd>Browse</kbd> button to display your report
- [ ] merge on master (the pipe will be triggered again)
### Help
- Create a file to append content with Python: `f = open(file_name, "a")`
- Write content to the file: `f.write('hello world')`
- Close the file: `f.close()`progresshttps://code.axians.com/ci-cd-discovery/templates/hello-world-template/-/issues/606-use-docker-images2020-12-01T16:52:59+01:00Philippe Charrière06-use-docker-imagesIt's possible to use a specific Docker image for the whole pipeline or for a specific job.
For example, I would like to display a message from my CI script using **NodeJS**, I will write something like that:
```yaml
hello_javascript:
...It's possible to use a specific Docker image for the whole pipeline or for a specific job.
For example, I would like to display a message from my CI script using **NodeJS**, I will write something like that:
```yaml
hello_javascript:
image: node:slim # 1️⃣
stage: polyglot
tags: [docker]
variables:
HELLO_MESSAGE: "Hello from JavaScript"
SOURCE_CODE: | # 2️⃣
message = process.env.HELLO_MESSAGE
console.log(message)
script:
- node -e "$SOURCE_CODE" # 3️⃣ 4️⃣
```
> - 1️⃣ use the keyword `image` to use a docker image
> - 2️⃣ you can create a variable with a multiline string with the pipe notation
> - 3️⃣ you can evaluate a JavaScript code in a string with NodeJS with the flag `e`: node -e "console.log('👋 hello')"
> - 4️⃣ don't forget to surround the variable with `"` to avoid interpolation issues
### TODO:
Do the same thing as above, but with **Python**
- [ ] create a merge request from this issue
- [ ] open the WebIDE
- [ ] update the `.gitlab-ci.yml` file
- [ ] add a new stage: `polyglot`
- [ ] add a new job: `hello_python` belonging to `polyglot`
- [ ] use the `python:slim` image
- [ ] `HELLO_MESSAGE` value is `Hello from Python`
- [ ] `SOURCE_CODE` contains your Python source code
- [ ] add a script that runs python to execute the content of `SOURCE_CODE` (and then to display the value of `HELLO_MESSAGE`)
- [ ] commit your changes, it will trigger the pipeline
- [ ] observe the pipeline
- [ ] observe the output of the pipeline
- [ ] merge on master (the pipe will be triggered again)
### Help
- To evaluate a string with Python code from the command line, use the flag `c`: `python -c "print('hello')"`
- To get the value of an environment variable, use this method: `os.environ.get(variable_name)`
- > you need to import `os` package beforeprogresshttps://code.axians.com/ci-cd-discovery/templates/hello-world-template/-/issues/505-display-project-information2020-12-01T16:52:55+01:00Philippe Charrière05-display-project-information
### TODO
#### Part 1: define your own variables
- [ ] Define 2 CI variables in the settings of the project (see **§ Help**, *Define CI variables in the CI/CD Settings* section:
- `FIRST_NAME` (set it with your first name)
- `LAST_...
### TODO
#### Part 1: define your own variables
- [ ] Define 2 CI variables in the settings of the project (see **§ Help**, *Define CI variables in the CI/CD Settings* section:
- `FIRST_NAME` (set it with your first name)
- `LAST_NAME` (set it with your last name)
- [ ] create a merge request from this issue
- [ ] open the WebIDE
- [ ] update the `.gitlab-ci.yml` file
- [ ] add a new stage: `display_variables`
- [ ] add a new job: `ci_variables` belonging to `display_variables`
- [ ] add a script to this job to display your first name and your last name
- [ ] commit your changes, it will trigger the pipeline
- [ ] observe the pipeline
- [ ] observe the output of the pipeline
- [ ] **don't merge** on master
#### Part 2: use GitLab Predefined Variables
- [ ] update the `.gitlab-ci.yml` file
- [ ] add a new job: `predefined_variables` belonging to `display_variables`
- [ ] add a script to this job to display 2 CI variables:
- The title of the commit - the full first line of the message: `CI_COMMIT_REF_NAME`
- The branch or tag name for which project is built: `CI_COMMIT_TITLE`
- [ ] commit your changes, it will trigger the pipeline
- [ ] observe the pipeline
- [ ] observe the output of the pipeline
- [ ] merge on master (the pipe will be triggered again)
### Help
#### Define CI variables in the CI/CD Settings
- Go to the left panel menu and choose **Settings**/**CI/CD**
- Expand the **Variables** section (click on the <kbd>Expand</kbd> button):
| CI/CD Settings |
| ------ |
| ![image](/uploads/70e555b7d49d4993f75c452487fea1f5/image.png) |
- Click on the <kbd>Add Variable</kbd> button
- Add a `MESSAGE` variable whit this content: `Hello people!`
| CI/CD Settings / Variables section |
| ------ |
| ![image](/uploads/21f3d67a3feb37f3f1f89c92c4e6bbcd/image.png) |
- Uncheck the checkbox `Protect variable`
- Click on the <kbd>Add variable</kbd> button
| CI/CD Settings / Add a variable |
| ------ |
| ![image](/uploads/aac2eeed9fb5b64d46ace4c540ef0c80/image.png) |
🎉 **Now, you can use the `MESSAGE` variable in the CI script of your project**: `echo "this is a message: $MESSAGE"`
#### Predefined variables
Refs:
- Predefined variables https://docs.gitlab.com/ee/ci/variables/predefined_variables.htmlprogresshttps://code.axians.com/ci-cd-discovery/templates/hello-world-template/-/issues/404-calc-functions2020-12-01T16:52:50+01:00Philippe Charrière04-calc-functions
### TODO
#### Part 1: make some calculations
- [ ] create a merge request from this issue
- [ ] open the WebIDE
- [ ] update the `.gitlab-ci.yml` file
- [ ] Deactivate the existing jobs by prefixing each job name by a `.`: `hello` be...
### TODO
#### Part 1: make some calculations
- [ ] create a merge request from this issue
- [ ] open the WebIDE
- [ ] update the `.gitlab-ci.yml` file
- [ ] Deactivate the existing jobs by prefixing each job name by a `.`: `hello` becomes `.hello`
- [ ] Create a new stage `calculator`
- [ ] Create a new job `addition` belonging to `calculator` with:
- 2 variables `x` and `y`
- a script that adds `x` and `y` and displays the result
- [ ] commit your changes, it will trigger the pipeline
- [ ] observe the pipeline
- [ ] observe the output of the pipeline
- [ ] **don't merge** on master
#### Part 2: create a bash function
- [ ] update the `.gitlab-ci.yml` file
- [ ] write a `bash` function to use it in your CI script (🖐️ see the **§ Help**, third point):
- create a YAML anchor and add to it an `add` function that calculates the addition of 2 parameters `$1` and `$2`
- use it in your `addition` job, like this: `add $x $y`
- [ ] commit your changes, it will trigger the pipeline
- [ ] observe the pipeline
- [ ] observe the output of the pipeline
- [ ] merge on master (the pipe will be triggered again)
### Help
- To perform an arithmetic expression with `bash`, use this notation: `$((expression))`
- To write a script in a multiline string (without the `-`at every line) you can use the `|`(pipe) notation:
```bash
my-little-job:
stage: my-stage
tags: [docker]
script: |
echo "hello 1"
echo "hello 2"
echo "hello 3"
```
- Use **YAML anchors** to define reusable functions:
```bash
.tools: &tools |
function hello() {
echo "Hello $1" # $1 is the first argument of the function
}
my-little-job:
stage: my-stage
tags: [docker]
before_script:
- *tools
script: |
hello Bob # output: "Hello Bob"
```progresshttps://code.axians.com/ci-cd-discovery/templates/hello-world-template/-/issues/303-before-after2020-12-01T16:52:46+01:00Philippe Charrière03-before-afterYou can run a script **before** and **after** every `script` section of each job with these global keywords:
- `before_script`
- `after_script`
Ref: https://docs.gitlab.com/ee/ci/yaml/#global-keywords
### TODO
- [ ] create a merge req...You can run a script **before** and **after** every `script` section of each job with these global keywords:
- `before_script`
- `after_script`
Ref: https://docs.gitlab.com/ee/ci/yaml/#global-keywords
### TODO
- [ ] create a merge request from this issue
- [ ] open the WebIDE
- [ ] update the `.gitlab-ci.yml` file
- [ ] create a global `before_script` that prints `"Hello, I'm the before script"`
- [ ] create a global `after_script` that prints `"Bye, I'm the after script"`
- [ ] override `before_script` and `after_script` in the `hey` job:
- `before_script`: print `"Before hey job"`
- `after_script`: print `"After hey job"`
- [ ] commit your changes, it will trigger the pipeline
- [ ] observe the pipeline
- [ ] observe the output of the pipeline
- [ ] merge on master (the pipe will be triggered again)
### Help
```yaml
# syntax
before_script:
- foo
```
### Remarks
You can use `before_script` and `after_script` in a job to override the global commands for the current job.progresshttps://code.axians.com/ci-cd-discovery/templates/hello-world-template/-/issues/202-use-variables2020-12-01T16:52:39+01:00Philippe Charrière02-use-variablesYou can use environment variables inside the `.gitlab-ci.yml` file for the whole pipeline or specifically for a job.
### TODO
- [ ] create a merge request from this issue
- [ ] open the WebIDE
- [ ] update the `.gitlab-ci.yml` file
- [...You can use environment variables inside the `.gitlab-ci.yml` file for the whole pipeline or specifically for a job.
### TODO
- [ ] create a merge request from this issue
- [ ] open the WebIDE
- [ ] update the `.gitlab-ci.yml` file
- [ ] The 2 messages `"hello world!"` and `"hey what's up?"` should be defined in variables (inside each jobs)
- [ ] Add a second stage named `christmas_songs`
- [ ] Add a global variable (visible for the whole pipeline) named `message` with this value `"Happy Christmas!"`
- [ ] Add 2 new jobs belonging to the `christmas_songs` stage:
- first job `first_song` will have a variable named `song` with the value `"Jingle Bells"`
- second job `second_song` will have a variable named `song` with the value `"Let It Snow!"`
- each job will print `Happy Christmas!` followed by the name of its song
- [ ] commit your changes, it will trigger the pipeline
- [ ] observe the pipeline
- [ ] observe the output of the pipeline
- [ ] merge on master (the pipe will be triggered again)
### Help
The script below will display "I'm walking" and "I'm running", but in 2 different stages
```yaml
stages:
- first_steps
- a_little_further
variables:
who: "I'm"
walking:
stage: first_steps
tags: [docker]
variables:
action: "walking"
script:
- echo "${who} ${action}"
running:
stage: a_little_further
tags: [docker]
variables:
action: "running"
script:
- echo "${who} ${action}"
```progresshttps://code.axians.com/ci-cd-discovery/templates/hello-world-template/-/issues/101-say-hello2020-12-01T17:51:41+01:00Philippe Charrière01-say-hello🖐️ This exercise is performed by the speakers and replayed by the learners, raise the hand if you have any question or if you need help
### TODO
- [ ] create a merge request from this issue
- [ ] open the WebIDE
- [ ] create a `.gitlab...🖐️ This exercise is performed by the speakers and replayed by the learners, raise the hand if you have any question or if you need help
### TODO
- [ ] create a merge request from this issue
- [ ] open the WebIDE
- [ ] create a `.gitlab-ci.yml` file
- [ ] add one stage: `greetings` to the `.gitlab-ci.yml` file
- [ ] add 2 jobs (belonging to `greetings`) to the `.gitlab-ci.yml` file
- `hello`, and this job will display "hello world!"
- `hey`, and this job will display "hey what's up?"
- [ ] commit your changes, it will trigger the pipeline
- [ ] observe the pipeline
- [ ] observe the output of the pipeline
- [ ] merge on master (the pipe will be triggered again)
| 🖐️🖐️🖐️ Information |
| ----------------- |
| We're going to use an **Axians Docker runner** tagged `docker`; so every job has to contain this: `tags: [docker]`|
### Help
This pipeline will display **"I'm walking"**
```yaml
stages:
- first_steps
walking:
stage: first_steps
tags: [docker]
script:
- echo "I'm walking"
```progress