forked from datajoint/microns_phase3_nda
-
Notifications
You must be signed in to change notification settings - Fork 1
155 lines (154 loc) · 5.59 KB
/
worker-ami-build-1.0.0.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
name: worker-ami-build
on:
workflow_call:
inputs:
worker_type:
required: true
type: string
workflow_version:
required: true
type: string
staging:
required: true
type: string
runner_timeout:
required: false
type: number
default: 180
secrets:
RUNNER_PAT:
required: true
RUNNER_REGION:
required: true
RUNNER_TYPE:
required: true
RUNNER_AMI_ID:
required: true
RUNNER_SUBNET_ID:
required: true
RUNNER_SG_ID:
required: true
RUNNER_AWS_ACCESS_KEY_ID:
required: true
RUNNER_AWS_SECRET_ACCESS_KEY:
required: true
DJ_HOST:
required: true
DJ_USER:
required: true
DJ_PASS:
required: true
BUILD_PAT:
required: true
BUILD_AWS_ACCESS_KEY_ID:
required: true
BUILD_AWS_SECRET_ACCESS_KEY:
required: true
DEPLOY_SSH_KEY_BASE64:
required: true
DOCKER_REGISTRY_HOST:
required: true
DOCKER_REGISTRY_REPO:
required: true
DOCKER_REGISTRY_USERNAME:
required: true
DOCKER_REGISTRY_PASSWORD:
required: true
jobs:
start-runner:
timeout-minutes: 5 # normally it only takes 1-2 minutes
name: Start self-hosted EC2 runner
runs-on: ubuntu-latest
permissions:
actions: write
steps:
- name: Start EC2 runner
id: start-ec2-runner
uses: NextChapterSoftware/ec2-action-builder@main
with:
github_token: ${{ secrets.RUNNER_PAT }}
aws_access_key_id: ${{ secrets.RUNNER_AWS_ACCESS_KEY_ID }}
aws_secret_access_key: ${{ secrets.RUNNER_AWS_SECRET_ACCESS_KEY }}
aws_region: ${{ secrets.RUNNER_REGION }}
ec2_instance_type: ${{ secrets.RUNNER_TYPE }}
ec2_ami_id: ${{ secrets.RUNNER_AMI_ID }}
ec2_subnet_id: ${{ secrets.RUNNER_SUBNET_ID }}
ec2_security_group_id: ${{ secrets.RUNNER_SG_ID }}
ec2_instance_ttl: ${{ inputs.runner_timeout }} # Optional (default is 60 minutes)
ec2_spot_instance_strategy: BestEffort # Other options are: SpotOnly, BestEffort, MaxPerformance
ec2_instance_tags: >
[
{"Key": "Scope", "Value": "Works"},
{"Key": "Contract", "Value": "${{github.event.repository.name}}"},
{"Key": "Application", "Value": "worker-ami-build-runner"},
{"Key": "WorkflowVersion", "Value": "${{ inputs.workflow_version }}"},
{"Key": "WorkerType", "Value": "${{ inputs.worker_type }}"},
{"Key": "Staging", "Value": "${{ inputs.staging }}"}
]
build_worker_ami:
needs:
- start-runner
runs-on: ${{ github.run_id }}
env:
HOME: /root
steps:
- name: Checkout packer build repo
uses: actions/checkout@v4
with:
token: ${{ secrets.BUILD_PAT }}
repository: 'datajoint-company/dj-gitops'
path: 'dj-gitops'
- name: Setup Python
uses: actions/setup-python@v5
# WARNING: Running pip as the 'root'
# https://github.com/actions/setup-python/issues/513
with:
python-version: '3.10'
- run: pip install --user datajoint
- name: Call fetcher
env:
DJ_HOST: ${{ secrets.DJ_HOST }}
DJ_USER: ${{ secrets.DJ_USER }}
DJ_PASS: ${{ secrets.DJ_PASS }}
REPO_NAME: ${{github.event.repository.name}}
WORKER_TYPE: ${{ inputs.worker_type }}
WORKFLOW_VERSION: ${{ inputs.workflow_version }}
STAGING: ${{ inputs.staging }}
run: |
export ORG_NAME=$(echo ${REPO_NAME} | cut -d "_" -f 1)
export WORKFLOW_NAME=$(echo ${REPO_NAME} | cut -d "_" -f 2)
cd ${GITHUB_WORKSPACE}/dj-gitops/infrastructures/packer/worker_ami/inputs/
python fetcher.py
- name: Build worker AMI
env:
AWS_ACCESS_KEY_ID: ${{ secrets.BUILD_AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.BUILD_AWS_SECRET_ACCESS_KEY }}
STAGING: ${{ inputs.staging }}
REPO_NAME: ${{github.event.repository.name}}
DOCKER_REGISTRY_HOST: ${{ secrets.DOCKER_REGISTRY_HOST }}
DOCKER_REGISTRY_REPO: ${{ secrets.DOCKER_REGISTRY_REPO }}
DOCKER_REGISTRY_USERNAME: ${{ secrets.DOCKER_REGISTRY_USERNAME }}
DOCKER_REGISTRY_PASSWORD: ${{ secrets.DOCKER_REGISTRY_PASSWORD }}
run: |
# Set up deploy SSH key
echo -n "${{ secrets.DEPLOY_SSH_KEY_BASE64 }}" | base64 -d > ${GITHUB_WORKSPACE}/dj-gitops/infrastructures/packer/worker_ami/keys/${REPO_NAME}-deploy.pem
cd ${GITHUB_WORKSPACE}/dj-gitops/infrastructures/packer/worker_ami
packer init .
packer build -var-file ./inputs/${REPO_NAME}/${STAGING}.pkrvars.hcl . | tee ./outputs/packer.temp.log
- name: Terminate builder
env:
AWS_ACCESS_KEY_ID: ${{ secrets.BUILD_AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.BUILD_AWS_SECRET_ACCESS_KEY }}
if: always()
run: |
cd ${GITHUB_WORKSPACE}/dj-gitops/infrastructures/packer/worker_ami/outputs
bash terminate_builder_by_log.sh packer.temp.log
- name: Update worker AMI metadata
if: ${{ inputs.staging == 'stable' }}
env:
DJ_HOST: ${{ secrets.DJ_HOST }}
DJ_USER: ${{ secrets.DJ_USER }}
DJ_PASS: ${{ secrets.DJ_PASS }}
run: |
cd ${GITHUB_WORKSPACE}/dj-gitops/infrastructures/packer/worker_ami/outputs/
python insert_ami_meta.py