CD: Automate the activation of the self hosted runner #43
Workflow file for this run
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
# The cross platform build was created based on the [Packaging Rust Applications for the NPM Registry blog](https://blog.orhun.dev/packaging-rust-for-npm/). | |
name: Continuous Deployment | |
on: | |
pull_request: | |
paths: | |
- .github/workflows/pypi-cd.yml | |
- .github/workflows/build-python-wrapper/action.yml | |
push: | |
tags: | |
- "v*.*" | |
concurrency: | |
group: pypi-${{ github.head_ref || github.ref }} | |
cancel-in-progress: true | |
permissions: write-all | |
jobs: | |
start-self-hosted-runner: | |
name: Start self hosted EC2 runner | |
runs-on: ubuntu-latest | |
outputs: | |
label: ${{ steps.start-ec2-runner.outputs.label }} | |
ec2-instance-id: ${{ steps.start-ec2-runner.outputs.ec2-instance-id }} | |
steps: | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_EC2_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_EC2_SECRET_ACCESS_KEY }} | |
aws-region: ${{ secrets.AWS_REGION }} | |
- name: Start EC2 runner | |
id: start-ec2-runner | |
uses: machulav/ec2-github-runner@v2 | |
with: | |
mode: start | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
ec2-image-id: ami-03831836fb1f65aac | |
ec2-instance-type: m6g.xlarge | |
subnet-id: subnet-0a88f7abc7e1490e1 | |
security-group-id: sg-05024f40170e648f0 | |
iam-role-name: github-actions-role | |
aws-resource-tags: > | |
[ | |
{"Key": "Name", "Value": "ec2-github-runner"}, | |
{"Key": "GitHubRepository", "Value": "${{ github.repository }}"} | |
] | |
publish-binaries: | |
needs: start-self-hosted-runner | |
if: github.repository_owner == 'aws' | |
name: Publish packages to PyPi | |
runs-on: ${{ matrix.build.RUNNER == 'self-hosted' && needs.start-self-hosted-runner.outputs.label || matrix.build.RUNNER }} | |
strategy: | |
fail-fast: false | |
matrix: | |
build: | |
- { | |
OS: ubuntu-latest, | |
NAMED_OS: linux, | |
RUNNER: ubuntu-latest, | |
ARCH: x64, | |
TARGET: x86_64-unknown-linux-gnu, | |
} | |
- { | |
OS: ubuntu-latest, | |
NAMED_OS: linux, | |
RUNNER: self-hosted, | |
ARCH: arm64, | |
TARGET: aarch64-unknown-linux-gnu, | |
CONTAINER: "2_28", | |
} | |
- { | |
OS: macos-latest, | |
NAMED_OS: darwin, | |
RUNNER: macos-latest, | |
ARCH: x64, | |
TARGET: x86_64-apple-darwin, | |
} | |
- { | |
OS: macos-latest, | |
NAMED_OS: darwin, | |
RUNNER: macos-13-xlarge, | |
arch: arm64, | |
TARGET: aarch64-apple-darwin, | |
} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
submodules: "true" | |
- name: Set the release version | |
shell: bash | |
run: | | |
export version=`if ${{ github.event_name == 'pull_request' }}; then echo '255.255.255'; else echo ${GITHUB_REF:11}; fi` | |
echo "RELEASE_VERSION=${version}" >> $GITHUB_ENV | |
- name: Set the package version for Python | |
working-directory: ./python | |
run: | | |
SED_FOR_MACOS=`if [[ "${{ matrix.build.OS }}" =~ .*"macos".* ]]; then echo "''"; fi` | |
sed -i $SED_FOR_MACOS "s|255.255.255|${{ env.RELEASE_VERSION }}|g" ./Cargo.toml | |
# Log the edited Cargo.toml file | |
cat Cargo.toml | |
- name: Set up Python | |
if: ${{ !contains(matrix.build.RUNNER, 'self-hosted') }} | |
uses: actions/setup-python@v4 | |
with: | |
python-version: "3.10" | |
- name: Set up Python older versions for MacOS | |
if: startsWith(matrix.build.NAMED_OS, 'darwin') | |
run: | | |
brew update | |
brew upgrade || true | |
brew install [email protected] [email protected] | |
- name: Setup Python for self-hosted Ubuntu runners | |
if: contains(matrix.build.OS, 'ubuntu') && contains(matrix.build.RUNNER, 'self-hosted') | |
run: | | |
sudo apt update -y | |
sudo apt upgrade -y | |
sudo apt install python3 python3-venv python3-pip -y | |
- name: Update package version in config.toml | |
uses: ./.github/workflows/update-glide-version | |
with: | |
folder_path: "${{ github.workspace }}/python/.cargo" | |
named_os: ${{ matrix.build.NAMED_OS }} | |
- name: Build Python wrapper | |
uses: ./.github/workflows/build-python-wrapper | |
with: | |
os: ${{ matrix.build.OS }} | |
target: ${{ matrix.build.TARGET }} | |
publish: "true" | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Include protobuf files in the package | |
working-directory: ./python | |
run: | | |
SED_FOR_MACOS=`if [[ "${{ matrix.build.OS }}" =~ .*"macos".* ]]; then echo "''"; fi` | |
sed -i $SED_FOR_MACOS '/pb2.py/d' .gitignore | |
# Log the edited .gitignore file | |
cat .gitignore | |
- name: Build Python wheels (linux) | |
if: startsWith(matrix.build.NAMED_OS, 'linux') | |
uses: PyO3/maturin-action@v1 | |
with: | |
working-directory: ./python | |
target: ${{ matrix.build.TARGET }} | |
args: --release --strip --out wheels -i ${{ github.event_name != 'pull_request' && 'python3.8 python3.9 python3.10 python3.11 python3.12' || 'python3.10' }} | |
manylinux: auto | |
container: ${{ matrix.build.CONTAINER != '' && matrix.build.CONTAINER || '2014' }} | |
before-script-linux: | | |
# Install protobuf compiler | |
if [[ $(`which apt`) != '' ]] | |
then | |
apt install protobuf-compiler -y | |
else | |
PB_REL="https://github.com/protocolbuffers/protobuf/releases" | |
curl -LO $PB_REL/download/v3.15.8/protoc-3.15.8-linux-x86_64.zip | |
unzip protoc-3.15.8-linux-x86_64.zip -d $HOME/.local | |
export PATH="$PATH:$HOME/.local/bin" | |
fi | |
- name: Build Python wheels (macos) | |
if: startsWith(matrix.build.NAMED_OS, 'darwin') | |
uses: PyO3/maturin-action@v1 | |
with: | |
working-directory: ./python | |
target: ${{ matrix.build.TARGET }} | |
args: --release --strip --out wheels -i ${{ github.event_name != 'pull_request' && 'python3.8 python3.9 python3.10 python3.11 python3.12' || 'python3.10' }} | |
- name: Upload Python wheels | |
if: github.event_name != 'pull_request' | |
uses: actions/upload-artifact@v3 | |
with: | |
name: wheels | |
path: python/wheels | |
if-no-files-found: error | |
publish-to-pypi: | |
if: github.event_name != 'pull_request' | |
name: Publish the base PyPi package | |
runs-on: ubuntu-latest | |
needs: publish-binaries | |
steps: | |
- uses: actions/download-artifact@v3 | |
with: | |
path: python/wheels | |
name: wheels | |
- name: Publish to PyPI | |
uses: PyO3/maturin-action@v1 | |
env: | |
MATURIN_PYPI_TOKEN: ${{ secrets.LIVEPYPI_API_TOKEN }} | |
MATURIN_REPOSITORY: pypi | |
with: | |
command: upload | |
args: --skip-existing python/wheels/* | |
stop-runner: | |
name: Stop self-hosted EC2 runner | |
needs: | |
- start-self-hosted-runner # required to get output from the start-runner job | |
- publish-to-pypi # required to wait when the main job is done | |
runs-on: ubuntu-latest | |
if: ${{ always() }} # required to stop the runner even if the error happened in the previous jobs | |
steps: | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_EC2_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_EC2_SECRET_ACCESS_KEY }} | |
aws-region: ${{ secrets.AWS_REGION }} | |
- name: Stop EC2 runner | |
uses: machulav/ec2-github-runner@v2 | |
with: | |
mode: stop | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
label: ${{ needs.start-self-hosted-runner.outputs.label }} | |
ec2-instance-id: ${{ needs.start-self-hosted-runner.outputs.ec2-instance-id }} |