How to pass a Github Secret as Environment Variable to Docker? - docker

I'm getting started with CI/CD and Docker and i wanted to pass a connection string to docker in my workflow file.
deploy:
runs-on: ubuntu-latest
needs: publish
steps:
- name: deploy to server
uses: appleboy/ssh-action#master
env:
CONN_STRING: ${{ secrets.CONN_STRING }}
with:
host: ${{ secrets.SECRET_IP }}
username: ${{ secrets.SERVER_USERNAME }}
key: ${{ secrets.SERVER_KEY }}
port: 22
script: docker stop *** && docker rm **** && docker pull **** && docker run --env CONN_STRING=$CONN_STRING -d --name ******
As you can see i made an env called "CONN_STRING" which gets the connection string out of my github secrets. After that i want to pass it into the dockerscript by "CONN_STRING=$CONN_STRING". However my docker keeps crashing since I've added this. Anyone knows what I'm doing wrong?
The **** are merely names of my project, which i'd like to keep private.

You can add arg after FROM step:
ARG CONN_STRING
ENV connection_string=$CONN_STRING
and then pass it to a docker build command '--build-arg CONN_STRING=$CONN_STRING'
and then later in dcoker file you can refer to connection string as this ${connection_string}

Turns out you can just skip the environment variable in yml and use
CONN_STRING=${{ secrets.CONN_STRING }}

Related

How to pass a variable from GitHub action to Docker container?

When I push a commit with a tag, a docker container is automatically built by the docker/build-push-action#v1 GitHub Actions. The tag indicates the version number, which I want to show in the application.
Here is my workflow:
- name: Push to Docker Hub - develop
uses: docker/build-push-action#v1
with:
repository: my_repo/my_image
path: frontend/
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
tags: develop
args: SOFTWARE_VERSION=${{ steps.vars.outputs.tag }}
At that moment the variable SOFTWARE_VERSION should be available in the Dockerfile, is that right?
In my Dockerfile I set:
ARG SOFTWARE_VERSION
ENV SOFTWARE_VERSION ${SOFTWARE_VERSION}
Inside the container, the variable has an empty value. How to do it properly?
I don't know if it matters, but I run the container using docker-compose.
You are using args to pass the arguments.
However, the #v1 of https://github.com/docker/build-push-action offers build_args and #v3 build-args.
You should choose accordingly for the version that you need to use. IMO, you should go for the latest one.
Go to your Settings->Secrets and Variables
Add two Secrets
DOCKERHUB_USERNAME your username on Dockerhub
DOCKERHUB_TOKEN this is the token,you can get it from DockerHub
You must add this to precede Build and Push
— name: Login to DockerHub
if: GitHub.event_name != 'pull_request'
uses: docker/login-action#v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

Github Actions deploy to environment using Docker

I want to deploy to an enviroment using a docker image, but using the .yml below the process gets 'stuck' because of the run command, which supposed to run the docker container, how can I make sure the action ends but the enviroment 'staging' runs the container?
on:
push:
branches: [ master ]
jobs:
staging:
# The type of runner that the job will run on
runs-on: ubuntu-latest
environment:
name: staging
url: https://website.com
env:
DOCKERHUB_REPOSITORY: ${{ secrets.DOCKERHUB_REPOSITORY }}
DOCKERHUB_REPOSITORY_FULL: ${{ secrets.DOCKERHUB_REPOSITORY_FULL }}
steps:
- uses: actions/checkout#v2
- name: docker login
env:
DOCKERHUB_USER: ${{ secrets.DOCKERHUB_USER }}
DOCKERHUB_ACCESS_TOKEN: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }}
run: |
docker login -u $DOCKERHUB_USER -p $DOCKERHUB_ACCESS_TOKEN
- name: docker build
run: |
docker build -t $DOCKERHUB_REPOSITORY .
docker tag $DOCKERHUB_REPOSITORY $DOCKERHUB_REPOSITORY_FULL
- name: docker push
run: |
docker push $DOCKERHUB_REPOSITORY_FULL
- name: docker run
run: |
docker run --network="host" $DOCKERHUB_REPOSITORY
I tried removing the run command alltogether from the .yml but that's just made the .yml to run successfully but without a running enviroment.

SSH host and port using github actions

