skip to Main Content

I’m trying to deploy Django on AWS EC2 with AzureDevops pipeline. I have a ssh task to run docker container and I would like to set environment variables. I have tried various commands, but I can’t get it.

Here’s the task:

- task: SSH@0
  displayName: Restart Docker Container
  inputs:
    sshEndpoint: "TMC AWS EC2 Stage Authentication"
    runOptions: "inline"
    inline: |
      echo $(DEBUG)
      echo $(export DEBUG=$(DEBUG))
      ...
      sudo docker stop $(containerName)
      sudo docker rm $(containerName)
      sudo docker run -d -p 80:8000 --name $(containerName) $(AWS_ECR_REPOSITORY_NAME)/${{ parameters.imageName }}:${{ parameters.environment }}-latest

2

Answers


  1. Chosen as BEST ANSWER

    I have solved passing environment variables file to docker run command.

    - task: SSH@0
      displayName: Restart Docker Container
      inputs:
        sshEndpoint: "TMC AWS EC2 Stage Authentication"
        runOptions: "inline"
        inline: |
          touch .env
          echo SECRET_KEY=$(SECRET_KEY) >> .env
          echo DEBUG=$(DEBUG) >> .env
          ...
          sudo docker stop $(containerName)
          sudo docker rm $(containerName)
          sudo docker run -d -p 80:8000 --env-file=.env --name $(containerName) $(AWS_ECR_REPOSITORY_NAME)/${{ parameters.imageName }}:${{ parameters.environment }}-latest
    

  2. According to the documentation you can pass arguments if you use ‘script’ as your runOptions.

    - task: SSH@0
      inputs:
        sshEndpoint: 'TMC AWS EC2 Stage Authentication'
        runOptions: 'script'
        scriptPath: 'myscript.sh'
        args: '$(containerName)'
        readyTimeout: '20000'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search