train-model: Sentiment Analysis @ EC2 Spot #8
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: "train-model: Sentiment Analysis @ EC2 Spot" | |
on: | |
workflow_dispatch: | |
inputs: | |
aws_region: | |
description: 'AWS_REGION' | |
required: true | |
default: 'eu-west-2' | |
type: string | |
ami_id: | |
description: 'AMI_ID' | |
required: true | |
default: 'ami-093cb9fb2d34920ad' | |
type: string | |
ec2-instance-type: | |
description: 'AMI_ID' | |
required: true | |
default: 't3.nano' | |
type: string | |
jobs: | |
start-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@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ github.event.inputs.aws_region }} | |
- name: Start EC2 runner | |
id: start-ec2-runner | |
uses: digital-defiance/ec2-github-runner@v2 | |
with: | |
mode: start | |
pre-runner-script: | | |
sudo yum update -y && \ | |
sudo yum install docker git libicu -y | |
sudo systemctl enable docker | |
sudo systemctl start docker | |
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }} | |
market-type: spot | |
ec2-image-id: ${{ github.event.inputs.ami_id }} | |
ec2-instance-type: ${{ github.event.inputs.ec2-instance-type }} | |
subnet-id: subnet-07512e20d23632607 | |
security-group-id: sg-098d5c6cb021e55a4 | |
training-loop: | |
name: Train model | |
needs: start-runner # required to start the main job when the runner is ready | |
runs-on: ${{ needs.start-runner.outputs.label }} # run the job on the newly created runner | |
container: | |
image: pytorch/pytorch:2.1.2-cuda11.8-cudnn8-runtime | |
env: | |
MODEL_PARAM_1: 'yes' | |
steps: | |
- name: Install dependencies | |
run: | | |
pip install boto3==1.34.8 psutil==5.9.7 mlflow==2.9.2 pydantic==2.5.3 pydantic-settings==2.1.0 tqdm==4.66.1 tiktoken==0.5.2 | |
- uses: actions/checkout@v3 | |
- name: Hello World | |
run: echo 'Hello World!' | |
- name: Where am I | |
run: ls | |
- name: Do I see the parameter ? | |
run: echo $MODEL_PARAM_1 | |
stop-runner: | |
name: Stop self-hosted EC2 runner | |
needs: | |
- start-runner # required to get output from the start-runner job | |
- training-loop # 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@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ github.event.inputs.aws_region }} | |
- name: Stop EC2 runner | |
uses: digital-defiance/ec2-github-runner@v2 | |
with: | |
mode: stop | |
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }} | |
label: ${{ needs.start-runner.outputs.label }} | |
ec2-instance-id: ${{ needs.start-runner.outputs.ec2-instance-id }} |