CI #392
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
name: CI | |
on: | |
push: | |
release: | |
types: [published] | |
workflow_dispatch: | |
inputs: | |
deploy: | |
description: 'Deploy the website' | |
required: false | |
default: false | |
type: boolean | |
toit-version: | |
description: 'The version of the Toit documentation to fetch' | |
required: false | |
type: string | |
default: | |
toitlang-run-id: | |
description: 'The run ID of the Toit documentation to fetch' | |
required: false | |
type: string | |
default: | |
run-tests: | |
description: 'Run tests' | |
required: false | |
type: boolean | |
env: | |
BUILD_DIR: build | |
HAS_PROTOBUF: false | |
ARTIFACT_EXTENSION: tar.gz | |
DEV_DEPLOYMENT_OWNER: toitware | |
DEV_DEPLOYMENT_REPO: web-toitdocs-dev | |
DEV_DEPLOYMENT_WORKFLOW: ci.yml | |
jobs: | |
ci: | |
runs-on: ubuntu-latest | |
env: | |
TOITLANG_RUN_ID: | |
# Will be set in a step. | |
VERSION: | |
steps: | |
- name: Show inputs | |
if: github.event_name == 'workflow_dispatch' | |
run: echo "${{ toJSON(github.event.inputs) }}" | |
- uses: actions/checkout@v4 | |
with: | |
submodules: recursive | |
# We are using deprecated functionality. Downgrade Node so we still compile. | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: 16 | |
- name: Install | |
run: | | |
yarn install | |
- name: Protobuf | |
if: env.HAS_PROTOBUF == 'true' | |
run: | | |
sudo apt-get update | |
sudo apt-get install protobuf-compiler | |
make protobuf | |
- name: Lint | |
if: ${{ github.event.inputs.run-tests != 'false' }} | |
run: | | |
yarn lint | |
- name: Test | |
if: ${{ github.event.inputs.run-tests != 'false' }} | |
run: | | |
yarn test:ci | |
- name: Build | |
run: | | |
yarn build | |
- name: Generate version | |
id: version | |
shell: bash | |
run: | | |
GIT_VERSION=$(tools/gitversion) | |
echo $GIT_VERSION | |
# Replace any '/' with '-'. | |
VERSION=${GIT_VERSION//\//-} | |
echo $VERSION | |
echo $VERSION > VERSION | |
echo VERSION=$VERSION >> $GITHUB_ENV | |
echo "version=$VERSION" >> $GITHUB_OUTPUT | |
- name: Prepare artifacts | |
run: | | |
tar c -zf $VERSION.$ARTIFACT_EXTENSION -C $BUILD_DIR/ . | |
- name: Upload artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: build # This artifact name is used by the dev-deployment. | |
retention-days: 2 | |
if-no-files-found: error | |
path: | | |
${{ env.VERSION }}.${{ env.ARTIFACT_EXTENSION }} | |
VERSION | |
- name: Set toitlang run-id environment variable | |
if: github.event.inputs.toitlang-run-id | |
run: | | |
echo "TOITLANG_RUN_ID=${{ github.event.inputs.toitlang-run-id }}" >> $GITHUB_ENV | |
- name: Dispatch dev-deployment | |
if: github.ref == 'refs/heads/master' || github.event_name == 'workflow_dispatch' | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.LEON_REPOSITORY_DISPATCH }} | |
script: | | |
await github.rest.actions.createWorkflowDispatch({ | |
owner: '${{ env.DEV_DEPLOYMENT_OWNER }}', | |
repo: '${{ env.DEV_DEPLOYMENT_REPO }}', | |
workflow_id: '${{ env.DEV_DEPLOYMENT_WORKFLOW }}', | |
ref: 'main', | |
inputs: { | |
'run-id': '${{ github.run_id }}', | |
'toit-version': 'latest', | |
'toitlang-run-id': '${{ env.TOITLANG_RUN_ID }}', | |
}, | |
}); | |
- name: Upload release artifacts | |
if: | | |
github.event_name == 'release' && !github.event.release.prerelease | |
uses: svenstaro/upload-release-action@v2 | |
with: | |
repo_token: ${{ secrets.GITHUB_TOKEN }} | |
file: ${{ env.VERSION }}.${{ env.ARTIFACT_EXTENSION }} | |
asset_name: build.${{ env.ARTIFACT_EXTENSION }} | |
tag: ${{ github.event.release.tag_name }} | |
overwrite: true | |
deploy: | |
if: | | |
github.event.inputs.deploy == 'true' || | |
github.event_name == 'release' && !github.event.release.prerelease | |
# Only deploy if all tests passed. | |
needs: [ci] | |
runs-on: ubuntu-latest | |
steps: | |
# No need to checkout the project, since all we need is to download the | |
# build artifact from the build step. | |
- uses: actions/download-artifact@v4 | |
with: | |
name: build | |
path: build | |
- name: Untar | |
run: | | |
cd build | |
VERSION=$(cat VERSION) | |
tar x -zf $VERSION.$ARTIFACT_EXTENSION | |
rm $VERSION.$ARTIFACT_EXTENSION | |
- name: Fetch Toit documentation | |
run: | | |
# Fetch the Toit documentation from the GitHub release page. | |
VERSION=${{ github.event.inputs.toit-version }} | |
if [[ -z "$VERSION" || "$VERSION" == "latest" ]]; then | |
URL=https://github.com/toitlang/toit/releases/latest/download/toitdoc.json | |
else | |
URL=https://github.com/toitlang/toit/releases/download/$VERSION/toitdoc.json | |
fi | |
echo "Fetching Toit documentation from $URL" | |
mkdir -p build | |
curl -L $URL -o build/toitdoc.json | |
- name: Disable Jekyll | |
run: | | |
touch build/.nojekyll | |
# This seems to be the simplest way to publish to a separate branch. | |
- name: Deploy | |
uses: peaceiris/actions-gh-pages@v4 | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
publish_branch: gh-pages | |
publish_dir: ./build | |
# Optional. This will create a CNAME file so GitHub Pages serves it | |
# under this domain. | |
cname: libs.toit.io |