Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
crohr committed Sep 11, 2024
1 parent 6b31cfc commit fd1d661
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 123 deletions.
92 changes: 0 additions & 92 deletions .github/workflows/build-and-release.yml

This file was deleted.

69 changes: 69 additions & 0 deletions .github/workflows/build-test-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Build Test Release
run-name: ${{ inputs.image_id }}

on:
workflow_call:
inputs:
# e.g. ubuntu22-full-x64
image_id:
required: true
type: string
outputs:
ami_name:
description: "The name of the built AMI"
value: ${{ jobs.build.outputs.ami_name }}
ami_id:
description: "The ID of the built AMI"
value: ${{ jobs.build.outputs.ami_id }}

jobs:
build:
uses: ./.github/workflows/build.yml
with:
image_id: ${{ inputs.image_id }}
secrets: inherit

test1:
needs: build
uses: ./.github/workflows/test.yml
with:
ami_id: ${{ needs.build.outputs.ami_id }}
index: 1

test2:
needs: test1
uses: ./.github/workflows/test.yml
with:
ami_id: ${{ needs.build.outputs.ami_id }}
index: 2

test3:
needs: test2
uses: ./.github/workflows/test.yml
with:
ami_id: ${{ needs.build.outputs.ami_id }}
index: 3

test4:
needs: test3
uses: ./.github/workflows/test.yml
with:
ami_id: ${{ needs.build.outputs.ami_id }}
index: 4

test5:
needs: test4
uses: ./.github/workflows/test.yml
with:
ami_id: ${{ needs.build.outputs.ami_id }}
index: 5

release:
needs:
- build
- test5
uses: ./.github/workflows/release.yml
secrets: inherit
with:
image_id: ${{ inputs.image_id }}
ami_name: ${{ needs.build.outputs.ami_name }}
40 changes: 40 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Build and Release
run-name: ${{ inputs.distribution }}-full-${{ inputs.architecture }}

on:
workflow_call:
inputs:
image_id:
required: true
type: string
outputs:
ami_name:
description: "The name of the built AMI"
value: ${{ jobs.build.outputs.ami_name }}
ami_id:
description: "The ID of the built AMI"
value: ${{ jobs.build.outputs.ami_id }}

jobs:
build:
runs-on: ubuntu-latest
env:
SUBNET_ID: ${{ secrets.SUBNET_ID }}
outputs:
ami_name: ${{ steps.build.outputs.ami_name }}
ami_id: ${{ steps.build.outputs.ami_id }}
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2.2
bundler-cache: true
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- run: bundle exec bin/build --image-id ${{ inputs.image_id }}
id: build
timeout-minutes: 300
File renamed without changes.
27 changes: 8 additions & 19 deletions .github/workflows/sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,11 @@ name: Sync
on:
workflow_dispatch:
inputs:
distribution:
images:
type: string
required: true
description: 'Distribution(s) to build'
default: '["ubuntu22", "ubuntu24"]'
architecture:
type: string
required: true
description: 'Architecture(s) to build'
default: '["x64", "arm64"]'
promote:
type: boolean
required: false
description: 'Run the promotion job'
default: 'false'
default: '["ubuntu22-full-x64", "ubuntu22-full-arm64", "ubuntu24-full-x64", "ubuntu24-full-arm64"]'
schedule:
- cron: '0 8 */15 * *'

Expand All @@ -42,20 +32,19 @@ jobs:
env:
AMI_PREFIX: runs-on-dev
run: bundle exec bin/utils/cleanup-amis --force
- run: bundle exec bin/sync
- name: Synchronize with upstream repository
run: bundle exec bin/sync
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "Sync from upstream"

build-and-release:
build-test-release:
needs: sync
strategy:
fail-fast: false
matrix:
distribution: ${{ fromJSON(github.event.inputs.distribution || '["ubuntu22"]') }}
architecture: ${{ fromJSON(github.event.inputs.architecture || '["x64", "arm64"]') }}
uses: ./.github/workflows/build-and-release.yml
image_id: ${{ fromJSON(github.event.inputs.images || '["ubuntu22-full-x64", "ubuntu22-full-arm64", "ubuntu24-full-x64", "ubuntu24-full-arm64"]') }}
uses: ./.github/workflows/build-test-release.yml
secrets: inherit
with:
architecture: ${{ matrix.architecture }}
distribution: ${{ matrix.distribution }}
image_id: ${{ matrix.image_id }}
16 changes: 7 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
name: Test
run-name: Test ${{ inputs.distribution }}-full-${{ inputs.architecture }}
run-name: Test ${{ inputs.ami_id }}

