Auto start/stop yandex cloud virtual machines based on gitlab job webhooks
- Install deps via
yarn
ornpm i
- Copy .env.example to .env then configure it
- Start via
yarn start
ornpm start
- Someone run job
- Service got a webhook
- If status created or running
- Check if we need send start event to virtual machine by:
- If instance stopped (controlled by code)
- If it's new job (send because we can, maybe it doesn't need but bugs or etc.)
- Previous start time < current date - $RESEND_START_INSTANCE_AFTER
- Check if we need send start event to virtual machine by:
- If status success
- Check if all jobs finished
- If status created or running
- Go to your Cloud Folder dashboard
- From the menu on the left, select
Service accounts
- In the upper right corner, click on the button
Create service account
- Fill in the fields Name, Description
- Add
compute.operator
role - Open created service account
- In the upper right corner, click on the button
Create new key
, chooseCreate authorized key
- Add description, if needed. Click
Create
- Save the Public and Private keys
- PORT -> App port
- LOG_LEVEL -> Log level, ignored for app logs if FORCE_CONSOLE_LOGS = true, available: 'DEBUG', 'INFO', 'WARN', 'ERROR', default: 'DEBUG'
- FORCE_CONSOLE_LOGS -> Ignore LOG_LEVEL for app logs, available: true/false
- GITLAB_WEBHOOK_SECRET -> Value from
Secret token
on GitLab Webhook config - GITLAB_DOMAIN -> gitlab domain, needs for custom gitlab urls
- GITLAB_TOKEN -> gitlab token for calling api
- GITLAB_REFETCH_INTERVAL -> every what time (in ms) send refetch jobs. In some cases gitlab doesn't send webhook events
- TELEGRAM_CHAT_ID -> Set telegram chat id if you need logs into telegram (require TELEGRAM_TOKEN)
- TELEGRAM_TOKEN -> Set telegram bot token if you need logs into telegram (require TELEGRAM_CHAT_ID)
- STOP_INSTANCE_AFTER -> After what time (in ms) need stop instance
- RESEND_START_INSTANCE_AFTER -> After what time (in ms) resend start instance if instance already started by code (for preemptible instances)
- YANDEX_INSTANCE_ID -> ID of Virtual machine instance
- YANDEX_SERVICE_IDENTITY -> ID of Service account
- YANDEX_KEY_IDENTITY -> ID of Service account authorized key
- YANDEX_KEY_PEM_PATH -> Path to private key of authorized key
- YANDEX_KEY_PEM -> Private key as string (used as priority between pem path)
- YANDEX_REFRESH_TIME -> Max IAM token lifetime before refresh (max 12 hours)
- Go to
Settings
->Webhooks
- Set url, path:
/webhook
- Secret token same as
GITLAB_WEBHOOK_SECRET
in config - Select
Job events
inTrigger
- Configure
SSL verification
if needed - Click
Add webhook