I would like to deploy my application with a CI CD pipeline. I used appleboy/ssh-action#master and github actions. I generated an ssh key pair in git bash and I added the the pub file to my profile and the private key to secrets in the repo. The problem is that I don't know the hostname and the port number but I need it to deploy. Can somebody help me? I don't have any experience with this.
This is my github actions yaml file:
name: Deploy application
on:
push:
branches: [master]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: multiple command
uses: appleboy/ssh-action#master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
script: |
docker system prune -a -f
cd /mnt/tourmix-main
git clean -ffdx
git pull origin master --rebase
make release
docker system prune -a -f

Github Actions: Push docker image build by gradle task

For building images of my current project, I use the gradle task bootBuildImage. This task creates a OCI image using Cloud Native Buildpacks.
- name: Build image with Gradle
run: ./gradlew bootBuildImage
With the next step I'm trying to push this docker image to my private GitHub registry using build-push-action.
- name: Push image to Registry
uses: docker/build-push-action#v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
registry: docker.pkg.github.com
repository: sullrich84/wettkampfdb-backend
tags: latest
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
As I can tell from the logs, the problem with this step is that it seems to rely on a Dockerfile located in the workspaces root directory which does not exist.
unable to prepare context: unable to evaluate symlinks in Dockerfile path:
lstat /github/workspace/Dockerfile: no such file or directory
Is it possible to push the image created via bootBuildImage to my private GitHub registry without using/creating a dedicated Dockerfile?
If you are just looking for something to deal with docker push, you can just use the native docker command to do it.
Something like this.
- name: run docker push
run: |
#docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
#docker push $BUILD_TAG
#docker push $LATEST_TAG
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
The github-action you are using is not for pushing an image you define by repository and tag but rahter build and push https://github.com/docker/build-push-action#build-push-action
Builds and pushes Docker images and will log in to a Docker registry if required.
Specifically this is also related to https://github.com/docker/build-push-action/issues/17 - so just building without pushing is possible, not vice versa.
This github action does yet not allow just pushing.
This is for now very common for a lot of CI/CD solutions, where build and push are one task.
I use publishRegistry option of gradle bootBuildImage.
Set parameter in your build.gradle (below is gradle.kts)
tasks.bootBuildImage {
imageName = "${imageName}:${project.version}"
isPublish = true
docker {
publishRegistry {
url = dockerUrl
username = dockerUsername
password = dockerPassword
}
}
}
check this document

How to pull a Docker image in Github actions for a compute engine VM?

Using GH actions I'm building and pushing an image to my docker repository. How can this be pulled on a Google compute engine after having completed setup-gcloud:
steps:
- name: setup gcloud
uses: GoogleCloudPlatform/github-actions/setup-gcloud#master
with:
version: '290.0.1'
project_id: ${{ secrets.GCP_PROJECT_ID }}
service_account_key: ${{ secrets.GCP_SA_KEY }}
I am aware of GCE-Github actions and the google/docker-registry. But as my VM is not container-optimized I want to run
docker pull [docker-hub-repo] and perform a docker-compose up in a specific dir afterwards.
I didn't realise that the obvious gcloud compute ssh command is sufficient for this.
- run: gcloud compute ssh --zone $GCE_INSTANCE_ZONE $GCE_INSTANCE --command 'docker login -u [user] -p [password] && docker pull [repository:tag]'
2022 Edit
For better safety, use Github Secrets.
To avoid direct use of the SSH command via run, there is a job appleboy/ssh-action#master for this;
jobs:
deploy:
runs-on: [ubuntu-latest]
steps:
#Job starts here
- name: executing remote ssh commands
uses: appleboy/ssh-action#master
with:
host: ${{ secrets.WEBSPACE_HOST }}
username: ${{ secrets.WEBSPACE_USER }}
password: ${{ secrets.WEBSPACE_PASS }}
#Bash commands may be placed line by line here
script: |
cd ...
git pull
docker-compose up --build --detach
You could install Docker Engine on several Linux Platforms and on macOS and Windows10 through Docker Desktop.
The command "docker pull" pulls image by default from Docker Hub. You could also pull the images from your desired repository by specifying the path of repository.
You need to install Docker Compose so that you could run the command "docker-compose up" which starts compose and runs your entire app.

Resources