on:
workflow_dispatch:
inputs:
distribution:
required: true
type: string
architecture:
ami_id:
required: true
type: string
workflow_call:
inputs:
distribution:
ami_id:
required: true
type: string
architecture:
index:
required: true
type: string
type: number

jobs:
test:
runs-on: runs-on,runner=2cpu-linux-${{ inputs.architecture }},image=${{ inputs.distribution }}-dev-${{ inputs.architecture }}
name: Test${{ inputs.index || '1' }}
runs-on: runs-on,cpu=2,ami=${{ inputs.ami_id }}
steps:
- name: Logs
run: sudo cat /var/log/cloud-init-output.log
Expand Down
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.PHONY: sync sync-*

SHELL:=/bin/bash
export AMI_PREFIX=runs-on-dev

cleanup-dev:
env $(shell cat .env) AMI_PREFIX=runs-on-dev ./bin/utils/cleanup-amis

cleanup-prod:
env $(shell cat .env) AMI_PREFIX=runs-on-v2.2 ./bin/utils/cleanup-amis

sync:
env $(shell cat .env) ./bin/sync

sync-%:
env $(shell cat .env) ./bin/sync --image-id $*

# Build steps for each distribution and architecture
build-%: sync-%
env $(shell cat .env) ./bin/build --image-id $*
44 changes: 41 additions & 3 deletions bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,56 @@ puts "Running Packer init command: #{packer_init_cmd.join(' ')}"
system(*packer_init_cmd) or fail("Packer init failed")

puts "Running Packer build command: #{packer_build_cmd.join(' ')}"
system(*packer_build_cmd) or fail("Packer build failed")
unless system(*packer_build_cmd)
# Cleanup any dangling EC2 instances
ec2_client = Aws::EC2::Client.new(region: 'us-east-1')
instances = ec2_client.describe_instances(
filters: [
{ name: 'instance-state-name', values: ['running', 'pending', 'stopping', 'stopped'] },
{ name: 'tag:ami_name', values: [ami_name] }
]
).reservations.flat_map(&:instances)

instances.each do |instance|
puts "Terminating instance #{instance.instance_id}"
ec2_client.terminate_instances(instance_ids: [instance.instance_id])
end

fail("Packer build failed")
end

# Fetch AMI ID corresponding to AMI name
ec2_client = Aws::EC2::Client.new(region: 'us-east-1')
ami_id = nil

begin
response = ec2_client.describe_images({
filters: [{ name: 'name', values: [ami_name] }]
})

if response.images.any?
ami_id = response.images.first.image_id
puts "Found AMI ID: #{ami_id} for AMI name: #{ami_name}"
else
puts "No AMI found with name: #{ami_name}"
end
rescue Aws::EC2::Errors::ServiceError => e
puts "Error fetching AMI ID: #{e.message}"
end


if github_output_file = ENV.fetch('GITHUB_OUTPUT', nil)
File.open(github_output_file, 'a') do |f|
f.puts "ami_name=#{ami_name}"
f.puts "ami_id=#{ami_id}"
end
puts "AMI name #{ami_name} added to GitHub Actions outputs"
puts "AMI name #{ami_name} and ID #{ami_id} added to GitHub Actions outputs"
end

if github_step_summary_file = ENV.fetch('GITHUB_STEP_SUMMARY', nil)
File.open(github_step_summary_file, 'a') do |f|
f.puts "## #{image['id']}"
f.puts "- **AMI Name:** `#{ami_name}`"
f.puts "- **AMI ID:** `#{ami_id}`"
end
end
end
1 change: 1 addition & 0 deletions patches/ubuntu/templates/ubuntu22-full-arm64.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ source "amazon-ebs" "build_ebs" {
run_tags = {
creator = "RunsOn"
contact = "[email protected]"
ami_name = "${var.ami_name}"
}

tags = {
Expand Down
1 change: 1 addition & 0 deletions patches/ubuntu/templates/ubuntu22-full-x64.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ source "amazon-ebs" "build_ebs" {
run_tags = {
creator = "RunsOn"
contact = "[email protected]"
ami_name = "${var.ami_name}"
}

tags = {
Expand Down
Loading

0 comments on commit fd1d661

Please sign in to comment.