generated from Griefed/docker-template-repo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
.gitlab-ci.yml
185 lines (157 loc) · 6.1 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
stages:
- test
- release
- build
services:
- name: docker.griefed.dev/griefed/gitlab-ci-cd:2.2.12
alias: docker
image: docker.griefed.dev/griefed/gitlab-ci-cd:2.2.12
workflow:
rules:
- if: '$CI_MERGE_REQUEST_EVENT_TYPE == "detached"'
when: never
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: never
- when: always
test docker:
stage: test
variables:
project_name: $CI_PROJECT_NAME
SEMANTIC_RELEASE_PACKAGE: $CI_PROJECT_NAME
before_script:
- docker login -u "$DOCKERHUB_USER" -p "$DOCKERHUB_TOKEN" docker.io
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
- docker login -u "$DOCKERHUB_USER" -p "$GITHUB_TOKEN" ghcr.io
- docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
- docker buildx create --use --name grfdbuilder
script:
- docker buildx build
--push
--no-cache
--platform linux/amd64,linux/arm64
--tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:develop-$CI_COMMIT_SHORT_SHA"
--tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:develop-$CI_COMMIT_SHORT_SHA" .
except:
refs:
- tags
variables:
- $CI_COMMIT_TITLE =~ /^RELEASE:.+$/
release:
needs: ['test docker']
stage: release
variables:
project_name: $CI_PROJECT_NAME
SEMANTIC_RELEASE_PACKAGE: $CI_PROJECT_NAME
script:
- npx semantic-release
only:
- master
except:
refs:
- tags
variables:
- $CI_COMMIT_TITLE =~ /^RELEASE:.+$/
build:
stage: build
variables:
project_name: $CI_PROJECT_NAME
SEMANTIC_RELEASE_PACKAGE: $CI_PROJECT_NAME
before_script:
- docker login -u "$DOCKERHUB_USER" -p "$DOCKERHUB_TOKEN" docker.io
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
- docker login -u "$DOCKERHUB_USER" -p "$GITHUB_TOKEN" ghcr.io
- docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
- docker buildx create --use --name grfdbuilder
script:
- docker buildx build
--push
--no-cache
--platform linux/amd64,linux/arm64
--tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:$CI_COMMIT_TAG"
--tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:latest"
--tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:$CI_COMMIT_TAG"
--tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:latest" .
only:
- tags
Check Packages:on-schedule:
only:
- schedules
before_script:
- |-
echo "Preparing package versions comparison."
# Check and, if necessary, update git user and mail
if [[ "$(git config --list | grep user.name)" != "user.name=$GIT_USER" ]];then
git config --global user.name $GIT_USER
fi
if [[ "$(git config --list | grep user.email)" != "user.email=$GIT_MAIL" ]];then
git config --global user.email $GIT_MAIL
fi
# Clean system of potentially interrupting images
docker image rm -f $DOCKERHUB_USER/$DOCKERHUB_REPO:latest
docker image rm -f $DOCKERHUB_REPO
rm -rf /tmp/$CI_PROJECT_PATH
mkdir -p /tmp/$CI_PROJECT_PATH
echo "Preparations complete."
script:
- |-
echo "Comparing package versions."
# Clone the repository
git clone $CI_PROJECT_URL.git /tmp/$CI_PROJECT_PATH && \
cd /tmp/$CI_PROJECT_PATH && \
if [ ! -s "package_versions.txt" ];then
echo "No package_versions.txt present. Creating..."
# Gather package information from latest build
docker run --rm --entrypoint /bin/sh -v /tmp/$CI_PROJECT_PATH:/tmp $DOCKERHUB_USER/$DOCKERHUB_REPO:latest -c '\
apt list -qq --installed > /tmp/package_versions.txt && \
sort -o /tmp/package_versions.txt /tmp/package_versions.txt && \
chmod 777 /tmp/package_versions.txt' && \
# Checkout our branch
git checkout -f $CI_DEFAULT_BRANCH && \
wait && \
# Add and commit new file to repository
git add package_versions.txt && \
git commit -m 'chore: Add list of package versions.' && \
# Push the changes to the remote
echo "https://***:***@$CI_SERVER_HOST/$CI_PROJECT_PATH.git"
git push "https://$GIT_USER:$GITLAB_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH".git --all && \
# Nice
echo "package_versions.txt added."
elif [ -s "package_versions.txt" ];then
echo "package_versions.txt present. Comapring..."
# Build local image for new package versions list
docker build --no-cache --tag $DOCKERHUB_REPO . && \
# Get packages from newly build local image
docker run --rm --entrypoint /bin/sh -v /tmp/$CI_PROJECT_PATH:/tmp $DOCKERHUB_REPO -c '\
apt list -qq --installed > /tmp/package_versions_new.txt && \
sort -o /tmp/package_versions_new.txt /tmp/package_versions_new.txt && \
chmod 777 /tmp/package_versions_new.txt' && \
# Get checksum of old packages
OLD_CHECKSUM=$(md5sum package_versions.txt | cut -f1 -d" ") && \
# Get checksum of new packages
NEW_CHECKSUM=$(md5sum package_versions_new.txt | cut -f1 -d" ") && \
# If new checksum is not the same as old checksum, we have new versions
if [ "${OLD_CHECKSUM}" != "${NEW_CHECKSUM}" ]; then
# Checkout our branch
git checkout -f $CI_DEFAULT_BRANCH && \
# Copy the new package versions list to repository
mv -f package_versions_new.txt package_versions.txt && \
wait && \
# Add and commit new file to repository
git add package_versions.txt && \
git commit -m 'build: Update installed packages in Docker container.' && \
# Push the changes to the remote
git push "https://$GIT_USER:$GITLAB_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git" --all && \
# Nice
echo "Packages updated."
else
echo "No package updates available."
fi
fi
echo "Comparison complete."
after_script:
- |-
echo "Cleaning up."
docker image rm -f $DOCKERHUB_USER/$DOCKERHUB_REPO:latest
docker image rm -f $DOCKERHUB_REPO
rm -rf /tmp/$CI_PROJECT_PATH
echo "Done."