Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: attempting to cleanup ci #113

Merged
merged 14 commits into from
Jan 25, 2024
124 changes: 12 additions & 112 deletions .github/workflows/bazel.yaml
Original file line number Diff line number Diff line change
@@ -1,73 +1,17 @@
name: Bazel
name: Build and test
on:
NikolaMilosa marked this conversation as resolved.
Show resolved Hide resolved
push:
branches:
- 'main'
pull_request:
merge_group:
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '30 1 * * *'
jobs:
bazel:
runs-on: ubuntu-22.04
steps:

########################################
# Setup
########################################
- name: Free Disk Space (Ubuntu)
uses: jlumbroso/free-disk-space@v1.3.1
with:
# this might remove tools that are actually needed,
# when set to "true" but frees about 6 GB
tool-cache: true
large-packages: false # this is slow
- uses: actions/checkout@v4
- uses: bazelbuild/setup-bazelisk@v2

########################################
# Download and unpack cache
########################################
- name: Mount bazel cache
uses: actions/cache@v3
with:
path: "~/.cache/bazel"
# Configure cache updates
# https://github.com/actions/cache/blob/main/tips-and-workarounds.md#update-a-cache
# https://github.com/actions/cache/blob/main/examples.md#---bazel
key: ${{ runner.os }}-bazel-${{ hashFiles('.bazelversion', '.bazelrc', 'WORKSPACE', 'WORKSPACE.bazel', 'MODULE.bazel', 'Cargo.Bazel.lock') }}
restore-keys: |
${{ runner.os }}-bazel-

