Rename branch in AWS CodeCommit - amazon-web-services

I remember there was an option in gitlab to rename branch but I don't see such option in AWS CodeCommit.
I also tried to find such command in aws command line itility ('aws codecommit help') but there was no any as well.

The only solution I could find:
Let's say we need to rename A -> B. We create branch B from A and then remove A.
Found on some old gitlab forums, probably gitlab did not had 'rename' feature long time ago.
EDIT: If you want to rename a default branch then default branch must be changed before removal (you may switch it to the newly created branch). Default branch configuration is under the Settings repository menu, which is on the same level as Code, Pull requests, Commits and so on.

Related

An AWS CI/CD Pipeline that allows manual deploy by commit

Background
I want to create the following CI/CD flow in AWS and Github, for a react app using Amplify:
A single main branch, with short-lived feature branches and PRs into main.
Each PR triggers its own test environment in Amplify, with its own temporary subdomain, which gets torn down when the PR is merged, as described here.
Merging into main does not automatically trigger a deploy to production.
Instead, there is a separate mechanism (a web page, or amplify command, or even triggers based on git tags) for manually selecting a commit from main to deploy to production.
Questions
It's not clear to me if...
Support for this flow is already built into Amplify (based on the docs I've read, I think the answer is "no", but I'm not sure).
Support for this flow is already built into AWS CodePipeline, or if it can be configured there.
There is another AWS tool that solves this.
I'm looking for answers to those questions, or specific references in the docs which address them.
The answers for Amplify are Yes, Yes, Yes, Partially.
(1) A single main branch, with short-lived feature branches and PRs into main.
Yes. Feature branch deploys. Can define which branch patterns, such as feature*/, you wish to auto-deploy.
(2) Each PR triggers its own test environment in Amplify, with its own temporary subdomain,
Yes. Web Previews for PRs. "A web preview deploys every pull request made to your GitHub repository to a unique preview URL which is completely different from the URL your main site uses."
(3) Merging into main does not automatically trigger a deploy to production.
Yes. Disable automatic builds on main.
(4) Instead, there is a separate mechanism ... for manually selecting a commit from main to deploy to production.
Partially (HEAD only?). Call the StartJob API to manually trigger a build from, say, Lambda. The job type RELEASE starts a new job with the latest change from the specified branch. I am not sure if jobType: MANUAL with a commitId starts a job from an arbitrary commit hash.
Another workaround for 3+4 is to skip the build for an arbitrary commit. Amplify will skip building if [skip-cd] appears at the end of a commit message.
In my experience, I don't think there is any easy way to meet your requirement.
If you are using Gitlab, you can try Gitlab Review Apps to achieve that (I tried before with some scripts)
Support for this flow is already built into Amplify (based on the docs I've read, I think the answer is "no", but I'm not sure).
Check below links, if this help:
https://www.youtube.com/watch?v=QV2WS535nyI
https://dev.to/rajandmr/deploying-react-app-using-aws-amplify-with-ci-cd-pipeline-setup-3lid
Support for this flow is already built into AWS CodePipeline, or if it can be configured there.
For this, you need to create a full your own pipeline. Yes, you can configure your pipeline.
There is another AWS tool that solves this.
If you are okay with Jenkins, then Jenkins will help you to achieve this.
You can deploy Jenkins docker in AWS EC2 and create your pipeline. You can also use the parameterised option for selecting your environment and git branch.

CDK pipelines branching strategy

