Skip to content

Commit 062b450

Browse files
authored
Merge pull request #1278 from NickeZ/nickez/build-container-in-ci
Nickez/build container in ci
2 parents 8b0d8a0 + 51bc17b commit 062b450

14 files changed

+171
-62
lines changed

.ci/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
CI Design guidelines
2+
3+
* It is more maintainable to create scripts in `.ci` and then call them from the workflows than to
4+
have scripts inline in the workflows. However, it is also good to split up scripts in multiple
5+
steps and jobs depending on what is being done.
6+
7+
* The docker image is rebuilt if the `Dockerfile` or `.containerversion` file is modified. (In case
8+
of a push event it is also automatically published to docker hub).
9+
10+
* If there are changes in the `Dockerfile`, then `.containerversion` must be updated with an
11+
unpublished version number.
12+
13+
* We listen to two kinds of events, `pull_request` and `push` using two different workflows,
14+
`pr-ci.yml` and `ci.yml`.
15+
* On pull request events, github will checkout a version of the tree that is the PR branch merged
16+
into the base branch. When we look for what is modifed we can diff HEAD^1 to HEAD. If github
17+
didn't do this, it would've missed commits added to the base branch since the PR branch was
18+
forked.
19+
20+
o--o--o--o <-- (base branch, typically 'master', parent 1)
21+
\ \
22+
\ o <-- (HEAD)
23+
\ /
24+
o----o <-- Pull requst branch (parent 2)
25+
26+
* On push events we get hashes of last commit before and after the push. When we look for what
27+
changed we can diff github.event.before with HEAD.
28+
29+
o--o--o--o--o--o <-- github.event.after (HEAD)
30+
\
31+
github.event.before

.ci/build-container

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
CONTAINER_REPO=shiftcrypto/firmware_v2
6+
CONTAINER_VERSION=$(cat .containerversion)
7+
8+
docker build --pull --no-cache -t $CONTAINER_REPO:latest -t $CONTAINER_REPO:$CONTAINER_VERSION .

.ci/check-container-sources-modified

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
#
3+
# This script works on merge commits. <rev>^1 means the first parent of <rev>.
4+
#
5+
# When the github action creates a temporary merge commit for a pull request, the first parent will
6+
# be the base (the branch being merged into).
7+
8+
set -e
9+
10+
if git diff --name-only HEAD^1 HEAD | grep -E '^(\.containerversion|Dockerfile)' >/dev/null; then
11+
echo "true"
12+
exit
13+
fi
14+
echo "false"

.ci/check-container-version-published

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
CONTAINER_REPO=shiftcrypto/firmware_v2
6+
CONTAINER_VERSION=$(cat .containerversion)
7+
8+
# docker manifest returns 1 (error) if the container doesn't exist and 0 (success) if it does.
9+
if docker manifest inspect $CONTAINER_REPO:$CONTAINER_VERSION > /dev/null; then
10+
>&2 echo Container version \'$CONTAINER_VERSION\' exists.
11+
echo true
12+
exit
13+
fi
14+
echo false

.ci/check-pep8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ set -o pipefail
1111
command -v git >/dev/null 2>&1 || { echo >&2 "git is missing"; exit 1; }
1212

1313
# grep will exit with 1 if no lines are found
14-
FILES=$(git --no-pager diff --diff-filter=d --name-only ${TARGET_BRANCH} | grep -v -e "old/" -e "generated/" -e "rust/vendor/" | grep -E ".py\$" || exit 0)
14+
FILES=$(git --no-pager diff --diff-filter=d --name-only ${TARGET_BRANCH} HEAD | grep -v -e "old/" -e "generated/" -e "rust/vendor/" | grep -E ".py\$" || exit 0)
1515
if [ -z "${FILES}" ] ; then
1616
exit 0
1717
fi

.ci/check-style

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ if test -t 1; then
2525
fi
2626
fi
2727

