Variable expansion in image and repo name - drone.io

Am using drone docker plugin to build and publish docker images, while building the docker image, i want refer some of the env variables exposed by Drone in drone docker plugin repo and tags parameters.
What is needed
repo = first three characters of DRONE_COMMIT_BRANCH variable value - like ${DRONE_COMMIT_BRANCH:0:3}, not working
tags = first seven characters of DRONE_COMMIT_SHA variable value, like ${DRONE_COMMIT_SHA:0:7}, working
image = should be according to above repo and tags like,
Eg:- image: registry/repo:tag
image: registry/${DRONE_COMMIT_BRANCH:0:3}:${DRONE_COMMIT_SHA:0:7}
Concerns
I can use any env variable exposed by Drone for such purpose, or only
specific is only can be used?
drone docker plugin repo parameter supports env variable
expansion and string manipulation, as supported by tags
parameter?
image parameter support variable expansion and string manipulation?
Drone YAML file
Please suggest me how to achieve this use case?
Thanks in advance.

Related

Change environment variable value of react app using azure devops - Deployment on aws (s3)

I have a react application in which they are getting backend api address by using Environment variable. Below in the example:
this._baseUrl = process.env.API_GATEWAY;
In local development environment, development team create .env. file and set environment variable value in that file, to call backend api and every things work fine, like below.
API_GATEWAY=http://localhost:3000
When i create CI/CD pipeline for same project then every things works fine and application is also successfully deployed on AWS (s3 bucket) but i am not able to change the value of environment variable while building the project using npm, like below:
- script: |
npm run build
displayName: 'npm build'
env:
API_GATEWAY: $(envAppApi)
API_GATEWAY used above is the name of environment variable used in code and $(envAppApi) is variable defined in variable group.
But when application is deployed on AWS then environment variable value not changed and it shows below error.
mutation.js:106 ReferenceError: process is not defined
at new e (http-api.ts:17:42)
at Function.value (http-api.ts:24:12)
at Object.mutationFn (Auth.ts:13:26)
at Object.fn (mutation.js:132:31)
at c (retryer.js:95:31)
at new u (retryer.js:156:3)
at t.executeMutation (mutation.js:126:20)
at mutation.js:86:20
(http-api.ts:17:42) => This is the same line where API_GATEWAY environment variable is set and already showed above.
Problem statement:
Is there is any way that we can update the value of environment variable while creating CI/CD pipeline? so the application run successfully. Thanks.
Note: I don't want to use .env. file in my pipeline for updating environment values in react application.
Is there is any way that we can update the value of environment variable while creating CI/CD pipeline?
Yes. I suggest that you can use RegEx Match & Replace task from RegEx Match & Replace.
This task will use regular expressions to match fields in the file.
Here is an example:
steps:
- task: RegExMatchReplace#2
displayName: 'RegEx Match & Replace'
inputs:
PathToFile: test.js
RegEx: 'this._baseUrl = ([a-zA-Z]+(\.[a-zA-Z]+)+)_[a-zA-Z]+;'
ValueToReplace: ' this._baseUrl = $(envAppApi)'
Then the value will update.
You can use this site to convert the regular expressions : Regex Generator

How to pass the Azure DevOps variables into Postman tests?

I have a Postman collection exported as json and integrated into Azure DevOps pipelines. It currently uses a URL variable from the Postman environment exported as json.
I need to set the URL variable in Azure DevOps and use it in my Postman tests.
I have set the variable in the Command Line task for running tests (see the screenshot).
Question 1. I'm not sure if this is the correct place to set this variable.
Question 2. How do I pass the Azure variable into the Postman tests?
In addition to running postman test with a script, I also recommend that you use this out-of-the-box task:Newman the cli Companion for Postman from extension: Newman the cli Companion for Postman.
You can directly set the global variable in this task.
Yaml sample:
steps:
- task: NewmanPostman#4
displayName: 'Newman - Postman'
inputs:
collectionFileSource: 'test1.postman_collection.json'
environment: 'test.postman_environment.json'
globalVars: 'aa=azuretest23'
ignoreRedirect: false
bail: false
sslInsecure: false
htmlExtraDarkTheme: false
htmlExtraLogs: false
htmlExtraTestPaging: false
You can hard code global variables directly, or you can use pipeline variables(e.g. aa=$(var))
Using this task may make it easier for you to set environment variables and global variables.
Update:
Here is my example:
In Release Pipeline:
You could test the same settings in Postman and check if it could work.
In Postman:
testurl is global variable. version is environment variable.
Environment Variables is a correct place if you inside of you scripts use syntax like $env:url (for powershell for instance) and from what I found this is not possible. Please check here
But you can pass variable like it is shown here or here
newman run -g environment.json --global-var "foo=$(YOURVARIABLE)"
So you should go to variables tab and there define this variable and use as above ot paste url instead of $(YOURVARIABLE)

