is it possible to use static ip when using github actions

Viewed 2874

Now I am using github actions as my project CI, I want to do some unit test when using github actions build my project. When I using unit test, the project must using database, now my database have a white list and only IP in white list could connect my database, but now when I run unit test in GitHub Actions, I did not know the GitHub Actions IP address. Is it possible to use a static ip or any other way to solve the problem? I am not want to any IP could connect my database, it may have a security problem. any suggestion?

4 Answers

This is currently only possible with a self-hosted runner on a VM you can control the IP address of.

See also:

Alternatively, your GitHub action workflow may be able to adjust the firewall settings as part of the run.

Or you could use something like SQL Server LocalDB or SQLLite to connect to the database locally on the runner. Or spin up a temporary DB in a cloud environment, open it up to the runner and throw it away afterwards.

Or you could use a VPN client to connect to actions runner to your environment. You can install anything you want on the runner.

You can dynamically retrieve the GitHub Actions runner's IP address during your workflow using the public-ip action and update your RDS instance's security group ingress rules before and after your unit test steps.

This will allow you to use GitHub's hosted runners with your workflow instead of hosting your own.

Note: You will need to also set AWS credentials on your runner with permissions to update the associated security group. Also, you need to make sure the RDS instance is in a public subnet with an Internet Gateway attached and security group attached to it.

Your workflow should look something like this:

deploy:
    name: deploy
    runs-on: ubuntu-latest
    env:
      AWS_INSTANCE_SG_ID: <your-rds-subnet-sg-id>
    steps:
      - name: configure aws credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: <your-ci-aws-access-key>
          aws-secret-access-key: <your-ci-aws-secret-key>
          aws-region: <your-rds-aws-region>
      - name: get runner ip addresses
        id: ip
        uses: haythem/public-ip@v1.2
      - name: whitelist runner ip address
        run: |
          aws ec2 authorize-security-group-ingress \
            --group-id $AWS_INSTANCE_SG_ID \
            --protocol tcp \
            --port 22 \
            --cidr ${{ steps.ip.outputs.ipv4 }}/32
      - name: connect to your rds instance and run tests
        run: |
          ...run tests...
      - name: revoke runner ip address
        run: |
          aws ec2 revoke-security-group-ingress \
            --group-id $AWS_INSTANCE_SG_ID \
            --protocol tcp \
            --port 22 \
            --cidr ${{ steps.ip.outputs.ipv4 }}/32

Ideally though you would run your integration tests in an EC2 within the same VPC as your RDS instance to avoid publicly exposing your RDS instance.

This is in beta (September 1, 2022) but it is possible to assign static IP address to runners:

Fixed IP ranges to provide access to runners via allow list services

Setup a fixed IP range for your machines by simply ticking a check box, this provides an IP range that can be allow listed in internal systems and in GitHub’s allow list to keep using Actions while making your GitHub environment more secure. Blockquote

More details here

If your database happens to be Redis or PostgreSQL, GitHub Actions includes a built-in feature called Service Containers to spin up an ephemeral database in CI for testing purposes.

These databases are short-lived: after your job that uses it completes, the service container hosting the database is destroyed. You can either run the database in a container or directly on the virtual machine if desired.

For more info, see Creating PostgreSQL service containers in the GitHub Actions docs.

If you happen to be using another database, you can install do some more manual legwork to install and run it yourself.

Related