1+ # For the sake of saving time, only run this step if the test-group is one that will run tests against an Elasticsearch on localhost.
12name : Set up local Elasticsearch
23
34description : Install a local Elasticsearch with version that matches prod
@@ -6,20 +7,83 @@ inputs:
67 token :
78 description : PAT
89 required : true
10+ elasticsearch_version :
11+ description : Version of Elasticsearch to install
12+ required : true
13+ # Make sure the version matches production and is available on Docker Hub
14+ default : ' 8.12.0'
915
1016runs :
1117 using : ' composite'
1218 steps :
13- - name : Install a local Elasticsearch for testing
14- # For the sake of saving time, only run this step if the test-group
15- # is one that will run tests against an Elasticsearch on localhost.
16- uses : getong/elasticsearch-action@95b501ab0c83dee0aac7c39b7cea3723bef14954
19+ # Cache the elasticsearch image to prevent Docker Hub rate limiting
20+ - name : Cache Docker layers
21+ id : cache-docker-layers
22+ uses : actions/cache@v2
1723 with :
18- # Make sure this matches production
19- # It might also need to match what's available on Docker hub
20- elasticsearch version : ' 8.12.0'
21- host port : 9200
22- container port : 9200
23- host node port : 9300
24- node port : 9300
25- discovery type : ' single-node'
24+ path : /tmp/docker-cache
25+ key : ${{ runner.os }}-elasticsearch-${{ inputs.elasticsearch_version }}
26+ restore-keys : |
27+ ${{ runner.os }}-elasticsearch-
28+
29+ - name : Load cached Docker image
30+ shell : bash
31+ if : steps.cache-docker-layers.outputs.cache-hit == 'true'
32+ run : docker load -i /tmp/docker-cache/elasticsearch.tar || echo "No cache found for elasticsearch, pulling image"
33+
34+ - name : Pull Docker image
35+ shell : bash
36+ if : steps.cache-docker-layers.outputs.cache-hit != 'true'
37+ run : docker pull elasticsearch:${{ inputs.elasticsearch_version }}
38+
39+ - name : Save Docker image to cache
40+ shell : bash
41+ if : steps.cache-docker-layers.outputs.cache-hit != 'true'
42+ run : |
43+ mkdir -p /tmp/docker-cache
44+ docker save -o /tmp/docker-cache/elasticsearch.tar elasticsearch:${{ inputs.elasticsearch_version }}
45+
46+ # Setups the Elasticsearch container
47+ # Derived from https://github.com/getong/elasticsearch-action
48+ - name : Run Docker container
49+ shell : bash
50+ env :
51+ INPUT_ELASTICSEARCH_VERSION : ${{ inputs.elasticsearch_version }}
52+ INPUT_HOST_PORT : 9200
53+ INPUT_CONTAINER_PORT : 9200
54+ INPUT_HOST_NODE_PORT : 9300
55+ INPUT_NODE_PORT : 9300
56+ INPUT_DISCOVERY_TYPE : ' single-node'
57+ run : |
58+ docker network create elastic
59+
60+ docker run --network elastic \
61+ -e 'node.name=es1' \
62+ -e 'cluster.name=docker-elasticsearch' \
63+ -e 'cluster.initial_master_nodes=es1' \
64+ -e 'discovery.seed_hosts=es1' \
65+ -e 'cluster.routing.allocation.disk.threshold_enabled=false' \
66+ -e 'bootstrap.memory_lock=true' \
67+ -e 'ES_JAVA_OPTS=-Xms1g -Xmx1g' \
68+ -e 'xpack.security.enabled=false' \
69+ -e 'xpack.license.self_generated.type=basic' \
70+ --ulimit nofile=65536:65536 \
71+ --ulimit memlock=-1:-1 \
72+ --name='es1' \
73+ -d \
74+ -p $INPUT_HOST_PORT:$INPUT_CONTAINER_PORT \
75+ -p $INPUT_HOST_NODE_PORT:$INPUT_NODE_PORT \
76+ -e discovery_type=$INPUT_DISCOVERY_TYPE \
77+ elasticsearch:$INPUT_ELASTICSEARCH_VERSION
78+
79+ # Check if Elasticsearch is up and running
80+ for i in {1..120}; do
81+ if curl --silent --fail http://localhost:9200; then
82+ echo "Elasticsearch is up and running"
83+ exit 0
84+ fi
85+ echo "Waiting for Elasticsearch to be ready..."
86+ sleep 1
87+ done
88+ echo "Elasticsearch did not become ready in time"
89+ exit 1
0 commit comments