CI/CD on Azure · Appendix to part 3
Initializing the Git repository and connecting it to GitHub
Initializing the local repository, checking the .gitignore, creating the remote repository on GitHub, choosing how to authenticate and pushing the first commit.
- Git
- GitHub
Why this appendix
In article 3, we introduced the sha-$GIT_SHA tag, derived from the current commit hash. This mechanism assumes a prerequisite we never stated explicitly: that the project already lives in a Git repository, with a commit history and a GitHub remote configured. If that is not the case yet, this appendix fills the gap before going further. And it is always good to brush up on the basics!
Step 1: Initializing the local repository
From the root of the project:
git initThis command creates a hidden .git/ subfolder that will hold the whole history of the project. Nothing is tracked or committed yet at this point; it is the equivalent of a mkdir for a repository: the structure exists, but it is empty.
Configuring the Git identity (if not already done globally)
Every commit is signed with a name and an email. If git has never been configured on this machine:
git config --global user.name "Name or handle"
git config --global user.email "[email protected]"--global applies this setting to all your repositories on the machine, not just this one. To check the active configuration at any time:
git config --listStep 2: Checking the .gitignore
Before the first commit, we need to make sure that no sensitive or useless file will be tracked by Git. It is the counterpart of the .dockerignore seen in article 2, but for Git rather than for the Docker build context. The two files overlap a lot, but serve different mechanisms and must both exist.
# Dependencies
node_modules/
# Next.js build
.next/
out/
# Environment variables: never commit secrets
.env.local
.env*.local
# Editors / IDE
.vscode/
.idea/
# System
.DS_Store
Thumbs.dbA point that needs special attention: .env.local (which holds the NEXT_PUBLIC_MAPBOX_TOKEN variable locally) must never be committed, even though we established that this token is not a critical secret once injected into the JS bundle. The discipline of never committing a .env* file must stay systematic, with no case-by-case exception.
If the .gitignore file does not exist yet, better create it before the first git add. A file, once committed, stays in the history even if it is added to the .gitignore afterwards, and it would then take an after-the-fact cleanup, which is trickier.
Step 3: First commit
# Check what will be tracked (check before adding)
git status
# Add all non-ignored files
git add .
# Check one last time what is going to be committed
git status
# First commit
git commit -m "Initial commit"The intermediate git status is not superfluous: it is the moment to spot a file that should not have been tracked (a badly ignored .env.local, for instance) before it enters the history.
Step 4: Creating the remote repository on GitHub
From the GitHub web interface
- On github.com, click New repository
- Name the repository, here WestBaliDrivers
- Leave the repository empty. Do not tick “Add a README”, “Add .gitignore” or “Add license”. These options would create an initial commit on the GitHub side, which would conflict with the local history that already exists.
- Choose Private. Even if it is “only” a showcase site, it is still built for a real network of drivers. Making its code public would amount to giving away the “digital assets” of a small business for free. So, to my mind, keeping it private is the right thing to do.
Step 5: Configuring the remote
Right after the empty repository is created, GitHub shows the commands to run. They look like this:
git remote add origin https://github.com/lolecat/WestBaliDrivers.gitorigin is simply a conventional alias for this remote repository. Nothing forces this exact name, but it is the near-universal convention, worth keeping unless there is a specific reason.
Check:
git remote -vorigin https://github.com/lolecat/WestBaliDrivers.git (fetch)
origin https://github.com/lolecat/WestBaliDrivers.git (push)Step 6: GitHub authentication
Since 2021, GitHub no longer accepts regular passwords for Git authentication over HTTPS. There are two options:
HTTPS + Personal Access Token (PAT)
On the first git push, Git will ask for a username and then a password. This is where a token (generated from GitHub → Settings → Developer settings → Personal access tokens) must be pasted instead of a password.
SSH
If you already have a key pair, make sure it is in the ~/.ssh/ of your host machine.
Otherwise, generate a key pair:
ssh-keygen -t ed25519 -C "[email protected]"The public key (~/.ssh/id_ed25519.pub) then goes into GitHub → Settings → SSH and GPG keys → New SSH key.
You then need to use the SSH URL rather than HTTPS for the remote:
git remote set-url origin [email protected]:lolecat/WestBaliDrivers.gitThe advantage of SSH: no token to type again, authentication relies on the key pair once and for all.
Step 7: First push
# -u links the local branch to the matching remote branch,
# so that the next "git push" commands no longer need origin/main
git push -u origin mainIf the default local branch is called master rather than main (it depends on the Git version and its configuration), adapt the command accordingly, or rename it beforehand with the following command:
git branch -m master mainStep 8: Final check
# Confirms that the code made it up
git log --oneline -5
# Gets the short hash of the current commit, which
# the pipeline will use for the sha-$GIT_SHA tag
git rev-parse --short HEADWhen you refresh the repository page on GitHub, the code should appear there, with the same commit message as the one typed locally in step 3.
For the rest of the series
From now on, every chapter assumes this repository exists, with at least a main branch. The staging branch (and the branching logic tied to the CI/CD pipeline) will be created in the chapter about GitHub Actions.