Deploy #69
This file contains hidden or 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: 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,1000:2048" | |
| 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@v18 | |
| id: components-to-nomad-jobs | |
| env: | |
| NOMAD_ENVIRONMENT: ${{ github.event.inputs.nomad_environment }} | |
| NOMAD_SHORT_ENVIRONMENT: ${{ github.event.inputs.nomad_environment == 'production' && 'prod' || 'stage' }} | |
| VAULT_ADDR: ${{ vars.VAULT_ADDR }} | |
| VAULT_TOKEN: ${{ vars.VAULT_TOKEN }} | |
| 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 | |
| environment: ${{ github.event.inputs.nomad_environment }} | |
| 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: 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(', ')}`); | |
| } | |