Drone.io secrets not populating in yml appropriately and documentation seems inaccurate

I am running version 0.8.4 as a container in my lab. CLI is also at version 0.8.4
I am trying to use a secret in a command one of my containers is trying to run.
Following the documentation has me needing to sign a repo to allow the job to consume the secret. The drone CLI does not seem to have a
drone sign command for me to run. So I create the secret with a --skip-verify=true flag. This creates the secret but when I run the job it errors out. The output in the UI shows a blank space where the secret should be injected.
Here is an excerpt of my .drone.yml where I am trying to inject secrets -s production -u ${cf_user} -p ${cf_password} --s
I have tried all the following ways to create a secret:
drone secret add <repo_name> --name <key> --value <value> --skip-verify=true
drone secret add <repo_name> --name <key> --value <value>
GUI Creation
I notice when I create an all capital name value the UI represents the value in all lowercase when the CLI shows it in capitals.
I also notice that if I include hyphens in the name and try to use that in my drone.yml the job errors out immediately with a bad substitution error.
Any help understanding what I am doing wrong would be much appreciated!
I got lost in the different documentation available. Should have been looking here rather than secret-guide.
In case I am not alone, I needed to add a secrects block in my pipeline.
I also needed to access them with $SECRET_KEY rather than ${SECRET_KEY}
pipeline:
publish:
image: governmentpaas/cf-cli
secrets: [ cf_user, cf_password ]
Just a little update on this one, I stumbled over it as well because the docs are inconsistent.
In the 0.8.5 version the only thing I had to do is:
add secrets via CLI or UI
add secrets array to utilise it
no need to pass variables to environment.

Declaring variables in docker-compose

I want to let my application know which image version it is running on.
The idea was to pass the Docker image tag into the image as an environment variable. However I don't want to change the version number in both the image line AND in the ENV variable line all the time.
Example:
version: "3"
VERSION=0.2.3
services:
app:
image: myimage:$VERSION
environment:
- APPLICATION_VERSION:$VERSION
Is it possible to declare variables in order to update all values together or is there any other solution available?
You cannot define $VERSION inside the docker-compose.yml.
You have two options for this:
define it in a .env file
send as a command line argument when you run the docker-compose command. e.g. VERSION=0.2.3 docker-compose up -d

Travis CI: how to use repository variables in travis.yml

I tried to use an enviromnet variable in my travis.yml, but all I get is an empty string. I have added a repository variable DEPLOY_KEY with some value to my repository settings, now I try to access it like this:
after_success:
- "curl -H 'Content-Type: application/json;' -X POST -d '{\"api-key\": $DEPLOY_KEY, \"branch\": $TRAVIS_BRANCH}' https://some.where/deploy"
I expected $DEPLOY_KEY to return my key, but instead it returns only an empty string, even though Travis does export DEPLOY_KEY=[secure] when running the build.
I think I need to add something like this to my travis.yml:
env:
- secret: "..."
But my problem is, what is "..." exactly? Is it my repositories public key? I can't find any information on how to use repository variables inside my travis.yml in the docs.
Some solutions suggested that encrypted variables could be used instead, but then, why give me the ability to set repository variables in the first place?
The documentation at https://docs.travis-ci.com/user/encryption-keys/ shows how to use encrypted environment variables.
According to that document the "..." contains both the name of the environment variable and the value of the environment variable. You will need to to use the travis CLI tool to create that secret value. The command to generate that secret looks like travis encrypt SOMEVAR="secretvalue"