Skip to content

Commit

Permalink
Add a testing CI on the aws image
Browse files Browse the repository at this point in the history
  • Loading branch information
bidoubiwa committed Apr 27, 2023
1 parent 99fa169 commit 5c056f9
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 3 deletions.
17 changes: 17 additions & 0 deletions .github/scripts/check_instance.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

start_time=$(date +%s)

while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' --max-time 5 http://11.1.1)" != "200" ]]; do
sleep 5

elapsed_time=$(($(date +%s) - $start_time))
# A timeout error is raised after waiting for 10 minutes
if [[ $elapsed_time -gt 600 ]]; then
echo "Timeout error: The request took too long to complete."
exit 1
fi
done

echo "Instance is ready!"
exit 0
15 changes: 15 additions & 0 deletions .github/scripts/check_meilisearch_availability.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

status="$(curl http://$PUBLIC_IP/health)"

# Check if the variable content is equal to '{"status":"available"}'
if [ "$status" != '{"status":"available"}' ]; then
# If not, echo the variable value and an error message, and exit with code 1
echo "Error: Meilisearch is not running correctly."
echo "Server response: $status"
exit 1
else
# If it is equal, echo a success message and exit with code 0
echo "Meilisearch is available."
exit 0
fi
61 changes: 61 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Test AWS image

on:
pull_request:
push:
# trying and staging branches are for BORS config
branches:
- trying
- staging
- main

jobs:
test-image:
runs-on: ubuntu-latest
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install Packer
run: |
wget https://releases.hashicorp.com/packer/1.7.7/packer_1.7.7_linux_amd64.zip # change version
unzip packer_1.7.7_linux_amd64.zip
sudo mv packer /usr/local/bin/
- name: Build image
run: |
image_id=$(packer build -var 'image_name=temporary-iam-used-in-packer-testing' -machine-readable -only "amazon-ebs.*" meilisearch.pkr.hcl | awk -F, '$0 ~/artifact,0,id/ {print $6}' | cut -d ":" -f 2)
echo "export IMAGE_ID=$image_id" >> $GITHUB_ENV
- name: check env
run: echo $IMAGE_ID

- name: Install Terraform
run: |
sudo apt-get update
sudo apt-get install -y curl unzip
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install -y terraform
- name: Create instance
run: |
terraform init
terraform apply -auto-approve -var ami_id=$IMAGE_ID
public_ip=$(terraform output public_ip)
echo "export PUBLIC_IP=$public_ip" >> $GITHUB_ENV
- name: Test that the instance is running
run: |
sh .github/scripts/check-instance.sh
- name: Test that Meilisearch is running correctly
run: |
sh .github/scripts/check_meilisearch_availability.sh
- name: Destroy instance and AMI
if: always()
run: |
terraform destroy -var ami_id=$IMAGE_ID -auto-approve
15 changes: 12 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ packer_cache/
crash.log

# https://www.packer.io/guides/hcl/variables
# Exclude all .pkrvars.hcl files, which are likely to contain sensitive data,
# such as password, private keys, and other secrets. These should not be part of
# version control as they are data points which are potentially sensitive and
# Exclude all .pkrvars.hcl files, which are likely to contain sensitive data,
# such as password, private keys, and other secrets. These should not be part of
# version control as they are data points which are potentially sensitive and
# subject to change depending on the environment.
#
*.pkrvars.hcl

# For built boxes
*.box

**/terraform.plan

# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*
24 changes: 24 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions terraform-config.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
variable "ami_id" {
type = string
}

variable "region" {
description = "The AWS region to use for resources"
type = string
default = "us-east-1"
}

provider "aws" {
region = var.region
}

data "aws_security_group" "selected" {
id = "sg-037fd498b332442c1"
}

resource "aws_instance" "test" {
ami = var.ami_id
instance_type = "t2.small"
tags = {
Name = "aws-packer-testing-instance"
}
vpc_security_group_ids = [data.aws_security_group.selected.id]

provisioner "local-exec" {
command = "aws ec2 deregister-image --image-id ${self.ami}"
when = destroy

environment = {
AWS_REGION = "us-east-1"
}
}
}

output "public_ip_address" {
value = aws_instance.test.public_ip
}

0 comments on commit 5c056f9

Please sign in to comment.