Skip to content

Commit

Permalink
✨ Add CI script
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki-minamoto authored and yosh-matsuda committed Nov 30, 2023
1 parent 462e6cb commit d1e3ce4
Show file tree
Hide file tree
Showing 12 changed files with 4,537 additions and 19,058 deletions.
138 changes: 138 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: "$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS"
when: never
- if: "$CI_COMMIT_BRANCH || $CI_COMMIT_TAG"

stages:
- test
- format
- output
- push

execute changes:
stage: test
tags:
- docker
image: python:3.10
script:
- apt-get update && apt-get install -y libgl1-mesa-dev
- pip install pipenv
- pipenv install --skip-lock
- |-
for NBPATH in $(git diff --name-only HEAD^..HEAD | grep '.ipynb'); do
echo "[NbConvertApp] Executing notebook $NBPATH"
OUTPUT=$(pipenv run jupyter nbconvert --to html --stdout --execute "$NBPATH" --log-level WARN 2>&1 >/dev/null) || true
if [ -n "$OUTPUT" ]; then
echo "$OUTPUT" 1>&2
STATUS=1
fi
done
rules:
- changes:
- notebooks/**/*.ipynb

execute all:
stage: test
tags:
- docker
image: python:3.10
script:
- apt-get update && apt-get install -y libgl1-mesa-dev
- pip install pipenv
- pipenv install --skip-lock
- |-
for NBPATH in $(/bin/ls -1 notebooks/{en,ja}/{tutorials,examples}/*.ipynb); do
echo "[NbConvertApp] Executing notebook $NBPATH"
OUTPUT=$(pipenv run jupyter nbconvert --to html --stdout --execute "$NBPATH" --log-level WARN 2>&1 >/dev/null) || true
if [ -n "$OUTPUT" ]; then
echo "$OUTPUT" 1>&2
STATUS=1
fi
done
- if [ -n "$STATUS" ]; then exit 1; fi
rules:
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
changes:
- notebooks/**/*.ipynb
when: on_success
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
when: manual
allow_failure: true

format:
stage: format
tags:
- docker
image: python:3.10
script:
- git remote set-url origin https://gitlab-ci-token:$GITLAB_TOKEN@${CI_REPOSITORY_URL##*@}
- git config user.email "[email protected]"
- git config user.name "GitLab Bot"
- git fetch origin $CI_COMMIT_REF_NAME
- git switch -C $CI_COMMIT_REF_NAME origin/$CI_COMMIT_REF_NAME
- git clean -xfd
- apt-get update && apt-get install -y jq
- pip install pipenv
- pipenv install --skip-lock
- pipenv run ./format.sh
- git add -u
- |-
if [ $(git status -s -uno --ignore-submodules=all | wc -l) -gt 0 ]; then
git commit -m "🎨 Format"
git push origin $CI_COMMIT_REF_NAME
exit 1
fi
rules:
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

html:
stage: output
tags:
- docker
image: python:3.10
script:
- git remote set-url origin https://gitlab-ci-token:$GITLAB_TOKEN@${CI_REPOSITORY_URL##*@}
- git config user.email "[email protected]"
- git config user.name "GitLab Bot"
- git fetch origin $CI_COMMIT_REF_NAME
- git switch -C $CI_COMMIT_REF_NAME origin/$CI_COMMIT_REF_NAME
- git clean -xfd
- pip install pipenv
- pipenv install --skip-lock
- pipenv run ./make-html-changes.sh
- git add -u
- |-
if [ $(git status -s -uno --ignore-submodules=all | wc -l) -gt 0 ]; then
git commit -m "📝 Update html files"
git push origin $CI_COMMIT_REF_NAME
exit 1
fi
rules:
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

sync-repositories:
stage: push
tags:
- docker
image: bitnami/git
resource_group: git-push
script:
# prepare remote repositories
- git remote set-url origin https://gitlab-ci-token:$GITLAB_TOKEN@${CI_REPOSITORY_URL##*@}
- git config remote.github.url >&- && git remote set-url github https://$GITHUB_USERNAME:[email protected]/fixstars/amplify-se-examples.git || git remote add github https://$GITHUB_USERNAME:[email protected]/fixstars/amplify-se-examples.git
- git config user.email "[email protected]"
- git config user.name "GitLab Bot"
- git fetch github main
- git fetch origin main develop
- git switch -C develop origin/develop
- git clean -xfd
# rebase and push
- git status
- git rebase github/main
- git push origin develop
- git push origin develop:main
- git push github develop:main
rules:
- if: '$CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "develop"'
6 changes: 6 additions & 0 deletions format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
shopt -s globstar

for f in $(/bin/ls -1 **/*.ipynb); do /bin/cat <<< $(jq --indent 1 --monochrome-output '(.cells[] | select(has("outputs")) | .outputs) = [] | (.cells[] | select(has("execution_count")) | .execution_count) = null | .metadata = {"language_info": {"name": "python", "pygments_lexer": "ipython3"}} | .cells[].metadata = {}' $f) > $f; done

black **/*.ipynb
Loading

0 comments on commit d1e3ce4

Please sign in to comment.