Configuring Automatic Git Mirroring

This page shows how to configure source code mirroring to the FoundriesFactory® repos. This is useful when using external Git repositories, hosted on services such as GitHub or Bitbucket. While the focus is on setting up GitHub Actions and Bitbucket pipelines, the steps can be adapted for other hosting services.

Creating Token

To allow external hosting services to access your FoundriesFactory repo, you need to create a token. Create a new API Token by clicking on + New Token. Complete with a Description and the Expiration date and select Next. Check the Use for source code access box and select your Factory.

../../_images/mirror-action.png

Fig. 18 Token for source code access

If Bitbucket is used, the token generated in the previous step is used as the <GIT_ACCESS_TOKEN> value. If GitHub is used, convert the token value to a base64 string and save the output of this command. This is your <BASE64_FIO_TOKEN> value.

echo -n <FIO_TOKEN> | base64 -w0
host:~$ echo -n SQMD1Gx860mPI6jZFlLJLwaCXT5CqAaQi6nEfIfH | base64 -w0
U1FNRDFHeDg2MG1QSTZqWkZsTEpMd2FDWFQ1Q3FBYVFpNm5FZklmSA==

Tip

<BASE64_FIO_TOKEN> should end with == with no carriage return.

Configuring GitHub

Go to GitHub and find the repository you want to mirror.

Click on Settings:

../../_images/mirror-action-github-setting.png

Fig. 19 GitHub Settings

Click on Secrets and create a new secret by clicking on New repository secret.

../../_images/mirror-action-github-secrets.png

Fig. 20 GitHub Secrets

The GitHub Action uses the variable GIT_ACCESS_TOKEN as the token to access your Factory repo.

However, complete the Name with GIT_ACCESS_TOKEN, and on Value paste the <BASE64_FIO_TOKEN> provided above.

Finally, click on Add secret.

../../_images/mirror-action-github-new-secret.png

Fig. 21 GitHub New Secret

Creating Mirror Action

The FoundriesFactory CI only triggers builds for configured branches. This is configured in the ci-scripts.git repository in the factory-config.yml file.

Your factory-config.yml can be inspected by updating the following URL with your <FACTORY-NAME>:

Under lmp: and containers:, the group tagging: shows the configured branches on refs/heads/<branch>:.

lmp:
  tagging:
    refs/heads/main:
      - tag: main
...
containers:
  tagging:
    refs/heads/main:
      - tag: main
...

In this example, the CI is configured to trigger new builds whenever a new commit is made to the main branch. The following commands, guides you to mirror the main branch.

Note

Notice that this example is updating the FoundriesFactory repository containers.git. The same approach can be used to update lmp-manifest.git and meta-subscriber-overrides.git.

Clone your GitHub repository and enter its directory:

Note

Make sure to update the clone command with your repository URL.

git clone https://github.com/<host>/<repo_name>
cd <repo_name>

Check out the main branch.

git checkout main

You must store workflow files in the .github/workflows/ directory of your repository.

mkdir -p .github/workflows/

Finally, create the file mirror.yml and make sure you update the <FACTORY-NAME> with your Factory Name.

.github/workflows/mirror.yml:

name: Mirroring

on: [push]

jobs:
  to_foundries:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - uses: foundriesio/mirror-action@main
        with:
          REMOTE: "https://source.foundries.io/factories/<FACTORY-NAME>/containers.git"
          GIT_ACCESS_TOKEN: ${{ secrets.GIT_ACCESS_TOKEN }}
          PUSH_ALL_REFS: "false"

Warning

PUSH_ALL_REFS is false. If true, it synchronizes all branches.

Warning

Make sure you backup any content in your FoundriesFactory repo you want to preserve. This action can completely replace all branches.

Add the changed files, commit and push to your GitHub repository:

git add .github/workflows/mirror.yml
git commit -m "Adding Mirror Action"
git push

GitHub Action

Once mirror.yml is in place, every change to the configured branch will trigger a GitHub Action that mirrors it to you FoundriesFactory Repo.

You can find the GitHub Action by clicking on Actions:

../../_images/mirror-action-github-action.png

Fig. 22 GitHub Action

There, you can find a list of Actions as well as inspect each one.

../../_images/mirror-action-github-action-list.png

Fig. 23 GitHub Action list

Your FoundriesFactory and GitHub hosted repos should look the same.

../../_images/mirror-action-github-compare.png

Fig. 24 FoundriesFactory and GitHub

Configuring Bitbucket

Go to the source repo on Bitbucket and click on Pipelines:

../../_images/bitbucket-pipelines.png

Fig. 25 Bitbucket Pipelines

Select the Starter pipeline:

../../_images/bitbucket-pipelines-start.png

Fig. 26 Bitbucket Starter Pipeline

Replace the default content with the following:

pipelines:
  default:
    - step:
       name: Mirror to source.foundries.io
       image: alpine/git:latest
       script:
         - git push https://[email protected]/factories/<factory-name>/<repo-name>.git --all

Note

Make sure to provide the GIT_ACCESS_TOKEN generated in Creating Token and replace <factory-name> and <repo-name>.

Click on Commit file to enable this pipeline.

After this, every push to the Bitbucket mirrors all branches to source.foundries.io, and triggers builds for the branches enabled in your Factory.

Tip

This pipeline can be customized to mirror only specific branches as needed for your development.