CDK pipelines seems to only work, by default, with one branch. Am I missing something or is there a way to:
have a dev branch to deploy to the Dev account/ env
test branch deploy to Test account/env.
jons-cool-feature-branch to X account/env etc
Ideally we do not want to have to push everything to the master branch to deploy to dev / test, so that we can keep the master branch clean, tidy, and stable.
I have thought about having multiple pipelines, one for dev, one for test, and one for master, this would solve the issue, but doesn’t feel like the cleanest solution.
Are there any recommended patterns?
The AWS-prescribed best practice is to use trunk-based development.
Thus, a single pipeline cannot use multiple branches for deploying to different environments cleanly.
You should look into creating a single pipeline that would in turn create environment-specific pipelines.
Here is a relevant issue in the CDK repo:
https://github.com/aws/aws-cdk/issues/9461
Solution
Building on what #gshpychka said https://stackoverflow.com/a/69812428/12907894
A pipeline that deploys pipleines. I found lots of overcomplicated solutions online, but in the end it turned out to be quite simple.
Just adding extra pipelines, for each branch we wished to deploy.
A core pipeline that builds the branch pipelines.
Only variables that need to change between any of this:
Account ID
env name
branch name
Account Pattern
Build
Dev
Staging
Prod
core-pipeline
branch
master
Webhook -> null (so it doesn't fire on each build)
Deploys:
master-pipeline -> build account
staging-pipeline -> build account
master-pipeline
branch
master
deploys
app stack -> prod account
staging-pipeline
branch
Staging
deploys
app stack -> staging account
Codepipeline cannot branch. It is not designed to do so.
A solution is to have a multi stage pipeline that has manual approval steps in the middle if you absolutely must have multiple environments and a single pipeline.
That is
Source (Dev branch) -> Build/Deploy -> Manual Approval step -> Make use of of a Codebuild or a lambda to move your now tested code (still in the artifact chain) to your test branch for you (ie make use of a git server api to initiate the merge based on the commit message from the initial commit that started the chain -> Another Build./Deploy to your test env (can even do cross account deployment here) -> Manual Approval step -> Repeat as many times as you want until you deploy to Production.
However.... this is entirely a hack. You're better off with multiple pipelines. I would use the CDK to be able to dynamically adjust the cloudformation template for the pipeline itself to handle Dev/Prod and then simply deploy it twice, linking one to the source of Dev and one to the source of Main.

I am trying to sync two branches in two different repositories in AWS Codecommit. Is there any way to do the sync in an automated way?

Using Codecommit to store the repositories. I have one branches in RepoA and one branch in RepoB. I am trying to sync the changes made in RepoA's branch into RepoB's branch but in an automated way. Is there any way I can use Lambda or codebuild containers to do so?
I don't think so there is any out-of-the-box solution from CodeCommit for this. I also don't think having 2 different repos, one for developer and one for deployment is a good design.
Coming back to answer your question, you can sync the branches in 2 different repos through a script, and call the same from Lambda. However, what you have to take care over here is, are all the parameters to execute this script are well within Lambda's limits?
Assuming the sequence of operation is something like :
Clone repo A.
Checkout branch A.
Add new remote "origin2" as repo B to repo A cloned copy.
Pull from origin2 branch A.
Push to origin2 branch A.
I would say, better to trigger a Container with required script and details/parameters to execute the script.
If you already have any CI setup such as Jenkins/Bamboo, it will be a lot easier.

Use two sources in an AWS-CodePipeline pipeline

I have a specific case which I'm not sure if it's possible with AWS CodePipeline, and I didn't find any information about it in the documentation and event by googling....
So I would like to know if I can set two sources in a pipeline (it could be in the same stage or different stages).
Here is my use case :
I would like my pipeline to start when a file (a specific object) is modified in my s3 bucket
When this file changes and the pipeline is triggered, I would like to clone a codecommit repository and then process the build and other stages...
In the other hand when there is a commit on the master branch of my codecommit repository, I would like the pipeline to start and build my sources.
So The pipeline should be triggered either when the change comes from s3 or codecommit
I don't want to version the s3 file in my codecommit repository because it should be encrypted and used by others teams than dev team working with the git repository
And any time my pipeline starts either if it's from the s3 bucket change or the codecommit push, I should source the commit from the repository for build purposes...
I don't know if my objectives specifications are clear, if yes is it possible to use two source actions in a pipeline as described above and how to achieve this?
Thank you in advance.
Cheers,
Eugène NG
Yes. It is possible to have two sources for an AWS CodePipeline. Or many for that matter. The two sources have to be in your first stage.
Then in your build phase properties, you need to tell it that you are expecting two sources.
Then tell the build project which is your primary source. This is going to be the one that you want your build project to execute the codebuild.
From your buildspec or from any scripts you call, you can then access the source directories by referencing:
$CODEBUILD_SRC_DIR_SourceOutput1
$CODEBUILD_SRC_DIR_SourceOutput2
Just replace SourceOutputX above with what you call your output from the source stage.
I found the following link with more information:
https://docs.aws.amazon.com/codebuild/latest/userguide/sample-multi-in-out.html
Yes, CodePipeline allows multiple source actions in a single pipeline. A change in either source will trigger a pipeline execution. The thing to know is that every pipeline execution will pull the latest source for both actions (not just the one with a change that triggered the pipeline execution).

Is there a way to force delete branch from aws codecommit

When you first create repository on aws codecommit through their API, that repository does not have any branches. Later when you create branch (e.g. master) it is the default one now and there is no way to delete it.
Does anyone know if there is some king of a workaround that will allow deleting of default branch on codecommit through API?
CodeCommit supports updating default branch: https://docs.aws.amazon.com/codecommit/latest/APIReference/API_UpdateDefaultBranch.html, deleting non -default branch: https://docs.aws.amazon.com/codecommit/latest/APIReference/API_DeleteBranch.html. Can you clarify the use case a little bit so that we can understand why you need to delete the default branch directly?