How to pass custom environment variables to the Lightsale instance?
bigsolom opened this issue · comments
Hello, is it possible to set custom environment variables to be passed to the Lightsale instance?
To give more context, I'm trying to achieve the following workflow:
- A Docker image will be built for every PR and pushed to Github Container Registry, the image will be tagged with
pr-XXX
where XXX: pull request number - Pull preview action should pull the created docker image for the PR and start the service
Since each PR would have a different image, I changed docker-compose.pullpreview.yml
to read the image as an Environment variable
docker-compose.pullpreview.yml
version: "3.8"
web:
image: ${IMAGE:?err}
Ideally this environment variable should be set from Github action since there it's known what's the PR number and accordingly we can construct the image name
Github action (pullpreview.yml)
- name: Pullpreview action
uses: pullpreview/action@v5
with:
registries: docker://${{ github.repository_owner }}:${{ secrets.GITHUB_TOKEN }}@ghcr.io
env:
IMAGE: ghcr.io/org/repo:pr-${{ github.event.number }}
But unfortunately this doesn't work, I always get this error:
parsing /app/docker-compose.pullpreview.yml: error while interpolating services.web.image: required variable IMAGE is missing a value: err
I also checked on the Lightsale instance, and the env variable IMAGE
is not set
ssh ec2-user@x.x.x.x
printenv
P.S. I only included snippets of docker-compose.pullpreview.yml
and pullpreview.yml
that are relevant, if you need more details please let me know.
Thanks,
Eslam
@bigsolom Hello! When the workflow starts, the pullpreview action runs on the GitHub runner, not on the lightsail instance. As such, any environment variable that you set on the action will only be seen from the GitHub runner.
2 ways to achieve your objective:
- add a step before the pullpreview action, which simply replaces the
image
value usingsed
, e.g.
steps:
- name: Configure pullpreview
env:
MY_IMAGE: ...
run: sed -i "s|image: REPLACE_ME|image: $MY_IMAGE|" docker-compose.pullpreview.yml
- name: PullPreview
uses: pullpreview/action@v5
...
- generate a compose file override with just the correct image name, and add that additional compose file to the
compose_files
input of the pullpreview action. Compose will then merge all the files at runtime:
steps:
- name: Configure pullpreview
env:
MY_IMAGE: ...
run: |
echo "services: { my_service: { image: $MY_IMAGE } }" > docker-compose.pullpreview-override.yml
- name: PullPreview
uses: pullpreview/action@v5
with:
compose_files: docker-compose.pullpreview.yml,docker-compose.pullpreview-override.yml
...
@bigsolom did it help?
Thanks for your answer @crohr , Yes it did help! I went for the first option "Replacing file contents using sed
"
Should I close this issue now? or do you want to keep it open?
Great to know this helped. I will close the issue now.