########################################
# Once per night, update dependencies and completely delete and recreate bazel cache
########################################
- uses: actions/setup-python@v4
if: "${{github.event.schedule == '30 1 * * *' && github.ref == 'refs/heads/main'}}"
with:
python-version: "3.11"
- uses: snok/install-poetry@v1
if: "${{github.event.schedule == '30 1 * * *' && github.ref == 'refs/heads/main'}}"
- name: If running on schedule (at night) and on main, completely delete bazel cache then update deps
if: "${{github.event.schedule == '30 1 * * *' && github.ref == 'refs/heads/main'}}"
run: |
set -eExou pipefail
#
# Completely delete bazel cache
#
sudo rm -rf ~/.cache/bazel/*
#
# Update dependencies
#
cargo update
poetry update
./bin/poetry-export.sh
CARGO_BAZEL_REPIN=true bazel query //...
- uses: stefanzweifel/git-auto-commit-action@v5
if: "${{github.event.schedule == '30 1 * * *' && github.ref == 'refs/heads/main'}}"
with:
commit_message: Update dependencies
- uses: ./.github/workflows/setup-bazel
name: Setup bazel

########################################
# Build and test
Expand All @@ -82,31 +26,21 @@ jobs:
echo "GIT_HASH=$GITHUB_SHA" >> "$GITHUB_ENV" # Embed commit SHA as GIT_HASH
fi
- run: bazel build ...
name: Building
- run: bazel test ...
name: Testing

########################################
# Prepare release
########################################
- name: Extract binaries from bazel, so they can be pushed as GitHub artifacts in the next steps
- name: Upload artifact for release
uses: actions/upload-artifact@v2
if: startsWith(github.ref, 'refs/tags/v')
run: |
set -eExuo pipefail
# query the location of the bazel "dre" binary and copy it to the "release" directory
mkdir -p release
cp --dereference bazel-out/k8-opt/bin/rs/cli/dre release/dre
chmod +x release/dre
- name: Create a new GitHub Release
if: startsWith(github.ref, 'refs/tags/v')
# v0.1.15
uses: softprops/action-gh-release@de2c0eb89ae2a093876385947365aca7b0e5f844
with:
token: ${{ secrets.GITHUB_TOKEN }}
body_path: CHANGELOG.md
generate_release_notes: true
draft: true
prerelease: true
files: |
release/*
name: dre
path: bazel-out/k8-opt/bin/rs/cli/dre
- name: Call prepare release
uses: ./.github/workflows/prepare-release.yaml

########################################
# Upload container images
Expand All @@ -127,41 +61,7 @@ jobs:
########################################
- name: Update k8s deployments
if: ${{ startsWith(github.ref, 'refs/tags/v') || startsWith(github.ref, 'refs/heads/container') || (github.ref == 'refs/heads/main') }}
env:
GITLAB_API_TOKEN: ${{ secrets.GITLAB_API_TOKEN }}
run: |
set -eExou pipefail

cd .git

# checkout branch
git clone "https://gitlab-ci-token:${GITLAB_API_TOKEN}@gitlab.com/dfinity-lab/private/k8s/k8s.git"

cd k8s
git config user.email "idx@dfinity.org"
git config user.name "IDX Automation"
git checkout -b "update-image-tag-${GITHUB_SHA}"

# Update the internal dashboard image refs
# this regex matches the first group (ie the image name) and uses \1
# called a back-reference to insert the first group matched, the second
# part is to match the 40 characters hash that we replace with the $GITHUB_SHA
sed -i "s~\(\([[:alpha:]]\|-\)\+\):[[:alnum:]]\{40\}~\1:${GITHUB_SHA}~g" bases/apps/mainnet-dashboard/statefulset-slack.yaml bases/apps/mainnet-dashboard/backend/base/deployment.yaml bases/apps/mainnet-dashboard/frontend/deployment.yaml

# commit changes if there are any
git add .
if git diff --cached --quiet; then
echo "No changes to commit."
exit 0
fi

# Push changes and create a new merge request
git commit -m "Updating container base image refs"
git push \
-o merge_request.create \
-o merge_request.title="[nomrbot] - Updating container image refs mainnet-dashboard [$GITHUB_SHA]" \
-o merge_request.description="Changes to the release repository - [here](https://github.com/dfinity/dre/commit/$GITHUB_SHA)" \
--force --set-upstream origin "update-image-tag-${GITHUB_SHA}"
uses: ./.github/workflows/update-deployments.yaml

NikolaMilosa marked this conversation as resolved.
Show resolved Hide resolved
########################################
# Optimize bazel cache by hard-linking duplicate files
Expand Down
38 changes: 38 additions & 0 deletions .github/workflows/prepare-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Prepare release
on:
workflow_call:
inputs:
ARTIFACT_NAME:
required: true
description: "Name of the artifact to download"
type: string

jobs:
prepare:
runs-on: ubuntu-22.04
steps:
- name: Download built artifact
uses: actions/download-artifact@v2
with:
name: ${{ inputs.ARTIFACT_NAME }}

- name: Chmod
env:
ARTIFACT_NAME: ${{ inputs.ARTIFACT_NAME }}
run: chmod +x $ARTIFACT_NAME

- name: Prepare release
# v0.1.15
uses: softprops/action-gh-release@de2c0eb89ae2a093876385947365aca7b0e5f844
with:
token: ${{ secrets.GITHUB_TOKEN }}
body_path: CHANGELOG.md
generate_release_notes: true
draft: true
prerelease: true
files: |
release/*

- uses: geekyeggo/delete-artifact@v4
sasa-tomic marked this conversation as resolved.
Show resolved Hide resolved
with:
name: dre
31 changes: 31 additions & 0 deletions .github/workflows/setup-bazel/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Setup bazel
description: Reusable action for setting up bazel

runs:
using: composite
steps:
########################################
# Setup
########################################
- name: Free Disk Space (Ubuntu)
uses: jlumbroso/free-disk-space@v1.3.1
with:
# this might remove tools that are actually needed,
# when set to "true" but frees about 6 GB
tool-cache: true
large-packages: false # this is slow
- uses: bazelbuild/setup-bazelisk@v2

########################################
# Download and unpack cache
########################################
- name: Mount bazel cache
uses: actions/cache@v3
with:
path: "~/.cache/bazel"
# Configure cache updates
# https://github.com/actions/cache/blob/main/tips-and-workarounds.md#update-a-cache
# https://github.com/actions/cache/blob/main/examples.md#---bazel
key: ${{ runner.os }}-bazel-${{ hashFiles('.bazelversion', '.bazelrc', 'WORKSPACE', 'WORKSPACE.bazel', 'MODULE.bazel', 'Cargo.Bazel.lock') }}
restore-keys: |
${{ runner.os }}-bazel-
NikolaMilosa marked this conversation as resolved.
Show resolved Hide resolved
43 changes: 43 additions & 0 deletions .github/workflows/update-dependancies.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Update dependencies
on:
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '30 1 * * *'

jobs:
update:
runs-on: ubuntu-22.04
steps:
- uses: ./.github/workflows/setup-bazel

########################################
# Once per night, update dependencies and completely delete and recreate bazel cache
########################################
- uses: actions/setup-python@v4
with:
python-version: "3.11"
- uses: snok/install-poetry@v1
- name: If running on schedule (at night) and on main, completely delete bazel cache then update deps
NikolaMilosa marked this conversation as resolved.
Show resolved Hide resolved
run: |
set -eExou pipefail
#
# Completely delete bazel cache
#
sudo rm -rf ~/.cache/bazel/*
#
# Update dependencies
#
cargo update
poetry update
./bin/poetry-export.sh
CARGO_BAZEL_REPIN=true bazel query //...
NikolaMilosa marked this conversation as resolved.
Show resolved Hide resolved
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "chore: Update dependencies"
branch: bot-update-deps

########################################
# Optimize bazel cache by hard-linking duplicate files
########################################
- name: Optimize bazel cache directory before uploading
run: bin/optimize-bazel-cache.sh
NikolaMilosa marked this conversation as resolved.
Show resolved Hide resolved
43 changes: 43 additions & 0 deletions .github/workflows/update-deployments.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Update k8s deployments
on:
workflow_call:

jobs:
update:
runs-on: ubuntu-22.04
steps:
- env:
GITLAB_API_TOKEN: ${{ secrets.GITLAB_API_TOKEN }}
run: |
set -eExou pipefail

cd .git

# checkout branch
git clone "https://gitlab-ci-token:${GITLAB_API_TOKEN}@gitlab.com/dfinity-lab/private/k8s/k8s.git"

cd k8s
git config user.email "idx@dfinity.org"
git config user.name "IDX Automation"
git checkout -b "update-image-tag-${GITHUB_SHA}"

# Update the internal dashboard image refs
# this regex matches the first group (ie the image name) and uses \1
# called a back-reference to insert the first group matched, the second
# part is to match the 40 characters hash that we replace with the $GITHUB_SHA
sed -i "s~\(\([[:alpha:]]\|-\)\+\):[[:alnum:]]\{40\}~\1:${GITHUB_SHA}~g" bases/apps/mainnet-dashboard/statefulset-slack.yaml bases/apps/mainnet-dashboard/backend/base/deployment.yaml bases/apps/mainnet-dashboard/frontend/deployment.yaml

# commit changes if there are any
git add .
if git diff --cached --quiet; then
echo "No changes to commit."
exit 0
fi

# Push changes and create a new merge request
git commit -m "Updating container base image refs"
git push \
-o merge_request.create \
-o merge_request.title="[nomrbot] - Updating container image refs mainnet-dashboard [$GITHUB_SHA]" \
-o merge_request.description="Changes to the release repository - [here](https://github.com/dfinity/dre/commit/$GITHUB_SHA)" \
--force --set-upstream origin "update-image-tag-${GITHUB_SHA}"
Loading