-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
170 lines (157 loc) · 5.74 KB
/
.gitlab-ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
image: registry.gitlab.com/kyb/autorsync/autorsync-builder-alpine
## alpine with bash git openssh-client git-rev-label perl
before_script:
- function DEBUG { >&2 "$@" || true; }
build:
stage: build
script:
- git checkout -B $CI_COMMIT_REF_NAME
- ./BUILD
- echo -e '\e[1;37m'$(./autorsync --version)'\e[0m'
artifacts:
paths:
- "autorsync"
test_without_build:
stage: build
image: ubuntu
variables:
AUTORSYNC: "$CI_PROJECT_DIR/autorsync.bash"
script: &test_script
#- git checkout -B $CI_COMMIT_REF_NAME
- apt-get update -qyy && apt install -qyy --no-install-recommends openssh-server openssh-client ca-certificates perl rsync fswatch psmisc tree
#- apk add --no-cache bash rsync fswatch openssh perl ca-certificates
#- /usr/bin/ssh-keygen -A ## For Alpine only, not for Ubuntu
- ssh-keygen -o -t rsa -b 4096 -f ~/.ssh/id_rsa -P ""
- cat ~/.ssh/id_rsa.pub >>~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys
- mkdir -p /run/sshd ## Ubuntu only, fix a bug
- /usr/sbin/sshd
- ssh-keyscan -t rsa localhost >>~/.ssh/known_hosts; chmod 600 ~/.ssh/known_hosts
#- ssh -vv -oPasswordAuthentication=no localhost echo test
- ./test-autorsync.bash
- ps -aux || true
test:
stage: test
dependencies:
- build
image: ubuntu
variables:
AUTORSYNC: "$CI_PROJECT_DIR/autorsync"
script: *test_script
npm_publish:
stage: deploy
image: alpine
dependencies:
- build
variables:
GIT_DEPTH: 1
#GIT_STRATEGY: none
only:
refs:
- master
variables:
- $NPM_TOKEN
script:
- apk add --no-cache --update npm bash
- DEBUG echo -e '\e[1;37m'$(./autorsync --version)'\e[0m'
- NPM_VERSION="$( ./autorsync --npm-version )"
- echo -e '\e[1;37m'"$NPM_VERSION"'\e[0m'
- sed -i 's#"version":\s*".*"#"version":"'$NPM_VERSION'"#' package.json
- DEBUG cat package.json
- echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >~/.npmrc ## https://blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules
- npm publish .
## CHECK
- npm install -g autorsync
- test $(autorsync --version) = $(./autorsync --version)
retry: 1
.deploy_common:
stage: deploy
only:
refs:
- branches
variables:
- $KI
warning_artifacts:
stage: deploy
image: alpine
dependencies: ## empty
variables:
GIT_STRATEGY: none
only:
refs:
- branches
variables:
- $KI == ""
script:
- |
echo -e "\e[1;33m
------------------------
Variable KI (ssh private key) is unset.
Skipping jobs store_artifacts, remove_stale_artifacts.
------------------------
\e[1;37m
To enable store_artifacts
1. generate SSH key pair https://www.ssh.com/ssh/keygen
2. go to Settings -> Repository -> Deploy Keys and add one with write access enabled
3. go to Settings -> CI/CD -> Variables and put private key to variable KI.
\e[0m"
## --- STORE ARTIFACTS TO A DEDICATED BRANCH ---
store_artifacts:
extends: .deploy_common
stage: deploy
dependencies:
- build
variables:
GIT_STRATEGY: none
GIT_USER_EMAIL: [email protected]
GIT_USER_NAME: autorsync-ci
script:
## --------- GET REV_LABEL ----------
- REV_LABEL=$(./autorsync --version)
- echo -e $'\e[1;37m' "$REV_LABEL" $'\e[0m'
## --------- URL --------------
- echo "$KI" >/tmp/ki && chmod 400 /tmp/ki
- export GIT_SSH_COMMAND="ssh -i/tmp/ki -oStrictHostKeyChecking=no"
- SSH_URL=$(echo "$CI_REPOSITORY_URL" | perl -pe 's#https://(.+?):(.+?)@(.+?)/(.+?).git#git@\3:\4.git#' )
## ---------- CLONE, ADD and COMMIT ---------------
- git clone "$SSH_URL" --depth 1 --single-branch -b artifacts/$CI_COMMIT_REF_NAME ./artifacts || { git init artifacts && git -C artifacts checkout --orphan artifacts/$CI_COMMIT_REF_NAME; }
#- for f in autorsync ;do F="$REV_LABEL-$f"; ln $f artifacts/$F || { echo >&2 "Forced ln $f"; ln -f $f artifacts/$F; } && git -C artifacts add $F; done
- ln -f autorsync artifacts/autorsync
- echo "$REV_LABEL" >artifacts/README.md
- cd artifacts
- git add .
- DEBUG git status
- git config --global user.email "$GIT_USER_EMAIL"
- git config --global user.name "$GIT_USER_NAME"
- git commit -m"$REV_LABEL"
## ------------ PUSH --------------
- git push "$SSH_URL" HEAD:artifacts/$CI_COMMIT_REF_NAME #ToDo use $CI_REPOSITORY_URL
## ----------- EPILOG -----------
- echo "See artifacts at "$'\e[1;37m'"$CI_PROJECT_URL/tree/artifacts/$CI_COMMIT_REF_NAME"$'\e[0m'
## ToDO use git ls-remote. No fetch required. Much faster on a big repos! GIT_STRATEGY: none
## git ls-remote --heads "$CI_REPOSITORY_URL" artifacts/\* | cut -d\t -f2 | sed 's#refs/heads/##'
remove_stale_artifacts:
extends: .deploy_common
stage: deploy
dependencies:
image: alpine
variables:
GIT_CHECKOUT: "false"
GIT_SUBMODULE_STRATEGY: none
script:
- apk add --no-cache git perl openssh-client
- mkdir /temp && mount -t tmpfs -o size=500m tmpfs /temp
## --- FIND STALED ARTIFACTS BRANCHES ---
- git branch -r --list origin/artifacts/\* | grep -v HEAD | sed -e s#origin/##g -Ee s#^\\s+##g >/temp/ARTIFACTS_BRANCHES
- DEBUG cat /temp/ARTIFACTS_BRANCHES
- git branch -r --list origin/\* | grep -v -e HEAD -e artifacts/| sed -e s#origin/##g -Ee s#^\\s+#artifacts/#g >/temp/BRANCHES
- DEBUG cat /temp/BRANCHES
- fgrep -vf /temp/BRANCHES /temp/ARTIFACTS_BRANCHES >/temp/STALE_ARTIFACTS_BRANCHES || return 0
- DEBUG cat /temp/STALE_ARTIFACTS_BRANCHES
- PUSH_SPEC=$(sed -e 's#\s+##g' -e 's#^#:#g' /temp/STALE_ARTIFACTS_BRANCHES)
- DEBUG echo "$PUSH_SPEC"
## --------- URL --------------
- echo "$KI" >/tmp/ki && chmod 400 /tmp/ki
- export GIT_SSH_COMMAND="ssh -i/tmp/ki -oStrictHostKeyChecking=no"
- SSH_URL=$(echo "$CI_REPOSITORY_URL" | perl -pe 's#https://(.+?):(.+?)@(.+?)/(.+?).git#git@\3:\4.git#' )
- git push "$SSH_URL" $PUSH_SPEC
retry: 1