Remove auto trigger from ECR as Source in AWS CodePipeline - devops

I have a pipeline with Source stage which reads from ECR. For every image pushed to ECR my pipeline is triggered automatically. I don't want this behavior and would like to start my pipeline manually from Release Change button. How to achieve this?
I managed to achieve the same for GitHub Source Stage by removing the webhook from GitHub repo itself. Unable to find similar web-hook for ECR.

CodePipeline uses CloudWatch rules when the source is configured as ECR to start its execution on an Image Push.
To verify this, you can check the associated CloudWatch rule [1] and look if the event pattern currently set on your rule matches an event on ECR Image Push. Also you can refer to this sample Event for a Completed Image Push [2] to see the available attributes for filtering.
As a general guidance, you might want to check the following links that will walk through the process of using ECR as a Source on CodePipeline[3].
[1] Use CloudWatch Events to Start a Pipeline (Amazon ECR Source) - https://docs.aws.amazon.com/codepipeline/latest/userguide/create-cwe-ecr-source.html
[2] Amazon ECR Events and EventBridge - Sample Events from Amazon ECR - https://docs.aws.amazon.com/AmazonECR/latest/userguide/ecr-eventbridge.html#ecr-eventbridge-bus
[3] AWS DevOps Blog - https://aws.amazon.com/pt/blogs/devops/build-a-continuous-delivery-pipeline-for-your-container-images-with-amazon-ecr-as-source/

Related

Auto create Jenkins job from source code repo - no jenkins interaction

I am looking to auto create jobs in jenkins upon pull request , branches, master push etc similar to what we do in Gitlab. My SCM is butbucket here.
I have so far setup docker based agent integration with Jenkins and butbucket, when I create a job and configure it to use repo it all works fine , but I just want to remove altogether a step of job creation in jenkins and want the workflow like this:
In butbucket source code repo to keep all pipeline configuration for and branch and tag to trigger Jenkins pipeline without touching Jenkins for job creation or any config creation. Just want to drive all via the script in code repo for pipeline .
Any recommendations or help for workflow would be appreciated
I got the answer to my Question , hence listing the steps for very simple use case for how it would work.
Steps:
Go to bitbucket server repository to which you want to enable pull request based trigger. Add Post Recieve Hook to this repository "Webhook to Jenkins for Bitbucket Server" , Enable this hook to have connection to jenkins. Enabling this plugin will issue POST request to jenkins each time a new Pull request is opened.
On Jenkins Server this will work with Blue Ocean Pipeline which by default will pick the change for pull request branch and trigger the job on each pull request.
Blue Ocean pipeline will by default create multi branch pipeline job to work with bitbucket repository.

CI/CD for new ECS task Definations

I have Jenkins pipeline which builds docker image of spring boot application and push that image to AWS ECR.We have created ECS cluster which takes this image from ECR repository and runs container using ECS task and services.
We have created ECS cluster manually.But now i want whenever a new image is pushed by my CICD to ECR repository it should take the new image and create new task definition and run automatically.What are the ways to achieve this ?
But now i want whenever a new image is pushed by my CICD to ECR
repository it should take the new image and create new task definition
and run automatically.What are the ways to achieve this ?
As far this step is a concern, it would more easy to do with code pipeline as there is no out of the box feature in Jenkins which can detect changes in ECR image.
The completed pipeline detects changes to your image, which is
stored in the Amazon ECR image repository, and uses CodeDeploy to
route and deploy traffic to an Amazon ECS cluster and load balancer.
CodeDeploy uses a listener to reroute traffic to the port of the
updated container specified in the AppSpec file. The pipeline is also
configured to use a CodeCommit source location where your Amazon ECS
task definition is stored. In this tutorial, you configure each of
these AWS resources and then create your pipeline with stages that
contain actions for each resource.
tutorials-ecs-ecr-codedeploy
build-a-continuous-delivery-pipeline-for-your-container-images-with-amazon-ecr-as-source
If you are looking for this thing in Jenkins, then you have to manage these things at your end.
Here will be the step
Push image to ECR
re-use the image name and Create Task definition in your jenkins job using aws-cli or ecs-cli with same image name
Create service with new task definitioni
You can look for details here
set-up-a-build-pipeline-with-jenkins-and-amazon-ecs
We ended up to the same conclusion, as there was no exact tooling matching this scenario. So we developed a little "glueing" tool from fee others open-source ones, and recently open-sourced as well:
https://github.com/GuccioGucci/yoke
Please have a look, since we're sharing templates for Jenkins, as it's our pipeline orchestrator as well.

