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

Fix job services localhost example in workflow-syntax-for-github-actions.md #33621

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

piotrekkr
Copy link

@piotrekkr piotrekkr commented Jun 21, 2024

Why:

Because job services localhost example is wrong and misleading.

What's being changed (if available, include any code snippets, screenshots, or gifs):

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.<service_name>.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

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'] }}

image

Check off the following:

  • I have reviewed my changes in staging, available via the View deployment link in this PR's timeline (this link will be available after opening the PR).

    • For content changes, you will also see an automatically generated comment with links directly to pages you've modified. The comment won't appear if your PR only edits files in the data directory.
  • For content changes, I have completed the self-review checklist.

…ons.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.<service_name>.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'] }}
```
Copy link

welcome bot commented Jun 21, 2024

Thanks for opening this pull request! A GitHub docs team member should be by to give feedback soon. In the meantime, please check out the contributing guidelines.

@github-actions github-actions bot added the triage Do not begin working on this issue until triaged by the team label Jun 21, 2024
Copy link
Contributor

github-actions bot commented Jun 21, 2024

Automatically generated comment ℹ️

This comment is automatically generated and will be overwritten every time changes are committed to this branch.

The table contains an overview of files in the content directory that have been changed in this pull request. It's provided to make it easy to review your changes on the staging site. Please note that changes to the data directory will not show up in this table.


Content directory changes

You may find it useful to copy this table into the pull request summary. There you can edit it to share links to important articles or changes and to give a high-level overview of how the changes in your pull request support the overall goals of the pull request.

Source Preview Production What Changed
actions/using-workflows/workflow-syntax-for-github-actions.md fpt
ghec
ghes@ 3.13 3.12 3.11 3.10 3.9
fpt
ghec
ghes@ 3.13 3.12 3.11 3.10 3.9

fpt: Free, Pro, Team
ghec: GitHub Enterprise Cloud
ghes: GitHub Enterprise Server

@nguyenalex836 nguyenalex836 added content This issue or pull request belongs to the Docs Content team actions This issue or pull request should be reviewed by the docs actions team waiting for review Issue/PR is waiting for a writer's review and removed triage Do not begin working on this issue until triaged by the team labels Jun 21, 2024
@nguyenalex836
Copy link
Contributor

@piotrekkr Thanks so much for opening a PR! I'll get this triaged for review ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actions This issue or pull request should be reviewed by the docs actions team content This issue or pull request belongs to the Docs Content team waiting for review Issue/PR is waiting for a writer's review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants