Travis.ci - Build and Deploy based PR and Tags - travis-ci

I'm trying to create two different actions within travis.ci. The first action is to execute a script on every push on every branch. This is currently working as desired. The second is to trigger a different script only when git push origin --tags. In short:
Execute script1 always (currently working)
Execute script2 when tags are pushed
Here is what I'm trying:
language: python
python:
- 3.7
matrix:
include:
- python: 3.7
sudo: true
install:
- pip install -r requirements.txt
script: # Always want this to happen
- invoke package
branches:
only:
- master
- /^x\/.*/
deploy: # Want this to occur on git push origin --tags
provider: script
script: invoke release
on:
tags: true
The deploy section is not being triggered, and I can find no evidence of the invoke release script being invoked.
Update:
It may be due to the way I'm pushing tags..? I'm seeing this log in travis now:
Skipping a deployment with the script provider because this is not a tagged commit

Solved it from this github issue. Changed the deploy section to this:
deploy:
provider: script
script: invoke release
on:
tags: true
all_branches: true
but had to remove the branches section. Deployment script was invoked, nonetheless.

Related

Codefresh git-commit with husky yarn pre-commit

We are using the git-commit Codefresh step to push updates during a build. We use husky and have pre-commit hooks that rely on yarn. This fails within the step because the dependency is missing.
How should we handle these hooks?
Here is the current step configuration:
push_snapshots:
stage: deploy
title: Commit and push snapshot updates.
type: git-commit
working_directory: ${{clone}}
arguments:
repo: "${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}"
git: github
commit_message: Update snapshot post infra deploy
add:
- "./tenants/*/snapshot/*"
when:
steps:
- name: deploy_infra
on:
- success
and our .husky/pre-commit looks like:
yarn lint-staged
We've tried a couple of things:
Looked for a way to pass the --no-verify flag, but it looks like the only flag supported in the step is --allow-empty, and there is no way to pass arbitrary flags
Tried to override the image with a custom image (that extends git-commit:1.0 and then adds npm and yarn). That image was not actually read by the step, the step just used its usual image.

Daily automatic Bitbucket deploy with manual step when pushed to branch