Jenkins doesn't get notified and doesn't buil when user raise pull request from Bitbucket clou

I have created Jenkins server(v 2.138.3) with bitbucket cloud on ubuntu 16. Jenkins couldn't trigger the build when user raise pull request from bitbucket cloud (Not bitbucket server). But in my project it trigger the build when user commit the code by git command. I followed below link for trigger the build on pull request. But they used bitbucket server. Some options are not available in bitbucket cloud.
https://mohamicorp.atlassian.net/wiki/spaces/DOC/pages/209059847/Triggering+Jenkins+on+new+Pull+Requests
Can any one tell me how to do trigger the build when user raise pull request from bitbucket cloud? and I too stuttering to attach the Html documents of report in mail notification. Here I have attached my screen shot of Jenkins and bitbucket cloud webhook. Please help me to find the exact solution.
[Image of Bitbucket pull request builder][1]
[Image Of Poll SCM][2]
[bitbucket_Webhook][3]
[SCM][4]
[1]: https://i.stack.imgur.com/Rtx1i.png
[2]: https://i.stack.imgur.com/HvRA6.png
[3]: https://i.stack.imgur.com/HT3dZ.png
[4]: https://i.stack.imgur.com/LEYn7.png
You can try Stash Pull Request Builder to perform this. It worked for me.
Dont forgot to add In advance options :-
Advanced -> Refspec:
+refs/pull-requests/*:refs/remotes/origin/pr/*
Branch Specifier:
origin/pr/${pullRequestId}/from
SCM Config will be like below :-
Trigger Config:-

Docker trigger jenkins job when image is pushed

I am trying to build a jenkins job(trigger builds remotely) on docker image build, build all I am getting on docker hub is following:
HISTORY
ID Status Date & Time
7345... ! ERROR 10/12/17 10:03
Reason (I assume): Docker is not authenticated to post to the jenkins url.
Question: How can I trigger the job automatically when an image gets pushed to docker hub?
Pull and run Watchtower docker image to poll any third-party public Docker image on Docker Hub or Quay that you need (typically as a base image of your own containers). Here's how. "Polling" here does not imply crudely pulling the whole image every 5 minutes or so - we are monitoring periodically for changes in the image, downloading only the checksum (SHA digest) most of the time (when there are no changes in the locally cached image).
Install the Build Token Root Plugin in your Jenkins server and set it up to receive Slack-formatted notifications secured with a token to trigger builds remotely or - safer - locally (those triggers will be coming from Watchtower container, not Slack). Here's how.
Set up Watchtower to post Slack messages to your Jenkins endpoint upon every change in the image(s) (tags) that you want. Here's how.
Optionally, if your scale is so large that you could end up overloading and bringing down the entire Docker Hub with a flood HTTP GET requests (should the time triggers go wrong and turn into a tight loop) make sure to build in some safety checks on top of Watchtower to "watch the watchman".
You can try the following plugin: https://wiki.jenkins.io/display/JENKINS/CloudBees+Docker+Hub+Notification
Which claims to do what you're looking for.
You can configure a WebHook in DockerHub wich will trigger the Jenkins-Build.
Docker Hub webhooks targeting your Jenkings server endpoint require making periodic copies of the image to another repo that you own [see my other answer with Docker Hub -> Watchman -> Jenkins integration through Slack notifications].
More details
You need to set up a cron job with periodic polling (docker pull) of the source repo to [docker] pull its `latest' tag, and if a change is detected, re-tag it as your own and [docker] push to a repo you own (e.g. a "clone" of the source Docker Hub repo) where you have set up a webhook targeting your Jenkings build endpoint.
Then and only then (in a repo you own) will Jenkins plugins such as Docker Hub Notification Trigger work for you.
Polling for Dockerfile / release changes
As a substitute of polling the registry for image changes (which need not generate much network traffic thanks to the local cache of docker images) you can also poll the source Dockerfile on Github using wget. For instance Dockerfiles of the official Docker Hub images are here. In case when the Github repo makes releases, you can get push notifications of them using Github Watch > Releases Only feature and if they have CI docker builds. Docker images will usually be available with a delay after code releases, even with complete automation, so image polling is more reliable.
Other projects
There was also a proposal for a 2019 Google Summer of Code project called Polling Docker Registries for Image Changes that tried to solve this problem for Jenkins users (incl. apparently Google), but sadly it was not taken up by participants.
Run a cron job with a periodic docker search to list all tags in the docker image of interest (here's the script). Note that this script requires the substitution of the jannis/jq image with an existing image (e.g. docker run --rm -i imega/jq).
Save resulting tags list to a file, and monitor it for changes (e.g. with inotifywait).
Fire a POST request using curl to your Jenkins server's endpoint using Generic Webhook Trigger plugin.
Cautions:
for efficiency reasons this tags listing script should be limited to a few (say, 3) top pages or simple repos with a few tags,
image tag monitoring relies on tags being updated correctly (automatically) after each image change, rather than being stuck in the past, like say Ubuntu tags (e.g. trusty-20190515 was updated a few days ago - late November, without the change in its mid-May tag).

Trigger Jenkins job when a S3 file is updated

I'm looking for a way to trigger my Jenkins job whenever a file is created or updated in S3.
I can't seem to find anything by usual means of search. It is always upload artifacts to S3, but rarely download and even then I can't seem to find a way to trigger of the actual update process.
The only way I currently can figure out how to do this at all, would be to sync the file periodically and compare the hash to previous versions, but that is a really terrible solution.
The idea behind this would be to have an agency (which does not have access to our Jenkins) upload their build artifacts and to trigger a deployment from that.
You can use a combination of SNS Notifications for new artifacts in the S3 bucket https://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html and the Jenkins AWS SQS plugin to trigger a build (https://github.com/jenkinsci/aws-sqs-plugin)
A little bit of manual configuration is required in terms of the AWS SQS plugin, but it should work.
S3 Upload > SNS Notification > Publish to SQS > Trigger Jenkins Build
Ideally it would be straight to Jenkins like so: S3 Upload > SNS Notification > Publish to Jenkins HTTP Endpoint > Trigger Jenkins Build
Hope this helps
We can write a icron job if linux or powershell script if windows, which queries a particular s3 bucket for the given string, if it finds then u can trigger the Jenkins job.
For doing this, the Jenkins instance must be in the AWS itself if we are trying to add an IAM role, if not we need to add aws credentials.
To implement S3 Upload > Publish to SQS > Trigger Jenkins Build (assuming you have appropriate AWS Users, Roles and Policies attached):
Create an AWS SQS Queue
After creating an AWS SQS Queue, on AWS S3 bucket we need to configure:
S3 Bucket "Events" section to register an "Object Create" event
Provide the SQS Queue name. Detailed documentation.
On Jenkins, we need to:
Install Plugin AWS SQS from the Jenkins Install Plugin Page
Configure AWS SQS Plugin to point to SQS queue in Jenkins System Configuration
Configure the Jenkins Job to "Trigger build when a message is published to an Amazon SQS queue"
Note that Jenkins user MUST have Read access to SQS(all Read fucntions) in addition to S3 access.
Now whenever someone adds/updates anything on the bucket S3 sends an event notification the SQS which is then polled by the Jenkins AWS SQS plugin and the respective Job Build is triggered!
This article explains the process in detail AWS to Github to Jenkins. If you are just using S3 then you would skip the Github part.

Resources