我在gitlab上有一个exampleproject,我想通过脚本在.gitlab-ci.yml
中获取最后一个徽章的ID。我获得了所有徽章as a json的概述。有没有办法获取最后一个元素的“ id”?
目前,我正在为每个项目从json中手动设置一个自定义CI变量PYLINT_BADGE_ID
。在这种情况下,它是37777。如何通过命令行自动执行此操作?
我正在尝试解决以下问题:Pylint badge in gitlab。但是他们使用gitlab页面,anybadge,工件和自述文件来显示徽章(不在标准徽章区域中)。以下方式感觉更苗条:
This is the .gitlab-ci.yml I'm using
lint:
script:
- python -m pip install setuptools
- python -m pip install pylint pylint-exit
- pylint src/*.py | tee pylint.txt || pylint-exit $?
- score=$(sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' pylint.txt)
- echo "Pylint score was $score"
# To check your badge ID go to https://gitlab.com/api/v4/projects/43126475/badges
# and insert your $CI_PROJECT_ID. Must be a quite high number!
# Would be great to automate this!
- badge_url=https://img.shields.io/badge/lint%20score-$score-blue.svg
- >-
curl https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/badges/$PYLINT_BADGE_ID
-X PUT
-H "PRIVATE-TOKEN: $API_TOKEN"
-H "Content-Type: application/json"
-d '{"image_url": "'"$badge_url"'"}'
artifacts:
paths:
- pylint.txt
答案 0 :(得分:0)
经过数小时的正则表达式转义后,我将其添加到了@SpringBootApplication
public class So56226984Application {
public static void main(String[] args) {
SpringApplication.run(So56226984Application.class, args).close();
}
@Bean
public ApplicationRunner runner(JmsTemplate template, JmsListenerEndpointRegistry registry,
ConfigurableApplicationContext context) {
return args -> {
Scanner scanner = new Scanner(System.in);
String queue = scanner.nextLine();
Properties props = new Properties();
context.getEnvironment().getPropertySources().addLast(new PropertiesPropertySource("queues", props));
while (!"quit".equals(queue)) {
System.out.println("Adding " + queue);
props.put("queue.name", queue);
context.getBean("listener", Listener.class);
template.convertAndSend(queue, "qux sent to " + queue);
System.out.println("There are now " + registry.getListenerContainers().size() + " containers");
queue = scanner.nextLine();
}
scanner.close();
};
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Listener listener() {
return new Listener();
}
public static class Listener {
@JmsListener(destination = "${queue.name}")
public void listen(String in) {
System.out.println(in);
}
}
}
:
.gitlab-ci.yml
所以整个阶段看起来像这样:
- json_badge_info=$(curl -H "PRIVATE-TOKEN:$API_TOKEN" -X GET https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/badges)
- pylint_badge_id=$(expr match "$json_badge_info" '.*https[^"]*-blue\.svg\",\"id\":\([0-9]\+\),')
此解决方案取决于正则表达式中元素在json中查找lint:
stage: unittest-lint
script:
- python -m pip install setuptools pylint pylint-exit
- pylint src/*.py | tee pylint.txt || pylint-exit $?
- score=$(sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' pylint.txt)
- echo "Pylint score was $score"
# get the json with all badge urls via API and regex the id of the badge with 'blue.svg' in it
- json_badge_info=$(curl -H "PRIVATE-TOKEN:$API_TOKEN" -X GET https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/badges)
- pylint_badge_id=$(expr match "$json_badge_info" '.*https[^"]*-blue\.svg\",\"id\":\([0-9]\+\),')
- echo $pylint_badge_id
- badge_url=https://img.shields.io/badge/lint%20score-$score-blue.svg
- >-
curl https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/badges/$pylint_badge_id
-X PUT
-H "PRIVATE-TOKEN: $API_TOKEN"
-H "Content-Type: application/json"
-d '{"image_url": "'"$badge_url"'"}'
artifacts:
paths:
- pylint.txt
的顺序,该顺序必须在徽章ID之前。