28-
if git --no-pager diff --diff-filter=d --name-only ${TARGET_BRANCH} | grep -v -E "(^src/(rust|ui/fonts)|.*ugui.*|.*base32.*)" | grep -E "^(src|test)" | grep -E "\.(c|h)\$" | xargs -n1 "$CLANGFORMAT" -output-replacements-xml | grep -c "<replacement " >/dev/null; then
28+
if git --no-pager diff --diff-filter=d --name-only ${TARGET_BRANCH} HEAD | grep -v -E "(^src/(rust|ui/fonts)|.*ugui.*|.*base32.*)" | grep -E "^(src|test)" | grep -E "\.(c|h)\$" | xargs -n1 "$CLANGFORMAT" -output-replacements-xml | grep -c "<replacement " >/dev/null; then
2929
echo -e "${red}Not $CLANGFORMAT clean${normal}"
3030
# Apply CF to the files
31-
git --no-pager diff --diff-filter=d --name-only ${TARGET_BRANCH} | grep -v -E "(^src/(rust|ui/fonts)|.*ugui.*|.*base32.*)" | grep -E "^(src|test)" | grep -E "\.(c|h)\$" | xargs -n1 "$CLANGFORMAT" -i
31+
git --no-pager diff --diff-filter=d --name-only ${TARGET_BRANCH} HEAD | grep -v -E "(^src/(rust|ui/fonts)|.*ugui.*|.*base32.*)" | grep -E "^(src|test)" | grep -E "\.(c|h)\$" | xargs -n1 "$CLANGFORMAT" -i
3232
# Print list of files that weren't formatted correctly
3333
echo -e "Incorrectly formatted files:"
3434
git --no-pager diff --name-only

.ci/check-tidy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ for dir in build build-build; do
3636
s/-Wno-cast-function-type//g; s/-mfpu=fpv4-sp-d16//g; s/-Wformat-signedness//g' ${dir}/compile_commands.json
3737

3838
# Only check our files
39-
SOURCES1=$(git --no-pager diff --diff-filter=d --name-only ${TARGET_BRANCH} |\
39+
SOURCES1=$(git --no-pager diff --diff-filter=d --name-only ${TARGET_BRANCH} HEAD |\
4040
grep -v -E "(^src/(drivers|ui/fonts)|.*ugui.*|.*base32.*)" |\
4141
grep -E "^(src)" |\
4242
grep -v "^test/unit-test/u2f/" |\

.ci/publish-container

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
CONTAINER_REPO=shiftcrypto/firmware_v2
6+
CONTAINER_VERSION=$(cat .containerversion)
7+
8+
docker push $CONTAINER_REPO:latest
9+
docker push $CONTAINER_REPO:$CONTAINER_VERSION

.ci/pull-container

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
CONTAINER_REPO=shiftcrypto/firmware_v2
6+
CONTAINER_VERSION=$(cat .containerversion)
7+
8+
docker pull $CONTAINER_REPO:$CONTAINER_VERSION

.ci/run-container-ci

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616

1717
# The script runs all CI builds and checks in a Docker container.
1818
# It accepts two positional arguments:
19-
# 1. A workspace dir, the root of the git repo clone, or "pull" literal.
20-
# In the latter case, CI container image is pulled from a registry.
21-
# 2. An optional target branch for code style diffs. Defaults to "master" for
22-
# push commits and overwritten with TRAVIS_BRANCH env var for pull requests
23-
# when run on Travis CI.
19+
# 1. A workspace dir, the root of the git repo clone, to be mounted in the container.
20+
# 2. A git revision (see man gitrevisions) to compare against HEAD to filter out modified and new
21+
# files. Some scripts only run on that subset.
2422

2523
set -e
2624
set -x
@@ -29,28 +27,13 @@ CONTAINER_REPO=shiftcrypto/firmware_v2
2927
CONTAINER_VERSION=$(cat .containerversion)
3028
CONTAINER=$CONTAINER_REPO:${CONTAINER_VERSION}
3129

32-
if [ "$1" == "pull" ] ; then
33-
docker pull "$CONTAINER"
34-
exit 0
35-
fi
36-
3730
WORKSPACE_DIR="$1"
3831
if [ -z "${WORKSPACE_DIR}" ]; then
3932
echo "Workspace dir path is empty."
4033
exit 1
4134
fi
4235

43-
TARGET_BRANCH="${2:-master}"
44-
if [ "${TRAVIS}" == "true" ] && [ "${TRAVIS_PULL_REQUEST}" != "false" ] ; then
45-
TARGET_BRANCH=${TRAVIS_BRANCH}
46-
fi
47-
48-
# Fetch origin/master so that we can diff when checking coding style.
49-
git remote set-branches --add origin ${TARGET_BRANCH}
50-
git fetch origin
51-
52-
TARGET_BRANCH=origin/${TARGET_BRANCH}
53-
36+
TARGET_BRANCH="$2"
5437
# The safe.directory config is so that git commands work. even though the repo folder mounted in
5538
# Docker is owned by root, which can be different from the owner on the host.
5639
docker run -e TARGET_BRANCH="${TARGET_BRANCH}" \

0 commit comments

Comments
 (0)