Skip to content

Deploy

Deploy #39

Workflow file for this run

name: Deploy
on:
workflow_dispatch:
inputs:
components:
description: "The list of components to deploy"
required: true
type: string
nomad_environment:
description: "The environment to deploy to"
required: true
default: "production"
type: choice
options:
- staging
- production
# In the form of "component,cpu:ram;component2,cpu:ram"
nomad_resources:
description: "The resources to use when deploying"
required: false
default: "grid-bot,2000:1024"
type: string
permissions:
deployments: write
jobs:
parse-input-components:
name: Validate and find component directories
runs-on: ubuntu-latest
outputs:
nomad-files: ${{ steps.components-to-nomad-jobs.outputs.nomad-files }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Validate and Find components
uses: mfdlabs/component-finder-action@v8
id: find-component-directories
with:
components: ${{ github.event.inputs.components }}
component-search-directories: services
- name: Components to Nomad Jobs
uses: mfdlabs/component-nomad-parser-action@v14
id: components-to-nomad-jobs
env:
NOMAD_ENVIRONMENT: ${{ github.event.inputs.nomad_environment }}
NOMAD_SHORT_ENVIRONMENT: ${{ github.event.inputs.nomad_environment == 'production' && 'prod' || 'stage' }}
with:
components: ${{ steps.find-component-directories.outputs.components }}
resources: ${{ github.event.inputs.nomad_resources }}
deploy-nomad-jobs:
name: Deploy Nomad Jobs
runs-on: grid-bot-infra
needs: parse-input-components
if: ${{ needs.parse-input-components.outputs.nomad-files != '{}' && needs.parse-input-components.outputs.nomad-files != '' }}
env:
NOMAD_ADDR: ${{ vars.NOMAD_ADDR }}
NOMAD_TOKEN: ${{ secrets.NOMAD_TOKEN }}
steps:
- name: Setup Nomad CLI
uses: nferch/setup-nomad@v4.0.0
env:
NOMAD_TLS_SKIP_VERIFY: 1
- name: Set Initial GitHub Deployment Status
uses: chrnorm/deployment-action@v2
id: deployment
continue-on-error: true
with:
token: "${{ secrets.DEPLOYER_TOKEN }}"
environment: "${{ github.event.inputs.nomad_environment }}"
description: "Components: ${{ github.event.inputs.components }}"
- name: Deploy Nomad Jobs
uses: actions/github-script@v7
id: deploy-nomad-jobs
continue-on-error: true
with:
script: |
const fs = require('fs');
const path = require('path');
const child_process = require('child_process');
let jobs = ${{ needs.parse-input-components.outputs.nomad-files }};
jobs = new Map(Object.entries(jobs));
const failedJobs = [];
for (const [jobName, jobDefinition] of jobs) {
const [componentName] = jobName.split(':');
// Equivalent to mktemp
const tempFile = path.join(process.env.RUNNER_TEMP, `nomad-job-${componentName}.hcl`);
fs.writeFileSync(tempFile, jobDefinition);
// Poll the job status
try {
child_process.execSync(`nomad job run ${tempFile}`, { stdio: 'inherit' });
} catch (error) {
failedJobs.push(jobName);
}
}
if (failedJobs.length > 0) {
core.setFailed(`Failed to deploy the following jobs: ${failedJobs.join(', ')}`);
}
- name: Set GitHub Deployment Status
uses: chrnorm/deployment-status@v2
continue-on-error: true
with:
token: "${{ secrets.DEPLOYER_TOKEN }}"
deployment-id: ${{ steps.deployment.outputs.deployment_id }}
description: "Components: ${{ github.event.inputs.components }}"
# If the "deploy" step fails, the deployment status will be set to "failure"
# If the "deploy" step succeeds, the deployment status will be set to "success"
state: "${{ steps.deploy-nomad-jobs.outcome }}"