.gitlab-ci.yml 6.22 KB
Newer Older
Ludovic Cottret's avatar
Ludovic Cottret committed
1
2
3
4
5


stages:
  - build
  - test
6
  - package
Ludovic Cottret's avatar
Ludovic Cottret committed
7
  - deploy
Ludovic Cottret's avatar
Docker    
Ludovic Cottret committed
8
9
10
11
12
13
14
  - build-containers

.template_docker:
  stage: build-containers
  image: docker:latest
  services:
    - docker:dind
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  script:
    - if [ "$CI_COMMIT_BRANCH" = "master" ]; then docker build -t "$DOCKER_IMAGE:latest" .; fi
    - if [ "$CI_COMMIT_BRANCH" = "master" ]; then docker push "$DOCKER_IMAGE:latest"; fi
    - if [ "$CI_COMMIT_BRANCH" = "develop" -o "$CI_COMMIT_BRANCH" = "master" ]; then docker build -t "$DOCKER_IMAGE:$VERSION" .; fi
    # version in lower case
    - if [ "$CI_COMMIT_BRANCH" = "develop" -o "$CI_COMMIT_BRANCH" = "master" -a "$VERSION" != "$LOWER_CASE_VERSION" ]; then docker build -t "$DOCKER_IMAGE:$LOWER_CASE_VERSION" .; fi
    - if [ "$CI_COMMIT_BRANCH" = "develop" -o "$CI_COMMIT_BRANCH" = "master" -a "$VERSION" != "$LOWER_CASE_VERSION" ]; then docker push "$DOCKER_IMAGE:$LOWER_CASE_VERSION"; fi
    - docker build -t "$DOCKER_IMAGE:$CI_COMMIT_BRANCH" .
    - docker push "$DOCKER_IMAGE:$CI_COMMIT_BRANCH"
    # branch in lower case
    - docker build -t "$DOCKER_IMAGE:$LOWER_CASE_COMMIT_BRANCH" .
    - docker push "$DOCKER_IMAGE:$LOWER_CASE_COMMIT_BRANCH"
  rules:
    - if: $CI_COMMIT_BRANCH == "master"
      when: always
    - if: $CI_COMMIT_BRANCH == "develop"
      when: always
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
      when: manual
  needs:
    - build
    - package
Ludovic Cottret's avatar
Ludovic Cottret committed
39

Ludovic Cottret's avatar
Ludovic Cottret committed
40
variables:
lcottret's avatar
lcottret committed
41
  MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
42
  MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"
Ludovic Cottret's avatar
Ludovic Cottret committed
43

Ludovic Cottret's avatar
Ludovic Cottret committed
44
45
46
47
48
cache:
  paths:
    - .m2/repository/

build:
Ludovic Cottret's avatar
Ludovic Cottret committed
49
  image: maven:3.6-adoptopenjdk-14
Ludovic Cottret's avatar
Ludovic Cottret committed
50
51
  stage: build
  script:
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    - mvn $MAVEN_OPTS compile
    - TRUEVERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
    - VERSION=$TRUEVERSION
    - if [ "$CI_COMMIT_BRANCH" = "master" ]; then VERSION=${VERSION%"-SNAPSHOT"}; fi
    - LOWER_CASE_VERSION=${VERSION,,}
    - LOWER_CASE_COMMIT_BRANCH=${CI_COMMIT_BRANCH,,}
    - echo "VERSION=$VERSION" >> build.env
    - echo "TRUEVERSION=$TRUEVERSION" >> build.env
    - echo "LOWER_CASE_VERSION=$LOWER_CASE_VERSION" >> build.env
    - echo "LOWER_CASE_COMMIT_BRANCH=$LOWER_CASE_COMMIT_BRANCH" >> build.env
  artifacts:
    expire_in: 60 min
    paths:
      - target/
      - "*/target"
    reports:
      dotenv: build.env
Ludovic Cottret's avatar
Ludovic Cottret committed
69
70

test:
Ludovic Cottret's avatar
Ludovic Cottret committed
71
  image: maven:3.6-adoptopenjdk-14
Ludovic Cottret's avatar
Ludovic Cottret committed
72
73
  stage: test
  script:
74
75
    - find . -name "*.class" -exec touch {} \+
    - mvn $MAVEN_OPTS clean test
Ludovic Cottret's avatar
Ludovic Cottret committed
76
77
    - cat coverage/target/site/jacoco-aggregate/index.html | grep -o '<tfoot>.*</tfoot>'

78
79
80
81
82
83
84
85
86
87
88
89
90
package:
  image: maven:3.6-adoptopenjdk-14
  stage: package
  script:
    - mvn $MAVEN_OPTS clean install -DskipTests=true
    - cd met4j-toolbox
    - mvn $MAVEN_OPTS package -DskipTests=true
    - cd ..
  artifacts:
    expire_in: 60 min
    paths:
      - "met4j-toolbox/target/met4j-toolbox-$TRUEVERSION.jar"

Ludovic Cottret's avatar
Ludovic Cottret committed
91
deploySnapshot:
Ludovic Cottret's avatar
Ludovic Cottret committed
92
  image: maven:3.6-adoptopenjdk-14
Ludovic Cottret's avatar
Ludovic Cottret committed
93
94
  stage: deploy
  script:
lcottret's avatar
lcottret committed
95
96
97
    - mvn $MAVEN_CLI_OPTS deploy -Dmaven.test.skip=true
  only:
    - develop
Ludovic Cottret's avatar
Ludovic Cottret committed
98

