Only
Covered Gitlab features : only-except
Quand même, ils sont onze. J’ai calculé sur les treize dernières années, dans les deux heures qui précèdent le coucher du soleil, vous en êtes à une moyenne de 8,422.
Kaamelott S02E18
Run job only on… What you want !
We have a fancy cool application and a lots of contributors want to add features to it. To ensure our code is following our style guide, we would like to run a specific task only when a merge request is done 👍.
📝 Instructions
➡️ Modify the .gitlab-ci.yml
to add a new job for the linting
stages:
- install
- check
- tests
...
lint:
stage: check
script:
- ./mvnw antrun:run@ktlint
only:
- merge_requests
➡️ Modify the pom.xml
and add the following lines in it, inside the <plugins>
block
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>ktlint</id>
<phase>verify</phase>
<configuration>
<target name="ktlint">
<java taskname="ktlint" dir="${basedir}" fork="true" failonerror="true" classname="com.github.shyiko.ktlint.Main" classpathref="maven.plugin.classpath">
<arg value="src/**/*.kt"/>
</java>
</target>
</configuration>
<goals><goal>run</goal></goals>
</execution>
<execution>
<id>ktlint-format</id>
<configuration>
<target name="ktlint">
<java taskname="ktlint" dir="${basedir}" fork="true" failonerror="true" classname="com.github.shyiko.ktlint.Main" classpathref="maven.plugin.classpath">
<arg value="-F"/>
<arg value="src/**/*.kt"/>
</java>
</target>
</configuration>
<goals><goal>run</goal></goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.github.shyiko</groupId>
<artifactId>ktlint</artifactId>
<version>0.29.0</version>
</dependency>
</dependencies>
</plugin>
➡️ Use git to commit and push your modification :
git add .
git commit -m "chore(ci): trigger linting only on merge requests"
git push
➡️ Follow your job execution and note this new job is not present 😉
➡️ Create a test branch using git checkout -b test-lint
and push it with git push
➡️ Create a merge request in your project with the dedicated button inside your project, under "merge request" section
➡️ Follow the pipeline created from your merge request, which includes the specific job defined previously
Your pipeline should now contains only one job, you have changed your pipeline structure for all merge requests and only for that |
🚨 Solution
Project : https://gitlab.com/gitlab-workshop/solutions/blob/461d9553ab046c1f0503df0f46f564cebc9c1e69/ // REF(only-except:only)
Pipeline : https://gitlab.com/gitlab-workshop/solutions/pipelines/275964963 // REF(only-except:only)
Merge request : https://gitlab.com/gitlab-workshop/solutions/merge_requests/9