Skip to content

Commit

Permalink
Add reusable gitlab workflows (#42)
Browse files Browse the repository at this point in the history
* add initial gitlab-ci.yml

* update sync rules

* add shellcheck style template

* sync to master

* switch image to alpine

* Update sync to be more generic
- control repo with vars

* Move sync job to separate file for downstream trigger

* fix local

* move rules from upstream to downstream

* move rules from upstream to downstream

* revert rules from child to parent

* update defaults

* update defaults

* Move vars to downstream

* streamline rules

* add trigger defaults

* add trigger defaults

* revert default trigger

* re-enable manual sync
- add allow_failure so it doesn't block the pipeline

* streamline vars

* try to set default rules

* revert rules

* revert rules

* remove stage

* remove manual trigger

* streamline test

* move variables

* Shellcheck doesn't need anything so it can run first

* curl checkstyle template

* add curl

* add insecure curl

* debug checkstyle xslt

* use wget instead of curl

* checkstyle template in yml

* update method to dump file

* test

* fix filename

* remove debug

* change test to lint stage

* remove checkstyle2junit.xslt

* Remove wget

* rename default rules
- use corect branch name
- use correct image tag name

* update rule names

* add push_only rule

* move files to .gitlab

* update paths

* remove unused vars
  • Loading branch information
velomatt authored Mar 4, 2024
1 parent 65d3104 commit 5335e23
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
include:
- local: .gitlab/defaults.yml
- local: .gitlab/shellcheck.yml
- local: .gitlab/sync-github.yml

stages:
- lint
- sync

sync-github:
variables:
GITHUB_REPO: DSS
32 changes: 32 additions & 0 deletions .gitlab/defaults.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.default_rules:
merge_only:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
variables:
BRANCH_NAME: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
SONAR_BRANCH: ''
- if: $CI_PIPELINE_SOURCE == "schedule"
when: never
- if: $CI_PIPELINE_SOURCE == "web"
merge_and_push:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
variables:
BRANCH_NAME: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
SONAR_BRANCH: ''
- if: $CI_COMMIT_BRANCH == "master" && $CI_PIPELINE_SOURCE == "push"
- if: $CI_COMMIT_BRANCH =~ /^stable\/.*/ && $CI_PIPELINE_SOURCE == "push"
- if: $CI_PIPELINE_SOURCE == "schedule"
when: never
- if: $CI_PIPELINE_SOURCE == "web"
push_only:
- if: $CI_COMMIT_BRANCH == "master" && $CI_PIPELINE_SOURCE == "push"
- if: $CI_COMMIT_BRANCH =~ /^stable\/.*/ && $CI_PIPELINE_SOURCE == "push"
scheduled_sync:
- if: $CI_PIPELINE_SOURCE == "schedule" && $SCHEDULE_TYPE == "sync"

variables:
BRANCH_NAME: $CI_COMMIT_BRANCH
SONAR_BRANCH: -Dsonar.branch.name=$CI_COMMIT_BRANCH

default:
image:
name: dss-build_$BRANCH_NAME
68 changes: 68 additions & 0 deletions .gitlab/shellcheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
shellcheck:
stage: lint
image: koalaman/shellcheck-alpine:$SHELLCHECK_VER
before_script:
- apk update
- apk add git xmlstarlet
- shellcheck --version
- |
cat > checkstyle2junit.xslt <<EOL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" method="xml"></xsl:output>
<xsl:template match="/">
<testsuite>
<xsl:attribute name="tests">
<xsl:value-of select="count(.//file)" />
</xsl:attribute>
<xsl:attribute name="failures">
<xsl:value-of select="count(.//error)" />
</xsl:attribute>
<xsl:for-each select="//checkstyle">
<xsl:apply-templates />
</xsl:for-each>
</testsuite>
</xsl:template>
<xsl:template match="file">
<testcase>
<xsl:attribute name="classname">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:apply-templates select="node()" />
</testcase>
</xsl:template>
<xsl:template match="error">
<failure>
<xsl:attribute name="type">
<xsl:value-of select="@source" />
</xsl:attribute>
<xsl:text>Line </xsl:text>
<xsl:value-of select="@line" />
<xsl:text>: </xsl:text>
<xsl:value-of select="@message" />
<xsl:text> See https://www.shellcheck.net/wiki/</xsl:text>
<xsl:value-of select="substring(@source, '12')" />
</failure>
</xsl:template>
</xsl:stylesheet>
EOL
script:
- git ls-files --exclude='*.sh' --ignored -c -z |
xargs -t --no-run-if-empty -0 shellcheck --format=checkstyle |
xmlstarlet tr checkstyle2junit.xslt >
shellcheck.xml
artifacts:
when: always
reports:
junit: shellcheck.xml
variables:
SHELLCHECK_VER: v0.9.0
needs: []
rules:
- !reference [.default_rules, merge_and_push]
21 changes: 21 additions & 0 deletions .gitlab/sync-github.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sync-github:
stage: sync
image:
name: alpine:3.19.1
before_script:
- apk add git
- git config --global http.sslVerify false
script:
- mkdir $GITHUB_REPO && cd $GITHUB_REPO
- git clone --branch $GITHUB_BRANCH https://github.com/$GITHUB_PROJECT/$GITHUB_REPO .
- git remote rename origin old-origin
- git remote add origin https://$CI_USERNAME:$CI_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git
- git push origin $GITHUB_BRANCH:$GITLAB_BRANCH
- git push -u origin --tags
variables:
GIT_STRATEGY: none
GITHUB_PROJECT: OpenMPDK
GITHUB_BRANCH: master
GITLAB_BRANCH: master
rules:
- !reference [.default_rules, scheduled_sync]

0 comments on commit 5335e23

Please sign in to comment.