From 412ce482f4c19678d5df17b482a08c67028d7c08 Mon Sep 17 00:00:00 2001 From: Piotr Date: Fri, 21 Jun 2024 09:14:26 +0200 Subject: [PATCH 1/3] Fix job services localhost example in workflow-syntax-for-github-actions.md Current example states that : ``` When you specify the Docker host port but not the container port, the container port is randomly assigned to a free port. GitHub sets the assigned container port in the ${{job.services..ports}} context. In this example, you can access the service container ports using the ${{ job.services.nginx.ports['8080'] }} and ${{ job.services.redis.ports['6379'] }} contexts. ``` Which is not true because you either specify container port only and then Github Actions will choose random free port on **host**, or you specify both, host and container port. To access host port you should use container port as a key. In `nginx` example it should be `${{ job.services.nginx.ports['80'] }}` and not `8080`. Updated job services yaml example to show more clearly how to access those services. I tested all this using this yaml ```yaml jobs: job-a: runs-on: ubuntu-latest services: nginx: image: nginx # Map port 8080 on the Docker host to port 80 on the nginx container ports: - 8080:80 redis: image: redis # Map TCP port 6379 on Docker host to a random free port on the Redis container ports: - 6379/tcp steps: - run: docker ps - run: echo '${{ toJSON(job.services) }}' - run: | echo "Redis available on 127.0.0.1:${{ job.services.redis.ports['6379'] }}" echo "Nginx available on 127.0.0.1:${{ job.services.nginx.ports['80'] }}" - name: Check Redis run: | echo -e '*1\r\n$4\r\nPING\r\n' | netcat -w1 127.0.0.1 ${{ job.services.redis.ports['6379'] }} ``` --- .../writing-workflows/workflow-syntax-for-github-actions.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/content/actions/writing-workflows/workflow-syntax-for-github-actions.md b/content/actions/writing-workflows/workflow-syntax-for-github-actions.md index 2df60398d6bf..74684447a8de 100644 --- a/content/actions/writing-workflows/workflow-syntax-for-github-actions.md +++ b/content/actions/writing-workflows/workflow-syntax-for-github-actions.md @@ -1002,7 +1002,7 @@ For more information about the differences between networking service containers ### Example: Using localhost -This example creates two services: nginx and redis. When you specify the Docker host port but not the container port, the container port is randomly assigned to a free port. {% data variables.product.prodname_dotcom %} sets the assigned container port in the {% raw %}`${{job.services..ports}}`{% endraw %} context. In this example, you can access the service container ports using the {% raw %}`${{ job.services.nginx.ports['8080'] }}`{% endraw %} and {% raw %}`${{ job.services.redis.ports['6379'] }}`{% endraw %} contexts. +This example creates two services: nginx and redis. When you specify the container port but not the host port, the host port is randomly assigned to a free port on host. {% data variables.product.prodname_dotcom %} sets the assigned host port in the {% raw %}`${{job.services..ports}}`{% endraw %} context. In this example, you can access the service host ports using the {% raw %}`${{ job.services.nginx.ports['80'] }}`{% endraw %} and {% raw %}`${{ job.services.redis.ports['6379'] }}`{% endraw %} contexts. ```yaml services: @@ -1016,6 +1016,10 @@ services: # Map TCP port 6379 on Docker host to a random free port on the Redis container ports: - 6379/tcp +steps: + - run: | + echo "Redis available on 127.0.0.1:${{ job.services.redis.ports['6379'] }}" + echo "Nginx available on 127.0.0.1:${{ job.services.nginx.ports['80'] }}" ``` ## `jobs..services..image` From d2b16a3a8a1cc1d363e51aa431e441f360df8eb3 Mon Sep 17 00:00:00 2001 From: Piotr Date: Fri, 21 Jun 2024 09:36:56 +0200 Subject: [PATCH 2/3] Wrap contexts in raw tags --- .../writing-workflows/workflow-syntax-for-github-actions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/actions/writing-workflows/workflow-syntax-for-github-actions.md b/content/actions/writing-workflows/workflow-syntax-for-github-actions.md index 74684447a8de..bf092cf67d3c 100644 --- a/content/actions/writing-workflows/workflow-syntax-for-github-actions.md +++ b/content/actions/writing-workflows/workflow-syntax-for-github-actions.md @@ -1018,8 +1018,8 @@ services: - 6379/tcp steps: - run: | - echo "Redis available on 127.0.0.1:${{ job.services.redis.ports['6379'] }}" - echo "Nginx available on 127.0.0.1:${{ job.services.nginx.ports['80'] }}" + echo "Redis available on 127.0.0.1:{% raw %}${{ job.services.redis.ports['6379'] }}{% endraw %}" + echo "Nginx available on 127.0.0.1:{% raw %}${{ job.services.nginx.ports['80'] }}{% endraw %}" ``` ## `jobs..services..image` From f3c9891f98cd93f987fa9e2346181d7101da0d9a Mon Sep 17 00:00:00 2001 From: Piotr Date: Fri, 21 Jun 2024 23:15:43 +0200 Subject: [PATCH 3/3] Update comments --- .../writing-workflows/workflow-syntax-for-github-actions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/actions/writing-workflows/workflow-syntax-for-github-actions.md b/content/actions/writing-workflows/workflow-syntax-for-github-actions.md index bf092cf67d3c..d3c77feb77ed 100644 --- a/content/actions/writing-workflows/workflow-syntax-for-github-actions.md +++ b/content/actions/writing-workflows/workflow-syntax-for-github-actions.md @@ -1013,7 +1013,7 @@ services: - 8080:80 redis: image: redis - # Map TCP port 6379 on Docker host to a random free port on the Redis container + # Map random free TCP port on Docker host to port 6379 on redis container ports: - 6379/tcp steps: