-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1278 from NickeZ/nickez/build-container-in-ci
Nickez/build container in ci
- Loading branch information
Showing
14 changed files
with
171 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
CI Design guidelines | ||
|
||
* It is more maintainable to create scripts in `.ci` and then call them from the workflows than to | ||
have scripts inline in the workflows. However, it is also good to split up scripts in multiple | ||
steps and jobs depending on what is being done. | ||
|
||
* The docker image is rebuilt if the `Dockerfile` or `.containerversion` file is modified. (In case | ||
of a push event it is also automatically published to docker hub). | ||
|
||
* If there are changes in the `Dockerfile`, then `.containerversion` must be updated with an | ||
unpublished version number. | ||
|
||
* We listen to two kinds of events, `pull_request` and `push` using two different workflows, | ||
`pr-ci.yml` and `ci.yml`. | ||
* On pull request events, github will checkout a version of the tree that is the PR branch merged | ||
into the base branch. When we look for what is modifed we can diff HEAD^1 to HEAD. If github | ||
didn't do this, it would've missed commits added to the base branch since the PR branch was | ||
forked. | ||
|
||
o--o--o--o <-- (base branch, typically 'master', parent 1) | ||
\ \ | ||
\ o <-- (HEAD) | ||
\ / | ||
o----o <-- Pull requst branch (parent 2) | ||
|
||
* On push events we get hashes of last commit before and after the push. When we look for what | ||
changed we can diff github.event.before with HEAD. | ||
|
||
o--o--o--o--o--o <-- github.event.after (HEAD) | ||
\ | ||
github.event.before |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
CONTAINER_REPO=shiftcrypto/firmware_v2 | ||
CONTAINER_VERSION=$(cat .containerversion) | ||
|
||
docker build --pull --no-cache -t $CONTAINER_REPO:latest -t $CONTAINER_REPO:$CONTAINER_VERSION . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash | ||
# | ||
# This script works on merge commits. <rev>^1 means the first parent of <rev>. | ||
# | ||
# When the github action creates a temporary merge commit for a pull request, the first parent will | ||
# be the base (the branch being merged into). | ||
|
||
set -e | ||
|
||
if git diff --name-only HEAD^1 HEAD | grep -E '^(\.containerversion|Dockerfile)' >/dev/null; then | ||
echo "true" | ||
exit | ||
fi | ||
echo "false" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
CONTAINER_REPO=shiftcrypto/firmware_v2 | ||
CONTAINER_VERSION=$(cat .containerversion) | ||
|
||
# docker manifest returns 1 (error) if the container doesn't exist and 0 (success) if it does. | ||
if docker manifest inspect $CONTAINER_REPO:$CONTAINER_VERSION > /dev/null; then | ||
>&2 echo Container version \'$CONTAINER_VERSION\' exists. | ||
echo true | ||
exit | ||
fi | ||
echo false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
CONTAINER_REPO=shiftcrypto/firmware_v2 | ||
CONTAINER_VERSION=$(cat .containerversion) | ||
|
||
docker push $CONTAINER_REPO:latest | ||
docker push $CONTAINER_REPO:$CONTAINER_VERSION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
CONTAINER_REPO=shiftcrypto/firmware_v2 | ||
CONTAINER_VERSION=$(cat .containerversion) | ||
|
||
docker pull $CONTAINER_REPO:$CONTAINER_VERSION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: Pull request CI common | ||
|
||
inputs: | ||
base-sha: | ||
required: true | ||
runs: | ||
using: "composite" | ||
steps: | ||
- name: Check if container files was modified and if container version already exists | ||
id: checks | ||
shell: bash | ||
run: | | ||
echo modified=$(./.ci/check-container-sources-modified) >> "$GITHUB_OUTPUT" | ||
echo container-published=$(./.ci/check-container-version-published) >> "$GITHUB_OUTPUT" | ||
- name: Build container image | ||
if: steps.checks.outputs.modified == 'true' | ||
shell: bash | ||
run: | | ||
if "${{ steps.checks.outputs.container-published }}" == "true"; then | ||
echo "::error::Container modified but version $(cat .containerversion) already published" | ||
exit 1 | ||
fi | ||
./.ci/build-container | ||
- name: Pull container image | ||
if: steps.checks.outputs.modified == 'false' | ||
shell: bash | ||
run: ./.ci/pull-container | ||
|
||
- name: Run CI in container | ||
shell: bash | ||
run: ./.ci/run-container-ci ${{github.workspace}} ${{ inputs.base-sha }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,12 @@ jobs: | |
uses: actions/checkout@v4 | ||
with: | ||
submodules: recursive | ||
fetch-depth: 0 | ||
|
||
- name: Pull container image | ||
run: ./.ci/run-container-ci pull | ||
|
||
- name: Run CI in container | ||
run: ./.ci/run-container-ci ${{github.workspace}} ${{ github.base_ref }} | ||
- name: CI | ||
uses: ./.github/actions/pr-ci-common | ||
with: | ||
base-sha: ${{ github.event.pull_request.base.sha }} | ||
|
||
# Generate a list of commits to run CI on | ||
generate-matrix: | ||
|
@@ -34,9 +34,15 @@ jobs: | |
- name: Create jobs for commits in PR history | ||
id: set-matrix | ||
run: | | ||
echo matrix=$(.ci/matrix-from-commit-log origin/${{github.base_ref}}..${{ github.event.pull_request.head.sha}}~) >> $GITHUB_OUTPUT | ||
echo matrix=$(.ci/matrix-from-commit-log ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }}~) >> $GITHUB_OUTPUT | ||
# Run this job for every commit in the PR except HEAD. | ||
# This job simulates what github does for the PR HEAD commit but for every other commit in the | ||
# PR. So for every commit, it creates a merge commit between that commit and the base branch. | ||
# Then it runs the CI on that merge commit. | ||
# The only caveat is that this file (pr-ci.yml) is already loaded from the PR HEAD merge commit, | ||
# and therefore we need to load the `.ci` scripts from the PR HEAD merge commit. The outcome of | ||
# that is that changes to the CI is not tested per commit. All commits use the final version. | ||
pr-commit-ci: | ||
runs-on: ubuntu-22.04 | ||
needs: [ generate-matrix ] | ||
|
@@ -58,13 +64,14 @@ jobs: | |
GIT_COMMITTER_NAME: Bot | ||
GIT_COMMITTER_EMAIL: [email protected] | ||
run: | | ||
git fetch origin ${{ matrix.commit }} | ||
git fetch origin ${{ matrix.commit }} ${{ github.event.pull_request.merge_commit_sha }} | ||
git merge --no-ff --no-edit ${{ matrix.commit }} | ||
echo "merge commit parents:" | ||
git log -1 --format="Head %H, Parents %P" | ||
# Since the workflow definition is taken from the pull request merge commit, we need to | ||
# get the .ci scripts from there as well. | ||
git checkout -f ${{ github.event.pull_request.merge_commit_sha }} -- .ci .github | ||
- name: Pull container image | ||
run: ./.ci/run-container-ci pull | ||
|
||
- name: Run CI in container | ||
run: ./.ci/run-container-ci ${{github.workspace}} ${{ github.base_ref }} | ||
- name: CI | ||
uses: ./.github/actions/pr-ci-common | ||
with: | ||
base-sha: ${{ github.event.pull_request.base.sha }} |
This file was deleted.
Oops, something went wrong.