I'm trying to set up a pipeline in Bitbucket with a daily schedule for two branches.
develop : There a scheduled daily deployment running + when I push to this branch the pipeline runs again
master : This is the tricky one. I want to have a daily deployment because the page need to be rebuild daily, but I would like to have a security that if anyone pushes to this branch by mistake or the code is bad, it only runs the deployment after a manual trigger.
So my question is that is it possible to set up a rule to track if there was a push and in this case let the admin manually start the pipeline ?
pipelines:
branches:
develop:
- step:
name: Deploy staging
deployment: staging
caches:
- node
script:
- npm run staging:auto
- npm install firebase
- npm install firebase-functions
- npm install -g firebase-tools
- firebase deploy --token=$FIREBASE_TOKEN --project $FIREBASE_PROJECT_STAGING --only functions,hosting
artifacts:
- build/**
master:
- step:
name: Deploy to production
deployment: production
caches:
- node
script:
- npm run deploy:auto
- npm install firebase
- npm install firebase-functions
- npm install -g firebase-tools
- firebase deploy --token=$FIREBASE_TOKEN_STAGING --project $FIREBASE_PROJECT_PRODUCTION --only functions,hosting
artifacts:
- build/** ```
I'd suggest to schedule a different custom pipeline other than the one that runs on pushes to the production branch. The same steps definition can be reused with a yaml anchor and you can replace the trigger in one of them.
E.g:
definitions:
# write whatever is meaningful to you,
# just avoid "caches" or "services" or
# anything bitbucket-pipelines could expect
yaml-anchors:
- &deploy-pro-step
name: Deploy production
trigger: manual
deployment: production
script:
- do your thing
pipelines:
custom:
deploy-pro-scheduled:
- step:
<<: *deploy-pro-step
trigger: automatic
branches:
release/production:
- step: *deploy-pro-step
Sorry if I make some yaml mistakes, but this should be the general idea. The branch where the scheduled custom pipeline will run is configured in the web interface when the schedule is set up.

Publishing to NPMJS with Travis CI

I've set up a Travis CI to run a few scripts that should:
Deploy some static pages to Github pages
Deploy an NPM package to npmjs
Item 1 works, Item 2 doesn't.
Here's what my travis.yml file looks like:
language: node_js
node_js:
- '10'
script:
- gulp build
- gulp npmDist
deploy:
- provider: pages
local_dir: dist-site/
skip_cleanup: true
github_token: "$GITHUB_TOKEN"
on:
branch: master
- provider: npm
email: myemail#mydomain.com
api_key:
secure: THE-API-KEY-I-GOT-BY-CREATING-A-TOKEN-ON-NPMJS-AND-ENCRYPTING-IT-USING-TRAVIS-ENCRYPT-COMMAND-IN-TERMINAL
on:
tags: true
repo: githubaccount/reponame
all_branches: true
I trigger the script in two ways:
- When I merge to master, it deploys to GitHub Pages.
- When I create a tag and push to master it should deploy the package to npmjs.
As stated, the first part of the file works, as it actually deploys to GitHub Pages.
Here's the error I get from npmjs:
npm ERR! publish Failed PUT 401
npm ERR! code E401
npm ERR! You must be logged in to publish packages. : package-name
(oh, and a strange thing: Travis returns with a "Build Passed" and the succesful status (green), even though there's obviously something wrong)
Hope this makes sense? Thanx in advance for any help.
Fixed it — instead of having this in the travis.yml-file:
api_key:
secure: THE-API-KEY-I-GOT-BY-CREATING-A-TOKEN-ON-NPMJS-AND-ENCRYPTING-IT-USING-TRAVIS-ENCRYPT-COMMAND-IN-TERMINAL
I changed it to:
api_key: "$NPM_TOKEN"
..and added the NPM Token as an environment variable inside the Travis CI dashboard.
(Still curious as to why it didn't work, but I can't be bothered to do something about, as I've already wasted way too much time on this issue today)
I had the same problem and I just removed all previous keys and generated them again and my code looks like this:
deploy:
provider: npm
email: $NPM_USER
api_key: $NPM_TOKEN
To create your NPM_TOKEN you must:
Go to your npm profile
Tokens
Create Token
Select "Read and Publish" and create it.
Then you can specify it in your env variables for the corresponding project.
The key do not have to be encrypted and the user is your email. That will be it.
You will receive a notification like:
Installing deploy dependencies
dpl.2
Preparing deploy
dpl.3
Deploying application
+ your-artifact#x.x.x

Jenkins Build Trigger with Gitlab Webhook

I am able to generate build trigger url and able to call build operation via Gitlab Web hook.
But the build operation is calling in each commit irrespective of any branch. But I want to trigger build operation for a specific branch commit. Means want to execute build only if any code pushed to a specific branch.
In Gitlab yaml you can specify each job to trigger on certain branches or excluding branches
https://docs.gitlab.com/ee/ci/yaml/#only-and-except
job_name:
script:
- rake spec
- coverage
stage: test
only:
- master
tags:
- ruby
- postgres
allow_failure: true
The above yaml would only execute on master

TravisCI: Run after_success on a specific branch

I would like to know how to run an after_success script only for a specific branch.
I am using a custom script to deploy the app after build passes. I would only like to run this when on prod branch.
So far, I have tried the following:
#1
after_success:
- # some deployment script
on: prod
#2
branches:
only:
- prod
after_success:
- # some deployment script
#3
after_success:
branches:
only:
- prod
- # some deployment script
Any suggestions?
I solved it by writing a simple script using TRAVIS_BRANCH environment variable and executed the script in after_success
.travis.yml
after_success:
- ./deploy.sh
deploy.sh
#!/bin/bash
if [ "$TRAVIS_BRANCH" == "prod" ]; then
// do the deploy
fi
You can also do this by using the script provider in the deploy phase of your build. This approach is a bit cleaner but only allows one command, unlike after_success.
deploy:
provider: script
script: # some deployment script
on:
branch: prod

Resources