lcottret's avatar
lcottret committed
99
deployCentral:
Ludovic Cottret's avatar
Ludovic Cottret committed
100
  image: maven:3.6-adoptopenjdk-14
lcottret's avatar
lcottret committed
101
  stage: deploy
lcottret's avatar
lcottret committed
102
  before_script:
lcottret's avatar
lcottret committed
103
    - 'which ssh-agent || ( apt-get update -qy && apt-get install openssh-client -qqy )'
lcottret's avatar
lcottret committed
104
    - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
lcottret's avatar
lcottret committed
105
    - eval $(ssh-agent -s)
lcottret's avatar
lcottret committed
106
    - echo "${SSH_PRIVATE_KEY}" | tr -d '\r' | ssh-add -
lcottret's avatar
lcottret committed
107
108
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
lcottret's avatar
lcottret committed
109
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
lcottret's avatar
lcottret committed
110
    - 'which gpg || ( apt-get update -qy && apt-get install gnupg -qqy )'
lcottret's avatar
lcottret committed
111
    - export GPG_TTY=$(tty)
lcottret's avatar
lcottret committed
112
    - gpg -v --batch --import <(echo "${GPG_PRIVATE_KEY}")
lcottret's avatar
lcottret committed
113
  script:
lcottret's avatar
lcottret committed
114
115
    - git config --global user.email "$env:GITLAB_USER_EMAIL"
    - git config --global user.name "$env:GITLAB_USER_NAME"
lcottret's avatar
lcottret committed
116
    - git checkout -B "$CI_BUILD_REF_NAME"
117
118
    - mvn $MAVEN_CLI_OPTS release:prepare -DautoVersionSubmodules=true -Dmaven.test.skip=true -DscmCommentPrefix="[skip ci]" -DupdateWorkingCopyVersions=false -DpushChanges=false
    - mvn $MAVEN_CLI_OPTS release:perform -Darguments=-Dgpg.passphrase=${GPG_PASSPHRASE} -DlocalCheckout=true
lcottret's avatar
lcottret committed
119
120
  only:
    - master
Ludovic Cottret's avatar
Ludovic Cottret committed
121
122

buildSingularity:
Ludovic Cottret's avatar
Docker    
Ludovic Cottret committed
123
  stage: build-containers
Ludovic Cottret's avatar
Ludovic Cottret committed
124
125
126
127
128
  image:
    name: quay.io/singularity/singularity:v3.4.0
    entrypoint: [ "" ]
  script:
    - singularity build met4j-toolbox.sif met4j.singularity
129
130
131
132
133
134
135
    - if [ "$CI_COMMIT_BRANCH" = "master" ]; then singularity push --docker-username "${CI_REGISTRY_USER}" --docker-password "${CI_REGISTRY_PASSWORD}" met4j-toolbox.sif oras://"$CI_REGISTRY_IMAGE"/met4j-singularity:"latest"; fi
    - if [ "$CI_COMMIT_BRANCH" = "develop" -o "$CI_COMMIT_BRANCH" = "master" ]; then singularity push --docker-username "${CI_REGISTRY_USER}" --docker-password "${CI_REGISTRY_PASSWORD}" met4j-toolbox.sif oras://"$CI_REGISTRY_IMAGE"/met4j-singularity:"$VERSION"; fi
    # version in lower case
    - if [ "$CI_COMMIT_BRANCH" = "develop" -o "$CI_COMMIT_BRANCH" = "master" -a "$VERSION" != "$LOWER_CASE_VERSION"]; then singularity push --docker-username "${CI_REGISTRY_USER}" --docker-password "${CI_REGISTRY_PASSWORD}" met4j-toolbox.sif oras://"$CI_REGISTRY_IMAGE"/met4j-singularity:"$LOWER_CASE_VERSION"; fi
    - singularity push --docker-username "${CI_REGISTRY_USER}" --docker-password "${CI_REGISTRY_PASSWORD}" met4j-toolbox.sif oras://"$CI_REGISTRY_IMAGE"/met4j-singularity:"$CI_COMMIT_BRANCH"
    # branch in lower case
    - singularity push --docker-username "${CI_REGISTRY_USER}" --docker-password "${CI_REGISTRY_PASSWORD}" met4j-toolbox.sif oras://"$CI_REGISTRY_IMAGE"/met4j-singularity:"$LOWER_CASE_COMMIT_BRANCH"
Ludovic Cottret's avatar
Docker    
Ludovic Cottret committed
136
  rules:
137
138
139
140
141
142
    - if: $CI_COMMIT_BRANCH == "master"
      when: always
    - if: $CI_COMMIT_BRANCH == "develop"
      when: always
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
Ludovic Cottret's avatar
Docker    
Ludovic Cottret committed
143
144
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
      when: manual
145
146
147
  needs:
    - build
    - package
Ludovic Cottret's avatar
Docker    
Ludovic Cottret committed
148
149
150
151
152

buildDockerProdGitlab:
  extends: .template_docker
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
153
154
    - DOCKER_IMAGE="$CI_REGISTRY/metexplore/met4j/met4j-docker"

Ludovic Cottret's avatar
Docker    
Ludovic Cottret committed
155
156
157
158
159

buildDockerProdDockerhub:
  extends: .template_docker
  before_script:
    - docker login -u "$DOCKERHUB_USER" -p "$DOCKERHUB_PASSWORD" $DOCKERHUB_REGISTRY
160
    - DOCKER_IMAGE=$DOCKERHUB_IMAGE