Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a testing CI on the aws image #19

Closed
wants to merge 12 commits into from
18 changes: 18 additions & 0 deletions .github/scripts/check_instance.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

PUBLIC_IP=$1
start_time=$(date +%s)

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use the command -o /dev/null to capture output if you're going to send it to /dev/null? If you want to avoid having it printed in the terminal, the -s silent option is enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a copypasta from chatgpt 😅 Good catch

sleep 5
Comment on lines +6 to +7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add a echo -n "." before the sleep 5, that way, we have a visual during the process. But I'm not sure it's will be visible on github-action

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about echo "Pinging the server..." ?


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
16 changes: 16 additions & 0 deletions .github/scripts/check_meilisearch_availability.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

PUBLIC_IP=$1
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's better to comment on your code, but I don't think comments here do much.

echo "Meilisearch is available."
exit 0
fi
67 changes: 67 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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/
uses: hashicorp/setup-packer@main
with:
version: ${{ env.PACKER_VERSION }}

Is there a reason for not using the Packer Github action directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I tried initially but it didn't work. I can try again though

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's worth spending too much time on it - if it works with the official GitHub-action, all the better, otherwise, let's keep what works for now.


- name: Build image
run: |
packer init .
image_id="$(packer build -var 'image_name=${{ github.run_id }}' -machine-readable -only "amazon-ebs.*" meilisearch.pkr.hcl | awk -F, '$0 ~/artifact,0,id/ {print $6}' | cut -d ":" -f 2)"
echo "hello"
echo "$image_id"
echo $image_id
echo "IMAGE_ID=$image_id" >> $GITHUB_ENV

- name: check env
run: echo ${{ env.IMAGE_ID }}
- 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
Comment on lines +42 to +47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
- uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.1.7

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here for Terraform? Maybe we can use both simultaneously ...


- name: Create instance
run: |
terraform init
terraform apply -auto-approve -var ami_id=$IMAGE_ID
public_ip="$(terraform output public_ip_address | cut -d "\"" -f 2)"
echo "PUBLIC_IP=$public_ip" >> $GITHUB_ENV
- name: Test that the instance is running
run: |
sh .github/scripts/check_instance.sh ${{ env.PUBLIC_IP }}
- name: Test that Meilisearch is running correctly
run: |
sh .github/scripts/check_meilisearch_availability.sh ${{ env.PUBLIC_IP }}
- 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" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are predefined types, otherwise, you could change test by github_action_test or CI_test or something maybe more precise WDYT?

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
}